一、要求
设计一个学生选课信息管理系统,从屏幕读入学生、课程信息,执行学生选课操作,并显示选课结果。要求如下:
(1)设计一个学生类Student,包括:
学号stuID、姓名stuName、学生对象的数量stuNum三个数据域;
一个无参构造方法,创建默认的学生,构造方法中输出“学生类无参构造方法”;
一个有参构造方法,创建指定学号stuID、姓名stuName的学生,构造方法中输出“学生类有参构造方法”;
所有数据域的访问器方法;
两个修改器方法,可以修改学号stuID、姓名stuName的值。
(2)设计一个课程类Course,包括:
课程编号cID、课程名cName、课程对象的数量cNum三个数据域;
一个无参构造方法,创建默认的课程,构造方法中输出“课程类无参构造方法”;
一个有参构造方法,创建指定课程编号cID、课程名cName的课程,构造方法中输出“课程类有参构造方法”;
所有数据域的访问器方法;
两个修改器方法,可以修改课程编号cID、课程名cName的值。
(3)设计一个学生选课类Schedule,包括:
学生列表stuList、课程列表cList、学生选课总数schNum三个数据域,两个列表的默认长度任意设定;
一个无参构造方法,创建默认的学生选课对象;
一个学生选课方法 addCourse(Student stu,Course course),实现学生stu选择课程course操作;
一个显示学生选课详情方法 displayCourse(),显示所有学生选课情况。
(4)测试类Main,要求:
情况1 test1:
① 使用无参构造方法建立二个学生对象;
② 查看学生对象总数
情况2 test2:
① 使用无参构造方法建立三门课程对象;
② 查看课程对象总数
情况3 test3:
① 使用有参构造方法建立一个学生对象;
② 使用无参构造方法建立二门课程对象;
③ 使用学生选课类进行课程选择,为学生选择这两门课程
④ 查看学生选课总数
⑤ 查看学生选课详情
情况4 test4:
① 使用有参构造方法建立三个学生对象;
② 使用有参构造方法建立四门课程;
③ 使用学生选课类进行课程选择
第一个学生选择课程2、课程3;
第二个学生选择课程1;
第三个学生选择课程1、课程2、课程4。
④ 查看选课信息
查看学生对象总数
查看课程对象总数
查看学生选课总数
查看学生选课详情
(5)程序框架示例:
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int type = sc.nextInt();
switch(type){
case 1: test1(sc);break;
case 2: test2(sc);break;
case 3: test3(sc);break;
case 4: test4(sc);
}
}
//test方法为(4)中情况,见上述说明
public static void test1(Scanner sc) {
}
public static void test2(Scanner sc) {
}
public static void test3(Scanner sc) {
}
public static void test4(Scanner sc) {
}
}
// 学生类Student
class Student{
}
// 课程类Course
class Course{
}
// 学生选课类Schedule
class Schedule{
}
输入格式:
第一行数字代表测试情况,这里为测试情况3,见上述(4)中说明,为选课测试,第二行为学生信息,后面两行为课程信息,每行数据间使用空格分隔,如下所示:
3
01 Tom
c01 数据结构
c02 软件工程
其他测试情况格式设置相同,具体情况查看要求中的说明和输入样例。
输出格式:
每组输出占一行,每行如果有多个数据采用制表符分隔,如下所示:
学生类有参构造方法
课程类无参构造方法
课程类无参构造方法
学生选课的总数为:2
学生选课情况如下:
01 Tom c01 数据结构
01 Tom c02 软件工程
二、代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int type = sc.nextInt();
switch(type){
case 1: test1(sc);break;
case 2: test2(sc);break;
case 3: test3(sc);break;
case 4: test4(sc);
}
}
//test方法为(4)中情况,见上述说明
public static void test1(Scanner sc) {
Student1 stu1 = new Student1();
Student1 stu2 = new Student1();
stu1.stuID = sc.next();
stu1.stuName = sc.next();
stu2.stuID = sc.next();
stu2.stuName = sc.next();
System.out.println("学生总数为:"+Student1.stuNum);
}
public static void test2(Scanner sc) {
Course c1 = new Course();
Course c2 = new Course();
Course c3 = new Course();
c1.cID = sc.next();
c1.cName = sc.next();
c2.cID = sc.next();
c2.cName = sc.next();
c3.cID = sc.next();
c3.cName = sc.next();
System.out.println("课程总数为:"+Course.cNum);
}
public static void test3(Scanner sc) {
Student1 stu3 = new Student1(sc.next(), sc.next());
Course c4 = new Course();
Course c5 = new Course();
c4.cID = sc.next();
c4.cName = sc.next();
c5.cID = sc.next();
c5.cName = sc.next();
Schedule sch = new Schedule();
sch.addCourse(stu3, c4);
sch.addCourse(stu3, c5);
sch.displayCourse();
}
public static void test4(Scanner sc) {
Student1 stu1 = new Student1(sc.next(), sc.next());
Student1 stu2 = new Student1(sc.next(), sc.next());
Student1 stu3 = new Student1(sc.next(), sc.next());
Course C1 = new Course(sc.next(), sc.next());
Course C2 = new Course(sc.next(), sc.next());
Course C3 = new Course(sc.next(), sc.next());
Course C4 = new Course(sc.next(), sc.next());
Schedule sch1 = new Schedule();
sch1.addCourse(stu1, C2);
sch1.addCourse(stu1, C3);
sch1.addCourse(stu2, C1);
sch1.addCourse(stu3, C1);
sch1.addCourse(stu3, C2);
sch1.addCourse(stu3, C4);
System.out.println("学生总数为:"+Student1.stuNum);
System.out.println("课程总数为:"+Course.cNum);
sch1.displayCourse();
}
}
// 学生类Student
class Student1{
String stuID;
String stuName;
static int stuNum;
public Student1() {
stuNum++;
System.out.println("学生类无参构造方法");
}
public Student1(String stuID, String stuName) {
stuNum++;
System.out.println("学生类有参构造方法");
this.stuID = stuID;
this.stuName = stuName;
}
public String getStuID() {
return stuID;
}
public void setStuID(String stuID) {
this.stuID = stuID;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuNum() {
return stuNum;
}
}
// 课程类Course
class Course{
String cID;
String cName;
static int cNum;
public Course() {
cNum++;
System.out.println("课程类无参构造方法");
}
public Course(String cID, String cName) {
cNum++;
System.out.println("课程类有参构造方法");
this.cID = cID;
this.cName = cName;
}
public String getcID() {
return cID;
}
public void setcID(String cID) {
this.cID = cID;
}
public String getcName() {
return cName;
}
public void setcName(String cName) {
this.cName = cName;
}
public int getcNum() {
return cNum;
}
}
// 学生选课类Schedule
class Schedule{
Student1[] stuList = new Student1[10];
Course[] cList = new Course[10];
static int schNum;
public Schedule() {
}
public void addCourse(Student1 stu,Course course){
stuList[schNum] = stu;
// cList[][stu.num++] = course;
cList[schNum] = course;
schNum++;
}
public void displayCourse(){
System.out.println("学生选课的总数为:"+schNum);
System.out.println("学生选课情况如下:");
for (int i = 0; i < schNum; i++) {
if(i<schNum-1)
System.out.println(stuList[i].stuID+"\t"+stuList[i].stuName+"\t"+cList[i].cID+"\t"+cList[i].cName);
else
System.out.print(stuList[i].stuID+"\t"+stuList[i].stuName+"\t"+cList[i].cID+"\t"+cList[i].cName);
}
}
}
三、测试数据
输入样例1:
在这里给出一组输入。例如:
1
01 Tom
02 Anne
输出样例1:
在这里给出相应的输出。例如:
学生类无参构造方法
学生类无参构造方法
学生总数为:2
输入样例2:
在这里给出一组输入。例如:
2
c01 数据结构
c02 软件工程
c03 Java基础
输出样例2:
在这里给出相应的输出。例如:
课程类无参构造方法
课程类无参构造方法
课程类无参构造方法
课程总数为:3
输入样例3:
在这里给出一组输入。例如:
3
01 Tom
c01 数据结构
c02 软件工程
输出样例3:
在这里给出相应的输出。例如:
学生类有参构造方法
课程类无参构造方法
课程类无参构造方法
学生选课的总数为:2
学生选课情况如下:
01 Tom c01 数据结构
01 Tom c02 软件工程
输入样例4:
在这里给出一组输入。例如:
4
01 Tom
02 Anne
03 Jame
c01 数据结构
c02 软件工程
c03 Java基础
c04 C语言
输出样例4:
在这里给出相应的输出。例如:
学生类有参构造方法
学生类有参构造方法
学生类有参构造方法
课程类有参构造方法
课程类有参构造方法
课程类有参构造方法
课程类有参构造方法
学生总数为:3
课程总数为:4
学生选课的总数为:6
学生选课情况如下:
01 Tom c02 软件工程
01 Tom c03 Java基础
02 Anne c01 数据结构
03 Jame c01 数据结构
03 Jame c02 软件工程
03 Jame c04 C语言