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

In summary, the student is having difficulty getting their Java program to work and is beginning to get frustrated.
  • #1
d3nat
102
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5
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:
  • #6
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; 
}
 
  • #7
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!
 
  • #8
Nice program. Can you write the correct code one more time? I am taking this course too and I want to understand this program.
 

1. Why is my Java program not working?

There could be several reasons why your Java program is not working. Some common issues include syntax errors, logical errors, and runtime errors. It is important to carefully check your code for any mistakes and use debugging tools to identify and fix any errors.

2. How can I debug my Java program?

To debug your Java program, you can use tools such as the Java debugger (jdb), which allows you to step through your code and check the values of variables at different points. You can also use print statements or logging to track the flow of your program and identify any errors.

3. What are some common errors in Java programming?

Some common errors in Java programming include syntax errors, which occur when the code does not follow the correct syntax rules, and logical errors, which occur when the code does not produce the desired output. Runtime errors can also occur if there are issues with memory allocation or improper handling of exceptions.

4. How can I ensure my Java program runs smoothly?

To ensure your Java program runs smoothly, it is important to follow best practices such as using descriptive variable names, commenting your code, and properly indenting your code for readability. It is also important to test your code thoroughly and handle any potential errors or exceptions.

5. What resources can I use to improve my Java programming skills?

There are many resources available to improve your Java programming skills, such as online tutorials, coding challenges, and books. You can also join online communities or attend coding workshops to learn from others and improve your skills. Practice and hands-on experience are key to becoming a proficient Java programmer.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
815
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
5
Views
801
Back
Top