假定根据学生的3门学位课程的分数决定其是否可以拿到学位,对于本科生,如果3门课程的平均分数超过60分即表示通过,而对于研究生,则需要平均分超过80分才能够通过。根据上述要求,请完成以下Java类的设计: (1)设计一个基类Student描述学生的共同特征。 (2)设计一个描述本科生的类Undergraduate,该类继承并扩展Student类。 (3)设计一个描述研究生的类Graduate,该类继承并扩展Student类。 (4)设计一个测试类StudentDemo,分别创建本科生和研究生这两个类的对象,并输出相关信息。 请附:代码图、运行结果图
时间: 2024-02-01 19:13:34 浏览: 28
以下是Java类的设计和测试类的代码实现:
```java
// 基类Student
class Student {
private String name; // 姓名
private String id; // 学号
private double[] scores; // 3门课程的分数
public Student(String name, String id, double[] scores) {
this.name = name;
this.id = id;
this.scores = scores;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public double[] getScores() {
return scores;
}
// 计算平均分数
public double getAverageScore() {
double sum = 0;
for (double score : scores) {
sum += score;
}
return sum / scores.length;
}
// 是否通过
public boolean isPass() {
return getAverageScore() >= 60;
}
// 输出学生信息
public void printInfo() {
System.out.println("姓名:" + name);
System.out.println("学号:" + id);
System.out.println("平均分数:" + getAverageScore());
System.out.println("是否通过:" + isPass());
}
}
// 本科生类Undergraduate,继承Student类
class Undergraduate extends Student {
public Undergraduate(String name, String id, double[] scores) {
super(name, id, scores);
}
}
// 研究生类Graduate,继承Student类
class Graduate extends Student {
public Graduate(String name, String id, double[] scores) {
super(name, id, scores);
}
// 重写是否通过方法
@Override
public boolean isPass() {
return getAverageScore() >= 80;
}
}
// 测试类StudentDemo
public class StudentDemo {
public static void main(String[] args) {
// 创建本科生对象
double[] scores1 = {70, 80, 90};
Undergraduate undergraduate = new Undergraduate("张三", "20210001", scores1);
undergraduate.printInfo();
// 创建研究生对象
double[] scores2 = {85, 90, 95};
Graduate graduate = new Graduate("李四", "20200001", scores2);
graduate.printInfo();
}
}
```
运行结果:
```
姓名:张三
学号:20210001
平均分数:80.0
是否通过:true
姓名:李四
学号:20200001
平均分数:90.0
是否通过:true
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)