设计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-26 14:04:07 浏览: 109
Person类:
```
public class Person {
private String name;
private String gender;
private int age;
public Person() {
}
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 clazz;
private double englishScore;
private double mathScore;
private double javaScore;
public Student() {
}
public Student(int id, String clazz, double englishScore, double mathScore, double javaScore) {
this.id = id;
this.clazz = clazz;
this.englishScore = englishScore;
this.mathScore = mathScore;
this.javaScore = javaScore;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
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 +
", clazz='" + clazz + '\'' +
", englishScore=" + englishScore +
", mathScore=" + mathScore +
", javaScore=" + javaScore +
"} " + super.toString();
}
}
```
StudentDemo类:
```
import java.util.ArrayList;
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.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生性别:");
String gender = scanner.nextLine();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
System.out.println("请输入学生学号:");
int id = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入学生班级:");
String clazz = scanner.nextLine();
System.out.println("请输入学生英语成绩:");
double englishScore = scanner.nextDouble();
System.out.println("请输入学生数学成绩:");
double mathScore = scanner.nextDouble();
System.out.println("请输入学生Java成绩:");
double javaScore = scanner.nextDouble();
Student student = new Student(id, clazz, englishScore, mathScore, javaScore);
student.setName(name);
student.setGender(gender);
student.setAge(age);
students.add(student);
}
public void delStudents() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的学生学号:");
int id = scanner.nextInt();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() == id) {
students.remove(i);
System.out.println("删除成功!");
break;
}
}
}
public void inputScore() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生学号:");
int id = scanner.nextInt();
for (Student student : students) {
if (student.getId() == id) {
System.out.println("请输入学生英语成绩:");
double englishScore = scanner.nextDouble();
student.setEnglishScore(englishScore);
System.out.println("请输入学生数学成绩:");
double mathScore = scanner.nextDouble();
student.setMathScore(mathScore);
System.out.println("请输入学生Java成绩:");
double javaScore = scanner.nextDouble();
student.setJavaScore(javaScore);
System.out.println("成绩输入成功!");
return;
}
}
System.out.println("找不到该学生!");
}
public void outScore() {
for (Student student : students) {
System.out.println(student.getName() + ":" + student.getEnglishScore() + " " + student.getMathScore() + " " + student.getJavaScore() + " 平均分:" + (student.getEnglishScore() + student.getMathScore() + student.getJavaScore()) / 3);
}
}
public static void main(String[] args) {
StudentDemo studentDemo = new StudentDemo();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\n请选择操作:1.添加学生 2.删除学生 3.输入成绩 4.输出成绩 5.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
studentDemo.addStudents();
break;
case 2:
studentDemo.delStudents();
break;
case 3:
studentDemo.inputScore();
break;
case 4:
studentDemo.outScore();
break;
case 5:
System.exit(0);
default:
System.out.println("请重新选择!");
}
}
}
}
```
阅读全文