学生班。如何打印基于等级的单位输入?

时间:2017-07-16 01:40:33

标签: java

public class Student {

public static void main(String[] args) {
    // TODO Auto-generated method stub

}

private static int ID;
private String name;
private double units;
public String grade;

public Student(int ID, String name, double units) {
    this.ID = ID;
    this.name = name;
    this.units = units;
}

public void setID(int ID) {
    this.ID = ID;
}

public static int getID() {
    return ID;
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void setUnits(double units) {
    this.units = units;
}

public double getUnits() {
    return units;
}

public void setGrade(double units) {
    if (units < 30) {
        grade = "Freshman";
    } else if (units >= 31 && units <= 60) {
        grade = "Sophmore";
    } else if (units >= 61 && units < 90) {
        grade = "Junior";
    } else {
        grade = "Senior";
    }
}

public String getGrade(String grade) {
    return grade;
}

}

STUDENTTEST

import java.util.Scanner;
public class StudentTest {


public static void main(String[] args){
Student student = new Student(0, " ", 0);

Scanner scan = new Scanner(System.in);

int ID;
String name;
double unitsTaken;
String grade = "";

System.out.println("Student information:");
System.out.print("Student ID: ");
ID = scan.nextInt();
student.setID(ID);

scan.nextLine();
System.out.print("Student Name: ");
name = scan.nextLine();
student.setName(name);


System.out.print("Total Number of Units Completed: ");
unitsTaken = scan.nextDouble();
student.setUnits(unitsTaken);


    student.getGrade(grade);

    System.out.println("\nUpdated Student information");
    System.out.println("Student ID: " + ID);
    System.out.println("Student Name: " + name);
    System.out.println("Total Number of Units Completed: " + unitsTaken);
    System.out.println("Class standing: " + grade);
}


}

输出:

Student information:
Student ID: 123123
Student Name: john
Total Number of Units Completed: 34

Updated Student information
Student ID: 123123
Student Name: john
Total Number of Units Completed: 34.0
Class standing: 

班级站立不打印。我希望它根据输入的单位数量进行打印。为什么我的方法不起作用?

1 个答案:

答案 0 :(得分:0)

简而言之,问题是&#34;两个属性如何依赖于一个字段?&#34;有问题的字段是单位,它应该有一个且只有一个setter或&#34; mutator&#34;方法。两个属性getUnit和getGrade应该有两个getter或accessor方法。如,

public class Student {
    private int units;

    public int getUnits() {
        return units;
    }

    public void setUnits(int units) {
        this.units = units;
    }

    public String getGrade() {
        if (units < 30) {
            return "Freshman";
        } else if (units < 60) {
            return "Sophmore";
        } else if (units < 90) {
            return "Junior";
        } else {
            return "Senior";
        }
    }

    // Do not have a setGrade method!
}

这样可以使等级成为计算出来的,或者来自&#34;属性,是从单位​​字段的值派生的属性。

注意,这是错误的:

student.getGrade(grade);

它不应该使用参数:

student.getGrade();  // better

另一种选择是创建静态&#34;实用程序&#34;计算方法:

public static String getGrade(int units) {
    if (units < 30) {
        return "Freshman";
    } else if (units < 60) {
        return "Sophmore";
    } else if (units < 90) {
        return "Junior";
    } else {
        return "Senior";
    }
}

在这种情况下,您将传递units int值并以静态方式调用该方法:

System.out.println("Student grade: " + Student.getGrade(student.getUnits());