我的驱动程序'对于我放入构造函数的值,程序不会返回除null之外的任何内容。其他值工作正常,但name,major,id和studentType不起作用,只返回null。
这是学生档案:
public class Student
{
private String name; //name of the student
private String id; //student id
private String major; //student's major
private int completedHours; //number of hours the student has completed
private int qualityPoints; //number of overall quality points the 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
和&#39;驱动程序&#39;文件:
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());
}
}
调用stu1.studentRecord()时,我需要显示name,id,major和studentType。
答案 0 :(得分:0)
在构造函数中,您应该设置,而不是获取:
public Student(String name)
{
setName(name);
}
public Student(String name, String id, String major, char studentType)
{
setName(name);
setId(id);
setMajor(major);
setStudentType(studentType);
}
正确的二传手:
public void setName(String name)
{
this.name = name; // NOT name = this.name !!!
}
// in other setters:
// this.id = id
// this.major = major
// this.studentType = type