My 'driver' program will not return anything except null for the values I have put into the constructors. The other values work fine, but name, major, id, and studentType do not work, and only null is returned.

Here is the Student file:

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

and the '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 to have the name, id, major, and studentType displayed when stu1.studentRecord() is called.

有帮助吗?

解决方案

In constructor, you 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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top