how do I call a void method from another class to a new class with main?
I have two classes, but I don't see the error I am making.
public class Person {
private int age;
private String name;
public Person(int a, String n) {
a = age;
n = name;
}
public void printInfo() {
System.out.println(age + name);
}
//
public class Main {
public static void main(String[] args) {
Person obj1 = new Person(22, "Dan");
obj1.printInfo();
}
}
EDIT: Move the main method to a different class and done.
TestPerson.java
public class TestPerson {
public static void main(String[] args) {
Person obj1 = new Person(22, "Dan");
obj1.printInfo();
}
}
You have few mistakes in your code. It should be like as follows :
public class Person {
private int age;
private String name;
//Your constructor was wrong
public Person(int a, String n) {
age = a;
name = n;
}
public void printInfo() {
System.out.println(age + name);
}
}
Related
So I have a MusicBand Class and I want to create a method that merges members of 2 different groups into one and clears the empty one.
public class MusicBand {
private int year;
private String name;
private List<String> members;
public MusicBand(String name, int year, List<String> members) {
this.name = name;
this.year = year;
this.members = members;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getMembers() {
return members;
}
public void setMembers(List<String> members) {
this.members = members;
}
public static void transferMembers(MusicBand a, MusicBand b) {
for (String members : a.getMembers()) {
b.getMembers().add(members);
a.getMembers().clear();
}
}
public void printMembers(){
System.out.println(this.members);
}
}
public class Test4 {
public static void main(String[] args) {
List<String> members1 = new ArrayList<>();
members1.add("a");
members1.add("b");
members1.add("c");
List<String>members2 = new ArrayList<>();
members2.add("a2");
members2.add("b2");
members2.add("c2");
MusicBand group1 = new MusicBand("aaa",1990,members1);
MusicBand group2 = new MusicBand("bbb",2010,members2);
group1.printMembers();
group2.printMembers();
MusicBand.transferMembers(group1,group2);
}
}
So it prints out 2 groups and then instead of merging this happens "Exception in thread "main" java.util.ConcurrentModificationException"
What can I do to fix this?
Thanks in advance.
Move your a.getMembers().clear(); method outside your for loop.
In fact your transferMembers() method could look like the following:
public static void transferMembers(MusicBand a, MusicBand b) {
b.getMembers().add(members);
a.getMembers().clear();
}
There is no need for a for loop at all.
It is also bad practice to use a static method for this. So, your MusicBand class should just have a method to add members to it. So, instead of your static transferMembers(...) method you should have these two:
public void addMembers(MusicBand otherBand) {
getMembers().addAll(otherBand.getMembers());
}
public void clear() {
getMembers().clear();
}
You can then decide whether to call clear() from the calling class or inside the addMembers() method.
I am trying to use a getter to retrieve some variables that the user will input into a GUI, however it gives me an "non-static method cannot be referenced from a static context" error. I have tried making my getters and setters static and that did not work. I am not sure where to go from there. Here is my code:
Main:
public class creditInfoSystem {
String name = infoGUI.getName();
public static void main(String[] args) {
new infoGUI();
}
public void getData() {
}
}
Getters+Setters From GUI Class:
public void setName(String newName){
name = newName;
}
public String getName(){
return name;
}
public void setCustNum(double newCustNum){
custNum = newCustNum;
}
public double getCustNum(){
return custNum;
}
public void setCreditLimit(double newCreditLimit){
creditLimit = newCreditLimit;
}
public double getCreditLimit(){
return creditLimit;
}
public void setPrevBalance(double newPrevBalance){
prevBalance = newPrevBalance;
}
public double getPrevBalance(){
return prevBalance;
}
public void setCurrentPurchases(double newCurrentPurchases){
currentPurchases = newCurrentPurchases;
}
public double getCurrentPurchases(){
return currentPurchases;
}
public void setPayments(double newPayments){
payments = newPayments;
}
public double getPayments(){
return payments;
}
public void setCreditsReturns(double newCreditsReturns){
creditsReturns = newCreditsReturns;
}
public double getCreditsReturns(){
return creditsReturns;
}
public void setLateFees(double newLateFees){
lateFees = newLateFees;
}
public double getLateFees(){
return lateFees;
}
I can provide more parts of the code if needed.
The problem is that the creditInfoSystem class has no idea which infoGUI class you are referring to. Try and make infoGUI a parameter of creditInfoSystem and retrieve the data from there.
public class creditInfoSystem {
private InfoGUI infoGUI;
public creditInfoSystem(InfoGUI gui){
infoGUI = gui;
name = infoGUI.getName();
}
public static void main(String[] args) {
InfoGUI infoGUI = new infoGUI();
CreditInfoSystem sys = new creditInfoSystem(infoGUI);
}
}
If you are building a swing/FX GUI:
One problem I can predict is knowing when the name in the GUI is actually set. You should maybe consider making the GUI the main program where CreditInfoSystem is an attribute of the GUI. Then when a button is clicked or some other action, the input from the GUI is taken and passed to the CreditInfoSystem.
First of all, rename your classs InfoGUI and CreditInfoSystem, starting with an upper case, it is a Java convention.
Then, when you do InfoGUI.getName() you are trying to access a class method, which would have to be static. What you want to do is create an instance of InfoGUI and access it's instance method.
Also your name variable should be static because you use it in a static method, ie public static void main
public class CreditInfoSystem {
private static String name;
public static void main(String[] args) {
// Create an instance of your InfoGUI class
InfoGUI infoGuiInstance = new InfoGUI();
// Call the getter on the instance you just created
name = infoGuiInstance.getName();
}
}
And the renamed InfoGUI class:
class InfoGUI {
String name;
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
}
I have just recently learned about setter , getters and this.(somthing).
I having quite a hard time undersatnding when to use getters and when to use setters .
Another thing , can i use setter method to print out ?
For Example :
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
newAge = workerAge;
}
public void setWorkerName(String newName) {
newName = workerName;
}
public int setIde(int ide) {
ide = workerIde;
return ide;
}
}
public class App {
public static void main(String[] args) {
workerId worker1 = new workerId();
worker1.setWorkerAge(41);
worker1.setWorkerName("dan ");
worker1.setIde(318574524);
System.out.println(worker1.setIde());
}
}
the system out print shows an error and i didnt understand why , is it because only getters can be used in the sysout command ?
No offense intended, but your setters are all wrong. You should assign your properties to the values passed in the setter, not setting the value again. So your code should look like this:
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
workerAge = newAge;
}
public void setWorkerName(String newName) {
workerName = newName;
}
public int setIde(int ide) {
workerIde = ide;
}
}
If you need getters, it should look like this:
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
workerAge = newAge;
}
public void setWorkerName(String newName) {
workerName = newName;
}
public int setIde(int ide) {
workerIde = ide;
}
public int getIde() {
return workerIde;
}
}
Then you can print, e.g. System.out.println(worker1.getIde());
You should be using a getter method to get the values.
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
workerAge = newAge;
}
public void setWorkerName(String newName) {
workerName=newName;
}
public int getIde() {
return workerIde;
}
public void setIde(int ide) {
workerIde = ide;
}
}
public class App {
public static void main(String[] args) {
workerId worker1 = new workerId();
worker1.setWorkerAge(41);
worker1.setWorkerName("dan ");
worker1.setIde(318574524);
System.out.println(worker1.getIde());
}
}
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
this.workerAge = newAge;
}
public void setWorkerName(String newName) {
this.workerName = newName;
}
public int setIde(int ide) {
this.workerIde = ide;
return this.workerIde;
}
}
public class Car {
public static void main(String[] args) {
workerId worker1 = new workerId();
worker1.setWorkerAge(41);
worker1.setWorkerName("dan ");
worker1.setIde(318574524);
System.out.println(worker1.setIde(56));
}
}
Here is my java code
public class Account {
private String accName;
private String accId;
private int balance;
Account()
{
System.out.println("This is an empty constructor.");
}
Account(String name)
{
accName = name;
System.out.println("This is an valued constructor.");
}
public class main{
public Static void main(String[] args)
{
Account a1 = new Account("raff");
}
}
}
cmd says ' "(identifier)" expected
public Static void main(String[] args)'
I can't get the problem yet......
You have several problems there:
public class within public class
main shouldn't be a class there at all
"Static", a mispelling of the static keyword
Probably this is how you meant to write that:
public class Account {
private String accName;
private String accId;
private int balance;
Account() {
System.out.println("This is an empty constructor.");
}
Account(String name) {
accName = name;
System.out.println("This is an valued constructor.");
}
public static void main(String[] args) {
Account a1 = new Account("raff");
}
}
The static keyword, like all other Java keywords, should be written in lowercase:
public static void main(String[] args) {
I have two classes: profesor and subject
public class Profesor {
private int numbClassroom;
public Profesor(int numbClassroom) {
this.numbClassroom = numbClassroom;
}
public int getNumbClassroom() {
return numbClassroom;
}
public void setNumbClassroom(int numbClassroom) {
this.numbClassroom = numbClassroom;
}
public String ToString(){
return "Number of classroom: "+numbClassroom;
} }
The second class is:
public class Subject{
String name;
Profesor lecturer = new Profesor();
Date yearOfStudy;
public void Dodeli(Profesor p){
??????
}}
I do not know how to add professor like a lecturer to a current subject
Like this? I don't see any problem.
public void Dodeli(Profesor p){
lecturer = p;
}
Profesor lecturer = new Profesor();
No need to instantiate lecturer. Just declare it. Then have getter/setter methods for it
Then you can assign Professor to Subject
Subject subj = new Subject("OOP"); //assuming you have corresponding constructor
subj.setLecturer(new Professor()); //or if you have existing prof object
Maybe require something like this : try to encapsulate your code
public class Professor {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Subject{
private String name;
private Professor professor;
private int numbClassroom;
private Date yearOfStudy;
public int getNumbClassroom() {
return numbClassroom;
}
public void setNumbClassroom(int numbClassroom) {
this.numbClassroom = numbClassroom;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Professor getProfesor() {
return professor;
}
public void setProfesor(Professor profesor) {
this.professor = profesor;
}
public void Dodeli(){
System.out.println("Pofessor "+getProfesor().getName()+" is teaching "+getName()+" in Room NO :"+getNumbClassroom());
}
}
public class TestImpl {
public static void main(String arr[])
{
Subject subject = new Subject();
Professor professor = new Professor();
subject.setName("Biology");
professor.setName("MR.X");
subject.setNumbClassroom(1111);
subject.setProfesor(professor);
subject.Dodeli();
}
}