创建一个包含3个变量的数组

时间:2017-03-14 20:40:59

标签: java arrays

我正在尝试创建一个包含3种不同类型学生的学生阵列,每个学生将有3个变量名称和2个等级。 这是我到目前为止所做的,它给我以下错误找不到符号。

主要课程:

public class JavaLab5 {
    public static final int DEBUG = 0;

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

        Student s[] = new Student[10];


        s[0] = new MathStudent(Smith,14,15);
        s[1] = new MathStudent(Jack,16,19);
        s[2] = new MathStudent(Victor,18,21);
        s[3] = new MathStudent(Mike,23,28);
        s[4] = new ScienceStudent(Dave,32,25);
        s[5] = new ScienceStudent(Oscar,28,56);
        s[6] = new ScienceStudent(Peter,29,28);
        s[7] = new ComputerStudent(Philip,25,38);
        s[8] = new ComputerStudent(Shaun,34,39);
        s[9] = new ComputerStudent(Scott,45,56);

        for (int loop = 0; loop < 10; loop++) {
            System.out.println(s[loop].getSubjects());
            System.out.print(loop + " >>" + s[loop]);
        }
    }

}

这是学生班:

public class Student {
    private String name;
    //private int age;
    //public String gender = "na";
    public static int instances = 0;

    // Getters
    //public int getAge() {
        //return this.age;
    //}
    public  String getName() {
        return this.name;
    }

    // Setters
    //public void setAge(int age) {
        //this.age = age;
    //}
    public void setName(String name) {
        if (JavaLab5.DEBUG > 3) System.out.println("In Student.setName. Name = "+ name);
        this.name = name;
    }

    /**
     * Default constructor. Populates name,age and gender 
     * with defaults
     */
    public Student() {
        instances++;
        //this.age = 18;
        this.name = "Not Set";
        //this.gender = "Not Set";
    }

    /** 
     * Constructor with parameters 
     * @param age integer
     * @param name String with the name
    */
    public Student(String name) {
        //this.age = age;
        this.name = name;
    }

    /** 
     * Gender constructor
     * @param gender 
     */
    //public Student(String gender) {
        //this(); // Must be the first line!
        //this.gender = gender;
    //}

    /**
     * Destructor
     * @throws Throwable 
     */
    protected void finalize() throws Throwable {
        //do finalization here
        instances--;
        super.finalize(); //not necessary if extending Object.
    } 

    public String toString () {
        return "Name: " + this.name; //+ " Age: " + this.age + " Gender: " 
               //+ this.gender;
    }

    public String getSubjects() {
      return this.getSubjects();
    }
}

这是继承自Student类的MathStudent类:

public class MathStudent extends Student {

    private float algebraGrade;
    private float calculusGrade;

    /**
     * Default constructor
     * @param name
     * @param algebraGrade
     * @param calculusGrade
     */
    public MathStudent(String name, float algebraGrade, float calculusGrade) {
        super();
        this.algebraGrade = algebraGrade;
        this.calculusGrade = calculusGrade;
    }

    public MathStudent() {
        super();
        algebraGrade = 6;
        calculusGrade = 4;
    }

    // Getters
    public void setAlgebraGrade(float algebraGrade){
        this.algebraGrade = algebraGrade;
    }
    public void setCalculusGrade(float calculusGrade){
        this.calculusGrade = calculusGrade;
    }

    // Setters
    public float getAlgebraGrade() {
        return this.algebraGrade;
    }
    public float getCalculusGrade() {
        return this.calculusGrade;
    }

    /**
     * Display information about the subject
     * @return 
     */
    @Override
    public String getSubjects(){
        return(" Math Student     >> " + "Algebra Grade: "  + algebraGrade 
                + " Calculus Grade: "  + calculusGrade);
    }
}

1 个答案:

答案 0 :(得分:2)

检查你如何实例化学生,即

new MathStudent(Smith,14,15); 

名称应该在&#34; Smith&#34;

之类的引号中
new MathStudent("Smith",14,15); 

否则Smith将被解释为变量,并且未定义此变量。