Java Simple Java class + driver program help. Program isn't working, but should be.

AI Thread Summary
The discussion revolves around a beginner's struggle with a Java program that isn't displaying all desired student information when printed. The user initially encounters frustration because the program only outputs the GPA. The solution involves recognizing the need for a custom `toString()` method in the `Students` class, which overrides the default behavior inherited from the Object class. The user successfully modifies the class by adding a constructor that initializes the student's name, major, and address, along with a `Print()` method to display all attributes. The conversation highlights the importance of the `toString()` method for customizing output when using `System.out.println()`. An example of the `toString()` method is provided, demonstrating how to format the output to include the student's name, major, address, and GPA. The user expresses gratitude for the clarification and seeks further understanding, indicating a positive learning experience.
d3nat
Messages
102
Reaction score
0
Hi :)

I'm taking my first java course. I took two C++ courses last year, and I understand OOP pretty decently, but I cannot get this java program to work! And I'm beginning to get really frustrated because I can't find my mistake.

This is my class file:
package studentsjav;

__________
public class Students {
private double gpa;
private String name, major, address;


public Students() {

name="NoName";
major="NoMajor";
address="NoAddress";
ComputeGPA();
}

public void setName (String N) {
name=N;
}
public String getName(){
return name;
}
public void setMajor (String M) {
major=M;
}
public String getMajor (){
return major;
}
public void setAddress (String A) {
address=A;
}
public String getAdress() {
return address;
}

public double ComputeGPA () {
gpa = (Math.random()*3.5)+.5;
return gpa;
}
/* public String toString(){
String result=Double.toString(gpa);
return result;
}
*/
}

This is the driver file

package studentsjav;

public class StudentsJAV {

public static void main(String[] args) {
// TODO code application logic here
Students A = new Students();
A.setName("D3nat");
A.setMajor("Physics");
A.setAddress("Xville");
A.ComputeGPA();
//System.out.println(A);
}
}

Now, at the end of the driver file, I want to put
System.out.println(A);
and have it print my Student. It only prints the GPA.

Does anyone know why it won't print any of the other information?
I'm really at a loss.
Thank you.
 
Technology news on Phys.org
Wow. I feel really stupid... I forgot to put a print function in my class and I had my variables in my Students object wrong.

Fixed to:

public Students(String N, String M, String A) {
name=N;
major=M;
address=A;
ComputeGPA();
}

and
public void Print() {
System.out.println(name+"\t"+major+"\t"+address+"\t"+gpa);
}

resulting in my driver program looking like:

System.out.println("Name\tMajor\tAddress\tGPA");
Students A = new Students("Tom", "Physics", "Xtowne");
Students B = new Students("Charles","English", "Xtowne");
A.Print();
B.Print();

Just curious, is there a better method?
I've already submitted the assignment for this, but I'm curious for future homework.
 
If you want System.out.println(A) to do something nice, you need to provide a toString() method for your class. Otherwise, A gets printed by the toString() method in the "object" class, whcih won't print anything very interesting.
 
Oh, okay!
This may sound stupid, but how do I write something like that? My professor didn't mention anything like that. I flipped through the book and found an example that used it, but it was used to return the face value for a die example, which didn't really explain anything.
 
d3nat said:
Oh, okay!
This may sound stupid, but how do I write something like that? My professor didn't mention anything like that. I flipped through the book and found an example that used it, but it was used to return the face value for a die example, which didn't really explain anything.
Every class in Java has the Object class as a superclass. The http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html" method is there but, you can override it in any class by putting that method directly in your class. The method signature has to be exactly the same:

public String toString()

After that, you can put whatever you like in there.
 
Last edited by a moderator:
To get the same output as from your Print() method, you could do something like

Code:
public String toString() 
{ 
    return name + "\t" + major + "\t" + address + "\t" + gpa; 
}
 
AlephZero said:
To get the same output as from your Print() method, you could do something like

Oh, I see. I didn't know that was the purpose of the toString method. Thank you for clearing that up. I think I understand it now!
 
Nice program. Can you write the correct code one more time? I am taking this course too and I want to understand this program.
 
Back
Top