用java语言设计程序,具备以下功能(要求用到继承): a) 学生分本科生(学号、姓名、班级)和研究生(学号、姓名、班级、导师)两种; b) 课程(编号、课程名、学分)分必修和选修两种; c) 创建4个学生信息(2个本科生,2个研究生) d) 创建4门课程信息(2门必修,2门选修) e) 自动选课部分:为每个学生自动选修所有必修课; f) 秘书手动选课部分:为每个同学选修1-2门选修课; g) 打印出每个学生的选课信息
时间: 2023-05-29 13:06:42 浏览: 107
// 学生类
class Student {
private String id; // 学号
private String name; // 姓名
private String className; // 班级
public Student(String id, String name, String className) {
this.id = id;
this.name = name;
this.className = className;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getClassName() {
return className;
}
public String toString() {
return "学号:" + id + ",姓名:" + name + ",班级:" + className;
}
}
// 本科生类
class Undergraduate extends Student {
public Undergraduate(String id, String name, String className) {
super(id, name, className);
}
}
// 研究生类
class Graduate extends Student {
private String tutor; // 导师
public Graduate(String id, String name, String className, String tutor) {
super(id, name, className);
this.tutor = tutor;
}
public String getTutor() {
return tutor;
}
public String toString() {
return super.toString() + ",导师:" + tutor;
}
}
// 课程类
class Course {
private String id; // 编号
private String name; // 名称
private int credit; // 学分
public Course(String id, String name, int credit) {
this.id = id;
this.name = name;
this.credit = credit;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getCredit() {
return credit;
}
public String toString() {
return "编号:" + id + ",名称:" + name + ",学分:" + credit;
}
}
// 必修课程类
class RequiredCourse extends Course {
public RequiredCourse(String id, String name, int credit) {
super(id, name, credit);
}
}
// 选修课程类
class ElectiveCourse extends Course {
public ElectiveCourse(String id, String name, int credit) {
super(id, name, credit);
}
}
// 班级类
class Class {
private List<Student> students = new ArrayList<>(); // 学生列表
private List<Course> courses = new ArrayList<>(); // 课程列表
private Map<Student, List<Course>> selectedCourses = new HashMap<>(); // 已选课程列表
public void addStudent(Student student) {
students.add(student);
}
public void addCourse(Course course) {
courses.add(course);
}
// 自动选课,为每个学生自动选修所有必修课
public void autoSelectCourses() {
for (Student student : students) {
List<Course> courses = new ArrayList<>();
for (Course course : this.courses) {
if (course instanceof RequiredCourse) { // 必修课程
courses.add(course);
}
}
selectedCourses.put(student, courses);
}
}
// 手动选课,为每个同学选修1-2门选修课
public void selectCourses() {
Scanner scanner = new Scanner(System.in);
for (Student student : students) {
List<Course> courses = selectedCourses.get(student);
if (courses == null) {
courses = new ArrayList<>();
selectedCourses.put(student, courses);
}
System.out.println("为" + student.getName() + "同学选修选修课程(最多2门):");
for (Course course : this.courses) {
if (course instanceof ElectiveCourse) { // 选修课程
System.out.println(course);
System.out.print("是否选修该课程?(Y/N)");
String choice = scanner.nextLine();
if ("Y".equalsIgnoreCase(choice)) {
courses.add(course);
if (courses.size() == 2) {
break;
}
}
}
}
}
}
// 打印每个学生的选课信息
public void printSelectedCourses() {
for (Student student : students) {
List<Course> courses = selectedCourses.get(student);
System.out.println(student);
if (courses != null) {
System.out.println("选修课程:");
for (Course course : courses) {
System.out.println(course);
}
}
System.out.println();
}
}
}
public class Main {
public static void main(String[] args) {
// 创建班级
Class clazz = new Class();
// 创建学生
Student s1 = new Undergraduate("101", "张三", "1班");
Student s2 = new Undergraduate("102", "李四", "1班");
Student s3 = new Graduate("201", "王五", "2班", "张三");
Student s4 = new Graduate("202", "赵六", "2班", "李四");
// 创建课程
Course c1 = new RequiredCourse("001", "高等数学", 4);
Course c2 = new RequiredCourse("002", "大学英语", 2);
Course c3 = new ElectiveCourse("101", "Java程序设计", 3);
Course c4 = new ElectiveCourse("102", "数据结构", 3);
// 添加学生和课程
clazz.addStudent(s1);
clazz.addStudent(s2);
clazz.addStudent(s3);
clazz.addStudent(s4);
clazz.addCourse(c1);
clazz.addCourse(c2);
clazz.addCourse(c3);
clazz.addCourse(c4);
// 自动选课
clazz.autoSelectCourses();
// 手动选课
clazz.selectCourses();
// 打印选课信息
clazz.printSelectedCourses();
}
}
阅读全文