java子类有参构造函数_Java 子类有参构造器报错

本文介绍了如何使用Java创建一个名为'Person'的类,继承自'Person'并扩展了'Student',用于存储个人姓名、姓氏、ID和测试分数。重点讲解了如何构造函数和'calculate'方法来计算学生的平均成绩并输出等级。
摘要由CSDN通过智能技术生成

import java.util.*;classPerson {protectedString firstName;protectedString lastName;protected intidNumber;//Constructor

Person(String firstName, String lastName, intidentification){this.firstName =firstName;this.lastName =lastName;this.idNumber =identification;

}//Print person data

public voidprintPerson(){

System.out.println("Name: " + lastName + ", " +firstName+ "\nID: " +idNumber);

}

}class Student extendsPerson{private int[] testScores;/** Class Constructor

*

* @param firstName - A string denoting the Person's first name.

* @param lastName - A string denoting the Person's last name.

* @param id - An integer denoting the Person's ID number.

* @param scores - An array of integers denoting the Person's test scores.*/

//Write your constructor here

Student(String firstName, String lastName, int id, int[] scores){

super(); //隐藏会执行的默认构造器super(firstName, lastName, id); //因为少了父类自定义构造器初始化,所以会报错this.firstName =firstName;this.lastName =lastName;this.idNumber =id;this.testScores =scores;

}/** Method Name: calculate

* @return A character denoting the grade.*/

//Write your method here

public charcalculate(){int num =testScores.length;int s = 0;for(intscore : testScores) {

s= s +score;

}

s= s/num;char grade = '-';if(s>=90&&s<=100){

grade= 'O';

}else if(s>=80&&s<90){

grade= 'E';

}else if(s>=70&&s<80){

grade= 'A';

}else if(s>=55&&s<70){

grade= 'P';

}else if(s>=40&&s<55){

grade= 'D';

}else if(s<40){

grade= 'T';

}returngrade;

}

}classSolution {public static voidmain(String[] args) {

Scanner scan= newScanner(System.in);

String firstName=scan.next();

String lastName=scan.next();int id =scan.nextInt();int numScores =scan.nextInt();int[] testScores = new int[numScores];for(int i = 0; i < numScores; i++){

testScores[i]=scan.nextInt();

}

scan.close();

Student s= newStudent(firstName, lastName, id, testScores);

s.printPerson();

System.out.println("Grade: " +s.calculate());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值