PDA

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


d3nat
Oct16-11, 05:37 PM
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 begining 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.

d3nat
Oct16-11, 05:54 PM
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.

AlephZero
Oct16-11, 07:00 PM
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.

d3nat
Oct16-11, 08:21 PM
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.

Borg
Oct17-11, 03:51 AM
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 toString() (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.

AlephZero
Oct17-11, 06:44 AM
To get the same output as from your Print() method, you could do something like


public String toString()
{
return name + "\t" + major + "\t" + address + "\t" + gpa;
}

d3nat
Oct17-11, 08:37 AM
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!

pairofstrings
Nov15-11, 12:36 PM
Nice program. Can you write the correct code one more time? I am taking this course too and I want to understand this program.