【问题标题】:Array of Student objects java学生对象数组 java
【发布时间】:2014-01-26 18:12:07
【问题描述】:

只是为了简要介绍我正在尝试做的事情,这是我的教授给我的硬件:

  1. 定义一个扩展 Person 的 Student 类。它添加了属性

内部测试1,测试2,测试3 双重平均 字符串等级

它有方法computeaverage() 和calculategrade()。成绩是基于平均水平的,90以上是A,80到90是B,70到80是C等。所有其他属性都有一个设置和一个获取。

  1. 编写一个应用程序,使用大小为 20 的 student 类型的数组。程序会提示用户班级中有多少学生,然后允许他们输入学生及其考试成绩,然后计算他们的成绩并打印出来学生名单和他们的成绩。

话说回来……

星期四,我看到了他从老师那里得到的同学代码,他有一些我以前在第 37 行(学生构造函数)的学生课上从未见过的东西。他的代码与我在第 37 行的代码相似,而不是 getter 和 setter。但我不知道他做了什么以及正确的编码。所以我希望这里有人能告诉我我做错了什么以及这段代码如何在不使用 getter 和 setter 方法的情况下逃脱???

public class Person {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {



        Scanner kbd = new Scanner(System.in);
        Student newStudent = new Student();
        int size;

        System.out.println("Enter the amount of students:");
        size = kbd.nextInt();
        Student[] myStudent = new Student[size];
        String firstName;
        String lastName;
        int test1, test2, test3;
        Student s;

        for (int i=0; i < size; i++)
        {

        System.out.println("Enter first name of student: " + i);
        firstName = kbd.next();

        System.out.println("Enter last name if student: " +i);
        lastName = kbd.next();

        System.out.println("Please Enter first test score: ");
//        JOptionPane.showInputDialog("Please enter first test score:");
        test1= kbd.nextInt();

        System.out.println("Please enter second test score");
//        JOptionPane.showInputDialog("Please enter second test score:");
        test2= kbd.nextInt();

        System.out.println("Please enter third test score");
//        JOptionPane.showInputDialog("Please enter third test score:");
        test3=kbd.nextInt();

//        s = new Student (test1, test2, test3, firstName, lastName);
        myStudent[i].setTest1(test1);
        myStudent[i].setTest2(test2);
        myStudent[i].setTest3(test3);
        myStudent[i].setfName(fName);
        myStudent[i].setlName(lname);


        }
        for (int i = 0; i < size; i++)
        {
            System.out.println(myStudent[i].getGrade());
        }



    }
}


public class Student extends Person{

    int test1, test2, test3;
    double average;
    String grade, firstName, lastName;


    public Student() 
    {
        test1 = 0;
        test2 = 0;
        test3 = 0;
        average = 0;


    }




    public Student(int test1, int test2, int test3, String firstName, String lastName) 
    {
        this.test1 = test1;
        this.test2 = test2;
        this.test3 = test3;

        this.setfirstName = firstName;
    }


    public double computeAverage()
    {
        average = (test1 + test2 + test3)/3;
        return average;

    }

    public String calculateGrade()
    {
        average = computeAverage();

        if (average < 60){
            grade = "F";}
        else if (average < 70){
            grade = "D";}
        else if (average < 80){
            grade = "C";}
        else if (average < 90){
            grade = "B";}
        else {
            grade = "A";
        }
        return grade;
    }

    public int getTest1() {
        return test1;
    }

    public void setTest1(int test1) {
        this.test1 = test1;
    }

    public int getTest2() {
        return test2;
    }

    public void setTest2(int test2) {
        this.test2 = test2;
    }

    public int getTest3() {
        return test3;
    }

    public void setTest3(int test3) {
        this.test3 = test3;
    }

    public double getAverage() {
        return average;
    }

    public void setAverage(double average) {
        this.average = average;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }



}

