I have problem with printing out the output .
The class methods :
public class Account {
private String name1;
private String name2;
private double grade1;
private double grade2;
private double grade3;
private double average;
public void setAccount(String a1 ,String a2 ,double b1,double b2,double b3) {
a1=name1;
a2=name2;
b1=grade1;
b2=grade2;
b3=grade3;
}
public void setaverage(double k1){
k1=average;
}
public String getAccount(){
return name1;
}
public String getAccount1(){
return name2;
}
public double getAccount2(){
return grade1;
}
public double getAccount3(){
return grade2;
}
public double getAccount4(){
return grade3;
}
public double getaverage(){
return average=(grade1+grade2+grade3)/3;
}
}
And the main method:
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
Account account= new Account();
System.out.println("Enter the frist and last name of student" );
String na1=in.nextLine();
account.getAccount(na1);
} // end main
} // end class AccountTest
I have problem in account.getAccount(na1);
I tried to print one String to know if the program works .
So how to print the output?
I know that the problem because there’s no argument in the get method, but I don’t know how to print it, since it’s my first time using it .
There are plenty of issues here but don’t worry I’ll run you through them. There’s lots of little things you can do to make your code easier for you and others to understand. Encapsulation seems confusing at first but it starts to make sense pretty fast I found. First off in your constructor you’re doing the assignment backwards. I take it your teacher is making you use “getters” and “setters” for every private variable?
Create methods with these signatures:
And use them in your constructor:
Next I’d reccommend naming your get* methods in a similar way.
etc.
And, finally, for printing. Once you have all that set up printing to the console is very easy.
Read through the code and annotate it to explain (to yourself) what it’s doing. It starts to read more and more like english/humanLanguageOfChoice as you go on!
I’m a student too so if any real developers feel like having a dig go ahead.