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?
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:
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