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

  • Context: Java 
  • Thread starter Thread starter d3nat
  • Start date Start date
  • Tags Tags
    Class Java Program
Click For Summary

Discussion Overview

The discussion revolves around a Java programming issue where a student is struggling to print the details of a student object, specifically why only the GPA is printed instead of all the attributes of the object. The scope includes programming concepts related to object-oriented programming and Java class methods.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • The initial problem involves the student not being able to print all attributes of the Students class using System.out.println(A).
  • One participant identifies that a print function was missing and suggests a constructor that initializes the object with parameters for name, major, and address.
  • Another participant explains that to customize the output of System.out.println(A), a toString() method must be provided in the Students class.
  • There is a suggestion to implement the toString() method to return a formatted string containing all relevant attributes of the student.
  • One participant expresses confusion about how to write the toString() method and seeks clarification on its purpose.
  • Another participant reiterates the need to override the toString() method to achieve the desired output.
  • A later reply confirms understanding of the toString() method's purpose after receiving clarification.
  • One participant requests a complete code example for better understanding.

Areas of Agreement / Disagreement

Participants generally agree on the necessity of implementing a toString() method to achieve the desired output, but there is no consensus on the best approach to do so, as some seek further clarification and examples.

Contextual Notes

Some participants express uncertainty about the implementation details of the toString() method and its significance, indicating a potential gap in foundational knowledge regarding Java class methods.

Who May Find This Useful

Students learning Java programming, particularly those new to object-oriented concepts and class method implementations.

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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
13K