Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Oct 16, 2011 #1
    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() {


    public void setName (String N) {
    public String getName(){
    return name;
    public void setMajor (String M) {
    public String getMajor (){
    return major;
    public void setAddress (String 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();

    Now, at the end of the driver file, I want to put
    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.
  2. jcsd
  3. Oct 16, 2011 #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) {

    public void Print() {

    resulting in my driver program looking like:

    Students A = new Students("Tom", "Physics", "Xtowne");
    Students B = new Students("Charles","English", "Xtowne");

    Just curious, is there a better method?
    I've already submitted the assignment for this, but I'm curious for future homework.
  4. Oct 16, 2011 #3


    User Avatar
    Science Advisor
    Homework Helper

    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.
  5. Oct 16, 2011 #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.
  6. Oct 17, 2011 #5


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    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" [Broken] 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: May 5, 2017
  7. Oct 17, 2011 #6


    User Avatar
    Science Advisor
    Homework Helper

    To get the same output as from your Print() method, you could do something like

    Code (Text):

    public String toString()
        return name + "\t" + major + "\t" + address + "\t" + gpa;
  8. Oct 17, 2011 #7
    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!
  9. Nov 15, 2011 #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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook