Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 学生对象数组_Java_Arrays - Fatal编程技术网

Java 学生对象数组

Java 学生对象数组,java,arrays,Java,Arrays,下面是我的教授给我的硬件: 定义扩展Person的类学生。它添加了属性 Int Test1、test2、test3 双倍平均 弦级 它有computeaverage()和calculategrade()方法。分数基于平均值,高于90 A、80至90 A B、70至80 A C等。所有其他属性都有一个集合和一个get 编写一个使用大小为20的student类型数组的应用程序。该程序提示用户课堂上有多少学生,然后允许用户输入学生和他们的考试成绩,然后计算他们的成绩并打印出学生和成绩列表 话虽如此 周

下面是我的教授给我的硬件:

  • 定义扩展Person的类学生。它添加了属性
  • Int Test1、test2、test3 双倍平均 弦级

    它有computeaverage()和calculategrade()方法。分数基于平均值,高于90 A、80至90 A B、70至80 A C等。所有其他属性都有一个集合和一个get

  • 编写一个使用大小为20的student类型数组的应用程序。该程序提示用户课堂上有多少学生,然后允许用户输入学生和他们的考试成绩,然后计算他们的成绩并打印出学生和成绩列表
  • 话虽如此

    周四,我看到一个同学的代码,他从老师那里得到的,他有一些我以前在37行(学生构造器)的学生课上没有见过的东西。他没有使用getter和setter,而是使用了类似于我在第37行中使用的代码。但我不知道他做了什么和正确的编码。所以我希望这里的人能告诉我我做错了什么,以及这些代码如何在不使用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;
        }
    
    
    
    }
    
    公共类人物{
    /**
    *@param指定命令行参数
    */
    公共静态void main(字符串[]args)
    {
    扫描仪kbd=新扫描仪(System.in);
    学生新闻学生=新学生();
    整数大小;
    System.out.println(“输入学生人数:”);
    大小=kbd.nextInt();
    学生[]myStudent=新学生[人数];
    字符串名;
    字符串lastName;
    int test1、test2、test3;
    学生证;
    对于(int i=0;i
    通常,您希望尽可能多地封装字段。这意味着要做这些

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

    你的个人课错了。它没有属性或方法。没有理由延长它,因为它不会给党带来任何好处

    如果属性对公众可见,则不需要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;
        }
    }
    
    公共类人物{
    受保护的字符串名;
    受保护的字符串lastName;
    公众人物(字符串f、字符串l){
    this.firstName=f;
    this.lastName=l;
    }
    公共字符串getFirstName(){返回this.firstName;}
    公共字符串getLastName(){返回this.lastName;}
    }
    酒吧
    
    public Student(int test1, int test2, int test3, String firstName, String lastName) 
    {
        this.test1 = test1;
        this.test2 = test2;
        this.test3 = test3;
        ...
    }
    
    this.average = 0; // Or maybe 'this.average = computeAverage();'
    this.firstName = firstName;
    this.lastName = lastName;