Help with code outputting wrong info
This is the setter and getter part of the program;
Code Java:
public class Student
{
private String name, address, major;
private double gpa;
public Student(String name, String address, String major)
{
this.name = name;
this.address = address;
this.major = major;
gpa();
}
//Will return name
public String getName()
{
return name;
}
//Will return address
public String getAddress()
{
return address;
}
//Will return major
public String getMajor()
{
return major;
}
//Will calculate and return GPA
public double gpa()
{
Random generator = new Random();
float gpa = (float) (generator.nextDouble() * 4.0 + 0.5);
return gpa;
}
}
This is the tester
Code Java:
public class StudentTester
{
public static void main(String[] args)
{
Student student1 = new Student("Kevin","100 Main Street","Spanish");
System.out.println(student1.toString());
System.out.println(gpa()); //error here
Student student2 = new Student("Jim","15 Key Road","Art");
System.out.println(student2.toString());
Student student3 = new Student("Frank","8302 School Drive","Math");
System.out.println(student3.toString());
}
}
Now I'm having two issues, when I run the program without the GPA in the tester part it just outputs with random letters and numbers like so;
Code Java:
Student@9304b1
Student@190d11
Student@a90653
Also, I cannot figure out why I cannot output a random GPA.
Re: Help with code outputting wrong info
What does Student's toString() method return? That looks an awful lot like what is returned by Object's default toString() method.
Re: Help with code outputting wrong info
OP,
You need to override the toString() method in your Student class and print something sensible inside that.
The current output is showing the "Classname @ hex version of object's hashcode" which is the default implementaion of Object class's toString method.
Hope that helps,
Goldest