继承与多态性分配

时间:2015-01-31 13:32:20

标签: java inheritance methods polymorphism

我需要编写一个应用程序来显示每个学生的姓名和身份证号码,并计算他们是否通过了。我需要有4个不同的班级学生,学生考试,本科,研究生。

到目前为止,这就是我所拥有的:

学生

class Student {

//private data members
private long idNumber = 0;
private String name = "Not Given";
private int markForMaths = 0;
private int markForEnglish = 0;
private int markForEconomics = 0;
private int markForPhilosophy = 0;
private int markForIT = 0;


//Default constructor
public Student() {
name = "Not Given";
idNumber = 0;
markForMaths = 0;
markForEnglish = 0;
markForEconomics = 0;
markForPhilosophy = 0;
markForIT = 0;  
}

//Constructs a new Student with passed name and age parameters.
public Student(String studentName, long studentIdNumber) {
name = studentName;
idNumber = studentIdNumber;
}

    //Returns the name of student.
    public String getName( ) {
        return name;
    }

    //Returns the idNumber of student.
    public long getIdNumber( ) {
        return idNumber;
    }       

    //entermarks()
    //enter all subject marks given as args
    public void enterMarks(int maths, int english, int economics, int philosophy, int informationTechnology)
    {
    markForMaths = maths;
    markForEnglish = english;
    markForEconomics = economics;
    markForPhilosophy = philosophy;
    markForIT = informationTechnology;
    }

    //getMathsMark()
    //return mark for maths
    public int getMathsMark()
    {
    return markForMaths;
    }

    //getEnglishMark()
    //return mark for English
    public int getEnglishMark()
    {
    return markForEnglish;
    }

    //getEconomicsMark()
    //return mark for Economics
    public int getEconomicsMark()
    {
    return markForEconomics;
    }

    //getPhilosophyMark()
    //return mark for Philosophy
    public int getPhilosophyMark()
    {
    return markForPhilosophy;
    }

    //getITMark()
    //return mark for IT
    public int getITMark()
    {
    return markForIT;
    }

    //calculateAverageMark()
    //return the average of the three marks
    public double calculateAverageMark()
    {
    return ((markForMaths + markForEnglish +
    markForEconomics + markForPhilosophy + markForIT) / 3.0);
    }

    //Sets the name of student.
    public void setName(String studentName ) {
        name = studentName;
    }

    //Sets the idNumber of student.
    public void setIdNumber(long studentIdNumber ) {
        idNumber = studentIdNumber;
    }

}//end class

本科

public class Undergarduate extends Student{



}

研究生

public class Postgraduate extends Student{



}

StudentTest

import java.util.Scanner;

public class StudentTest {

static int array;

//create method createArray
public static Student[] createArray() {

    Scanner int_input = new Scanner(System.in);

    //user enters size of array         
    System.out.print("Enter Size of Array: ");
    array = int_input.nextInt();

    Student[] array = new Student[0]; 

    //read user input as arraySize
    return new Student[5];

}//end method   

//create method populateArray
public static void populateArray(Student[] array) {

    Scanner string_input = new Scanner(System.in);
    Scanner long_input = new Scanner(System.in);
    Scanner int_input = new Scanner(System.in);

    for (int i = 0; i < 3; i++) {
        Student student = new Student(); // new student

        //set name
        System.out.println("Enter Student Name: ");
        student.setName(string_input.nextLine());

        //set ID number
        System.out.println("Enter Student ID Number: ");
        student.setIdNumber(long_input.nextLong());

        //set Marks
        System.out.println("Enter Marks");
        student.enterMarks(int_input.nextInt());

        //put new student into array passed to the method
        array[i] = student; 

    }//end for loop                         
}//end method

//create method display Array
public static void displayArray(Student[] array){
    System.out.println("Array Contents");

    for (Student s : array) {

        System.out.println(String.format("%s %d", s.getName(), 
                s.getIdNumber(), s.getEnglishMark(), s.getMathsMark(), 
                s.getEconomicsMark(), s.getPhilosophyMark(), s.getITMark(),
                s.calculateAverageMark()));

    }//end for loop
}//end method

public static void main(String [] args) {
    // create array of size specified by user
    Student[] students = createArray();

    //populate this array with data from user
    populateArray(students);

    //display array contents
    displayArray(students);    

}//end main method
}//end class

我一直在线上的学生测试中出现错误

student.enterMarks(int_input.nextInt());

错误如下:

Student类型中的方法enterMarks(int,int,int,int,int,int)不适用于参数(int)

2 个答案:

答案 0 :(得分:1)

由于您的班级学生希望每个科目(英语,数学等)的分数,请更改您的StudentTest课程,以便它将每个科目作为输入传递给enterMarks方法。

从以下方式更改您的方法调用:

student.enterMarks(int_input.nextInt());

student.enterMarks(int_input.nextInt(), int_input.nextInt(), int_input.nextInt(), int_input.nextInt(), int_input.nextInt(), int_input.nextInt());//to make it more readable and usable, first take input from keyboard by asking user to enter the number, assign to individual marks variable and then pass it to the method.

答案 1 :(得分:0)

您正在尝试调用方法enterMarks(int),但在Student类中只定义了enterMarks(int, int, int, int, int, int)方法。