2014-04-06 2 views
1

내 '드라이버'프로그램은 생성자에 넣은 값에 대해 null을 제외하고 아무것도 반환하지 않습니다. 다른 값은 정상적으로 작동하지만 name, major, id 및 studentType은 작동하지 않으며 null 만 반환됩니다.다른 클래스 파일에서 호출 할 때 게터가 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 

하고 '드라이버'파일 : 여기

가 학생 파일입니다 내가 이름, 아이디, 주요가 필요

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()); 
} 
} 

및 studentType가 표시 될 때 stu1.studentRecord ()가 호출됩니다.

+0

, 예를 들어,'getName()'은 무엇입니까? –

+0

private String name에 저장된 이름을 반환합니다. –

+0

이제 4 개의 인자를 취하는'Student' 생성자는 무엇을합니까? –

답변

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 
+0

나는 그것을 시도했지만 어떤 이유로 든 항상 오류를 반환합니다. –

+0

실제 및 공식적인 인수는 길이가 다릅니다 –

+0

당신이 잘못 놓았습니다.이 수정 된 답변보기 –

관련 문제