1. 设计Person类, (1)包含姓名(String)、性别(String)和年龄(int)成员变量,以及相应的getter和setter方法。 (2) 至少实现一个构造方法 (3)重写toString方法,输出Person对象信息; 2. 以Person为父类,设计一个学生类Student, (1)增加学号(int)、班级(String)、英语成绩、数学成绩和java成绩 这几个成员变量,以相应的getter和setter方法。 (2) 至少实现一个构造方法 (3) 重写toString()方法,输出Student对象信息。 3. 设计一个测试类StudentDemo,包含成员变量List<Student> students; 在main()方法中,调用并实现: (1)添加学生方法addStudents,实现从键盘输入学生信息,创建学生对象,添加到students 。 (2)删除学生方法delStudents,根据从键盘输入的学号信息,从students 删除该学生对象。 (3)输入成绩方法inputScore,从键盘为不同学生输入英语、数学和java成绩。 (4)输出成绩方法outScore, 输出students中所有学生的基本信息、3门课程成绩和平均成绩。
时间: 2023-05-27 07:04:33 浏览: 68
Person类实现:
```
public class Person {
private String name;
private String gender;
private int age;
public Person(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
'}';
}
}
```
Student类实现:
```
public class Student extends Person {
private int id;
private String klass;
private double englishScore;
private double mathScore;
private double javaScore;
public Student(String name, String gender, int age, int id, String klass) {
super(name, gender, age);
this.id = id;
this.klass = klass;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getKlass() {
return klass;
}
public void setKlass(String klass) {
this.klass = klass;
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
public double getJavaScore() {
return javaScore;
}
public void setJavaScore(double javaScore) {
this.javaScore = javaScore;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", klass='" + klass + '\'' +
", englishScore=" + englishScore +
", mathScore=" + mathScore +
", javaScore=" + javaScore +
", name='" + getName() + '\'' +
", gender='" + getGender() + '\'' +
", age=" + getAge() +
'}';
}
}
```
StudentDemo类实现:
```
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class StudentDemo {
private List<Student> students;
public StudentDemo() {
students = new ArrayList<>();
}
public void addStudents() {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter the number of students you want to add: ");
int count = scanner.nextInt();
for (int i = 0; i < count; i++) {
System.out.println("Adding student #" + (i + 1));
System.out.print("Name: ");
String name = scanner.next();
System.out.print("Gender: ");
String gender = scanner.next();
System.out.print("Age: ");
int age = scanner.nextInt();
System.out.print("ID: ");
int id = scanner.nextInt();
System.out.print("Class: ");
String klass = scanner.next();
students.add(new Student(name, gender, age, id, klass));
}
}
public void delStudents() {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter the ID of the student you want to delete: ");
int id = scanner.nextInt();
Iterator<Student> iterator = students.iterator();
while (iterator.hasNext()) {
Student student = iterator.next();
if (student.getId() == id) {
iterator.remove();
System.out.println("Student with ID " + id + " was deleted.");
return;
}
}
System.out.println("Student with ID " + id + " was not found.");
}
public void inputScore() {
Scanner scanner = new Scanner(System.in);
for (Student student : students) {
System.out.println("Inputting scores for " + student.getName());
System.out.print("English Score: ");
student.setEnglishScore(scanner.nextDouble());
System.out.print("Math Score: ");
student.setMathScore(scanner.nextDouble());
System.out.print("Java Score: ");
student.setJavaScore(scanner.nextDouble());
}
}
public void outScore() {
System.out.println("Student Information");
System.out.println("===================");
for (Student student : students) {
System.out.println(student);
double average = (student.getEnglishScore() + student.getMathScore() + student.getJavaScore()) / 3;
System.out.println("English Score: " + student.getEnglishScore());
System.out.println("Math Score: " + student.getMathScore());
System.out.println("Java Score: " + student.getJavaScore());
System.out.println("Average Score: " + average);
System.out.println();
}
}
public static void main(String[] args) {
StudentDemo demo = new StudentDemo();
demo.addStudents();
demo.delStudents();
demo.inputScore();
demo.outScore();
}
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)