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-26 17:03:22 浏览: 226
Person类:
```java
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 "姓名:" + name + ",性别:" + gender + ",年龄:" + age;
}
}
```
Student类:
```java
public class Student extends Person {
private int studentNumber;
private String className;
private int englishScore;
private int mathScore;
private int javaScore;
public Student() {}
public Student(String name, String gender, int age, int studentNumber, String className, int englishScore, int mathScore, int javaScore) {
super(name, gender, age);
this.studentNumber = studentNumber;
this.className = className;
this.englishScore = englishScore;
this.mathScore = mathScore;
this.javaScore = javaScore;
}
public int getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(int studentNumber) {
this.studentNumber = studentNumber;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public int getEnglishScore() {
return englishScore;
}
public void setEnglishScore(int englishScore) {
this.englishScore = englishScore;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}
public int getJavaScore() {
return javaScore;
}
public void setJavaScore(int javaScore) {
this.javaScore = javaScore;
}
@Override
public String toString() {
return super.toString() + ",学号:" + studentNumber + ",班级:" + className + ",英语成绩:" + englishScore + ",数学成绩:" + mathScore + ",java成绩:" + javaScore;
}
}
```
StudentDemo类:
```java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class StudentDemo {
private List<Student> students = new ArrayList<>();
public void addStudents() {
Scanner input = new Scanner(System.in);
System.out.println("请输入学生的基本信息:");
System.out.print("姓名:");
String name = input.next();
System.out.print("性别:");
String gender = input.next();
System.out.print("年龄:");
int age = input.nextInt();
System.out.print("学号:");
int studentNumber = input.nextInt();
System.out.print("班级:");
String className = input.next();
Student student = new Student(name, gender, age, studentNumber, className, 0, 0, 0);
students.add(student);
System.out.println("添加成功!学生信息为:");
System.out.println(student);
}
public void delStudents() {
Scanner input = new Scanner(System.in);
System.out.print("请输入要删除学生的学号:");
int studentNumber = input.nextInt();
Iterator<Student> it = students.iterator();
while (it.hasNext()) {
Student student = it.next();
if (student.getStudentNumber() == studentNumber) {
it.remove();
System.out.println("删除成功!删除学生信息为:");
System.out.println(student);
return;
}
}
System.out.println("删除失败!学号" + studentNumber + "的学生不存在!");
}
public void inputScore() {
Scanner input = new Scanner(System.in);
for (Student student : students) {
System.out.println("为学生" + student.getName() + "输入英语成绩:");
int englishScore = input.nextInt();
student.setEnglishScore(englishScore);
System.out.println("为学生" + student.getName() + "输入数学成绩:");
int mathScore = input.nextInt();
student.setMathScore(mathScore);
System.out.println("为学生" + student.getName() + "输入java成绩:");
int javaScore = input.nextInt();
student.setJavaScore(javaScore);
}
}
public void outScore() {
for (Student student : students) {
int averageScore = (student.getEnglishScore() + student.getMathScore() + student.getJavaScore()) / 3;
System.out.println(student.getName() + " " + student.getStudentNumber() + " " + student.getClassName() + " " + student.getEnglishScore() + " " + student.getMathScore() + " " + student.getJavaScore() + " " + averageScore);
}
}
public static void main(String[] args) {
StudentDemo test = new StudentDemo();
while (true) {
System.out.println("请选择要进行的操作:");
System.out.println("1. 添加学生");
System.out.println("2. 删除学生");
System.out.println("3. 输入成绩");
System.out.println("4. 输出成绩");
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
switch (choice) {
case 1:
test.addStudents();
break;
case 2:
test.delStudents();
break;
case 3:
test.inputScore();
break;
case 4:
test.outScore();
break;
default:
System.out.println("无效的操作!");
break;
}
}
}
}
```
阅读全文