java - Getters returning null values when called in another class file -
my 'driver' program not return except null values have put constructors. other values work fine, name, major, id, , studenttype not work, , null returned.
here student file:
public class student { private string name; //name of student private string id; //student id private string major; //student's major private int completedhours; //number of hours student has completed private int qualitypoints; //number of overall quality points student has earned private char studenttype; //type of student g (graduate) u (undergraduate) x (invalid type) public student() { }//end student() public student(string name) { getname(); }//end student(string) public student(string name, string id, string major, char studenttype) { getname(); getid(); getmajor(); getstudenttype(); }//end student(string, string, string, char) public void setname(string name) { name = this.name; }//end setname(string) public void setid(string id) { id = this.id; }//end setid(string) public void setmajor(string major) { major = this.major; }//end setmajor(string) public void setcompletedhours(int hours) { if (hours > 0) completedhours = hours; }//end setcompletedhours(int) public void setqualitypoints(int points) { if (points > 0) qualitypoints = points; }//end setqualitypoints(int) public void setstudenttype(char type) { if (type == 'g' || type == 'g') type = 'g'; if (type == 'u' || type == 'u') type = 'u'; if (type != 'u' || type != 'g') type = 'x'; if (type != 'u' || type != 'g') type = 'x'; type = studenttype; }//end setstudenttype(char) public string getname() { return name; }//end getname() public string getid() { return id; }//end getid() public string getmajor() { return major; }//end getmajor() public int getcompletedhours() { return completedhours; }//end getcompletedhours() public int getqualitypoints() { return qualitypoints; }//end getqualitypoints() public char getstudenttype() { return studenttype; }//end getstudenttype() public double gpa() { double dgpa; double dpoints = qualitypoints; double dhours = completedhours; dgpa = dpoints/dhours; return dgpa; }//end gpa() public void addcompletedhours(int hours) { if (hours > 0) completedhours += hours; }//end addcompletedhours(int) public void addqualitypoints(int points) { if (points > 0) qualitypoints += points; }//end addqualityhours(int) public string classification() { string strclass = ""; if (studenttype == 'g') strclass += "graduate student, "; if (studenttype == 'u') strclass += "undergraduate, "; if (studenttype == 'x') strclass += "invalid student type, "; if (completedhours >= 90) strclass = "senior"; if (completedhours < 90) strclass = "junior"; if (completedhours < 60) strclass = "sophomore"; if (completedhours < 30) strclass = "freshman"; return strclass; }//end classification() public string studentrecord() { decimalformat df = new decimalformat("#,###0.000"); string stroutput = ""; stroutput += "\n name: " + name; stroutput += "\n id: " + id; stroutput += "\n major: " + major; stroutput += "\ncompleted hours: " + completedhours; stroutput += "\n quality points: " + qualitypoints; stroutput += "\n gpa: " + df.format(gpa()); stroutput += "\n classification: " + classification(); return stroutput; }//end studentrecord() }//end student
and 'driver' file:
import java.util.scanner; public class proj3 { public static void main(string[] args) { scanner kb = new scanner(system.in); student stu1 = new student("john smith", "1234b", "nursing", 'u'); stu1.setcompletedhours(34); stu1.setqualitypoints(85); student stu2 = new student("helen johnson", "5678t", "computer science", 'u'); stu2.setcompletedhours(64); stu2.setqualitypoints(210); student stu3 = new student("betty jones", "7890e", "mathematics", 'g'); stu3.setcompletedhours(26); stu3.setqualitypoints(85); system.out.println("\njohn smith: " + stu1.classification()); system.out.println("\nhelen johnson: " + stu2.classification()); system.out.println("\nbetty jones: " + stu3.classification()); stu1.addcompletedhours(51); stu1.addqualitypoints(170); system.out.print("\njohn smith: " + stu1.studentrecord()); system.out.print("\n\nbetty jones: " + stu3.studentrecord()); } }
i need have name, id, major, , studenttype displayed when stu1.studentrecord() called.
in constructor, should set, not get:
public student(string name) { setname(name); } public student(string name, string id, string major, char studenttype) { setname(name); setid(id); setmajor(major); setstudenttype(studenttype); }
and correct setters:
public void setname(string name) { this.name = name; // not name = this.name !!! } // in other setters: // this.id = id // this.major = major // this.studenttype = type
Comments
Post a Comment