【问题讨论】:

  • 不是很清楚,37是哪一行?因为有两个构造函数,一个不带参数,一个带5个。
  • 他使用了overloaded 构造函数。您可以调用Student() 并手动输入值或调用Student(int, int, int, String, String) 并在构造函数中一键设置它们。即使您使用第二个示例,您的代码仍然应该有 setter/getter。

标签: java arrays


【解决方案1】:

你的 Person 类是错误的。它没有属性或方法。没有理由延长它,因为它不会给派对带来任何好处。

如果属性对公众可见,则不需要 getter 或 setter,但这并不意味着这是一个好主意。

试着像这样思考:

public class Person {
    protected String firstName;
    protected String lastName;

    public Person(String f, String l) {
        this.firstName = f;
        this.lastName = l;
    }

    public String getFirstName() { return this.firstName; }
    public String getLastName() { return this.lastName; }
}

public class Student extends Person {
    public static final int MAX_GRADES = 3;
    private int numGrades = 0;
    private int maxGrades;
    private int [] grades;

    public Student(String f, String l) {
        this(f, l, MAX_GRADES);
    }

    public Student(String f, String l, int maxGrades) {
        super(f, l);
        this.maxGrades = (maxGrades > 0) ? maxGrades : MAX_GRADES;
        this.grades = new int[this.maxGrades];   
    }

    public void addGrade(int grade) {
        if (this.numGrades < this.maxGrades) {
            this.grades[numGrades++] = grade;
        }
    }

    public double getAverageGrade() { 
        double average = 0.0;
        if (this.numGrades > 0) {
            for (int i = 0; i < this.numGrades; ++i) {
                average += grade[i];
            }
            average /= this.numGrades;
        }
        return average;
    }
}

【讨论】:

    【解决方案2】:

    Studentclass 中有两个构造函数

    • 没有参数的构造函数
    • 带参数的构造函数 (int,int,int,String,String)

    这称为方法/函数重载。你可以用同名声明许多方法,但签名必须改变。换句话说,它必须有不同的参数(这样编译器就会知道要使用哪个版本的方法)。

    所以你有一个没有参数的构造函数,它只是将test1test2test3average 设置为0。你有这个构造函数:

    public Student(int test1, int test2, int test3, String firstName, String lastName) 
    {
        this.test1 = test1;
        this.test2 = test2;
        this.test3 = test3;
        ...
    }
    

    接收4 参数并将它们分配给相应的字段。

    注意你也应该在构造函数中初始化average,并设置firstNamelastName

    this.average = 0; // Or maybe 'this.average = computeAverage();'
    this.firstName = firstName;
    this.lastName = lastName;
    

    【讨论】:

      【解决方案3】:

      通常,您会希望尽可能地封装您的字段。这意味着让这些

      int test1, test2, test3;
      double average;
      String grade, firstName, lastName;
      

      私密的东西。然后,您将需要 getter 和 setter 从类外访问它们。这是好事。但是,在类内部,您可以在没有 getter 或 setter 的情况下使用它们,没有问题。

      这是否回答了您的问题?我不知道第 37 行是什么,因为您没有提供编号。 :)

      编辑:如果您不知道,构造函数可以重载。你有两个不同的构造函数,一个有参数,一个没有。您可以选择要使用哪一个来构建类,您可能希望使用第二个。

      如果您需要两个构造函数,一个是完整的,另一个是使用默认值的,您可能需要考虑从第一个构造函数内部引用第二个构造函数,以避免重复代码。像这样:

      public Student() {
          this(0,0,0);
      }
      
      public Student(int test1, int test2, int test3, String firstName, String lastName) {
          this.test1 = test1;
          this.test2 = test2;
          this.test3 = test3;
          this.average = 0;
          this.firstName = firstName;
          this.lastName = lastName;
      }
      

      【讨论】:

        【解决方案4】:

        他的实例变量(Student开头声明的变量)是public,这使得他可以直接访问和更改它们,而无需使用setter和getter。通常,这些变量是私有的,需要方法来访问/更改它们。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-05-31
          • 1970-01-01
          • 2014-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多