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门课程成绩和平均成绩。 运行StudentDemo程序,实现添加同学、输入成绩、输出成绩、删除同学的功能测试。
时间: 2023-05-27 18:02:49 浏览: 217
==操作符与equals方法-尚硅谷_佟刚_Java类设计
1. 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 + "]";
}
}
2. 学生类Student的代码:
public class Student extends Person {
private int stuNum;
private String className;
private double engScore;
private double mathScore;
private double javaScore;
public Student() {}
public Student(String name, String gender, int age, int stuNum, String className, double engScore, double mathScore, double javaScore) {
super(name, gender, age);
this.stuNum = stuNum;
this.className = className;
this.engScore = engScore;
this.mathScore = mathScore;
this.javaScore = javaScore;
}
public int getStuNum() {
return stuNum;
}
public void setStuNum(int stuNum) {
this.stuNum = stuNum;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public double getEngScore() {
return engScore;
}
public void setEngScore(double engScore) {
this.engScore = engScore;
}
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;
}
public double getAverageScore() {
return (engScore + mathScore + javaScore) / 3.0;
}
@Override
public String toString() {
return "Student [name=" + getName() + ", gender=" + getGender() + ", age=" + getAge() + ", stuNum=" + stuNum + ", className="
+ className + ", engScore=" + engScore + ", mathScore=" + mathScore + ", javaScore=" + javaScore + ", averageScore="
+ getAverageScore() + "]";
}
}
3. StudentDemo测试类的代码:
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<Student>();
// 添加学生方法
public void addStudents() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入姓名:");
String name = sc.next();
System.out.print("请输入性别:");
String gender = sc.next();
System.out.print("请输入年龄:");
int age = sc.nextInt();
System.out.print("请输入学号:");
int stuNum = sc.nextInt();
System.out.print("请输入班级:");
String className = sc.next();
Student student = new Student(name, gender, age, stuNum, className, 0, 0, 0); // 初始成绩为0
students.add(student);
System.out.println("添加成功!");
}
// 删除学生方法
public void delStudents() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入要删除的学生的学号:");
int stuNum = sc.nextInt();
Iterator<Student> it = students.iterator();
while (it.hasNext()) {
Student s = it.next();
if (s.getStuNum() == stuNum) {
it.remove();
System.out.println("删除成功!");
return;
}
}
System.out.println("该学号不存在!");
}
// 输入成绩方法
public void inputScore() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入要输入成绩的学生的学号:");
int stuNum = sc.nextInt();
for (Student s : students) {
if (s.getStuNum() == stuNum) {
System.out.print("请输入英语成绩:");
double engScore = sc.nextDouble();
System.out.print("请输入数学成绩:");
double mathScore = sc.nextDouble();
System.out.print("请输入Java成绩:");
double javaScore = sc.nextDouble();
s.setEngScore(engScore);
s.setMathScore(mathScore);
s.setJavaScore(javaScore);
System.out.println("成绩输入成功!");
return;
}
}
System.out.println("该学号不存在!");
}
// 输出成绩方法
public void outScore() {
System.out.println("所有学生的信息如下:");
for (Student s : students) {
System.out.println(s);
}
}
public static void main(String[] args) {
StudentDemo demo = new StudentDemo();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入选择操作的序号:1、添加学生 2、删除学生 3、输入成绩 4、输出成绩 5、退出");
int operateNum = sc.nextInt();
switch (operateNum) {
case 1:
demo.addStudents();
break;
case 2:
demo.delStudents();
break;
case 3:
demo.inputScore();
break;
case 4:
demo.outScore();
break;
case 5:
System.out.println("程序已退出!");
return;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
阅读全文