Results 1 to 3 of 3
  1. #1
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default main not passing information to created class

    My code isn't sending the name, id, and gpa to the Student class. I put a print statement inside the Student class and it prints out null 0 0.0, and thats with both the provided code outline and the filled in part for Pistol Pete. Heres my code, am I doing something wrong?

    Java Code:
    public class Lab13
    {
      //Constants
       private final static String NAME = "Kodie Hill";
       private final static int STUID = 123456789;
       private final static double GPA1  = 4.00;
       private final static double GPA2  = 2.34;
       private final static String NAME2 = "Pistol Pete";
       private final static int STUID2 = 000000001 ;
       private final static double GPA3  = 4.00;
    
    
      //main method
       public static void main (String[] args)
       {
          Student stu1;
    
          stu1 = new Student(NAME, STUID, GPA1);
    
          System.out.println("Name: " + stu1.getName());
          System.out.println("Id Number: " + stu1.getIdNum());
          System.out.println("GPA : " + stu1.getGPA());
          stu1.setGPA(GPA2);
          System.out.println(stu1 + "\n");
    
          // Create a second student object
          // With a name of Pistol Pete, a
          // gpa of 4.00, and a student Id of
          // 000000001
          Student stu2;
    
          stu2 = new Student(NAME2, STUID2, GPA3);
          // Print out this student similar to above.
          // You do not need to reset the gpa of this student.
          System.out.println("Name: " + stu2.getName());
          System.out.println("Id Number: " + stu2.getIdNum());
          System.out.println("GPA : " + stu2.getGPA());
          System.out.println(stu1);
    
    
       } // end of main
    } // end of class
    
    public class Student
    {
    
       private String name;
       private int idNum;
       private double gpa;
    
      // Constructor
       public Student(String namePar, int idNumPar, double gpaPar)
       {
    
          // Save namePar to class variable name
          namePar = name;
          // Save idNumPar to class variable idNum
          idNumPar = idNum;
          // Save gpaPar to class variable gpa
          gpaPar = gpa;
          System.out.println(namePar + " " + idNumPar + " " + gpaPar);
       }
    
      // Accessor: returns name of student
       public String getName()
       {
        // Return the name of the student
          return name;
       }
    
      // Accessor: returns GPA of student
       public double getGPA()
       {
        // Return the gpa of the student
          return gpa;
       }
    
      // Accessor: returns idNum of student
       public int getIdNum()
       {
        // Return the idnum of the Student
          return idNum;
       }
    
    
      // Mutator: Changes the GPA of the student
       public void setGPA(double g)
       {
        // Set the class variable gpa equal to g
          g = gpa;
       }
    
      // toString method: Returns a formatted string
      //  of student information.
      // As shown in sample output.
       public String toString()
       {
          // Return the formatted String.
          return
          "Student Name  : " + name + "\n" +
          "Student ID num: " + idNum + "\n" +
          "Student GPA   : " + gpa;
                                     
    }
    } // end of class
    Resulting output:
    Java Code:
    null 0 0.0
    null 0 0.0
    Kodie Hill 123456789 4.0
    Name: null
    Id Number: 0
    GPA : 0.0
    Student Name  : null
    Student ID num: 0
    Student GPA   : 0.0
    
    null 0 0.0
    null 0 0.0
    Pistol Pete 1 4.0
    Name: null
    Id Number: 0
    GPA : 0.0
    Student Name  : null
    Student ID num: 0
    Student GPA   : 0.0

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    It could be cause they are static

    It's better practice to assign values in objects like this:

    // Constructor

    private String namePar;
    private int idNamePar;
    private double gpPar;

    public Student(String namePar, int idNumPar, double gpaPar)
    {


    this.namePar = namePar;

    this.idNumPar = idNumPar;

    this.gpaPar = gpaPar;
    System.out.println(namePar + " " + idNumPar + " " + gpaPar);
    }
    Makes stuff neater
    Last edited by FlyNn; 04-19-2011 at 09:57 PM.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  3. #3
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    Turns out that in the constructer I needed to put the (variable)Par on the right side of the equal. It passed it correctly, it just over recorded null and 0's into the variables.

Similar Threads

  1. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 09:30 AM
  2. Passing array arguments into main
    By JohnDas in forum New To Java
    Replies: 12
    Last Post: 11-10-2010, 04:00 PM
  3. Replies: 7
    Last Post: 05-06-2009, 01:42 PM
  4. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 01:25 AM
  5. Passing variable information between classes
    By zen_to_go in forum New To Java
    Replies: 1
    Last Post: 10-30-2007, 09:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •