数据库实验(学生选课系统)
Posted 自学Java小白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库实验(学生选课系统)相关的知识,希望对你有一定的参考价值。
数据库实验(学生选课系统)
文章目录
一、使用管理工具创建数据库
1)使用 SQL Server Management Studio 建立学生选课数据库(XSXK)。
2)在 SQL Server Management Studio 中查看学生选课数据库的属性。
3)根据学生选课数据库物理结构设计,定义基本表、索引,实现相应的约束条件。
二、使用T-SQL创建数据库
(1) student(学生表)
字段名称 | 数据类型 | 长度大小 | 允许空 | 备注 |
---|---|---|---|---|
sno | char | 10 | 学号 | |
sname | char | 8 | 姓名 | |
ssex | char | 2 | 性别 | |
sage | smallint | √ | 年龄 | |
sdept | char | 30 | √ | 所在院系 |
stel | char | 13 | √ | 联系电话 |
主键:sno
索引:sname(升序)
check 约束:年龄大于 18, 性别的取值只能为男或女
default 约束:性别默认为男
更改学生表的结构,取消姓名不允许为空的约束
(2) Course (课程表)
字段名称 | 数据类型 | 长度大小 | 允许空 | 说明 |
---|---|---|---|---|
cno | char | 10 | 课程编号 | |
cname | char | 16 | 课程名称 | |
ccredit | smallint | 学分 | ||
cpno | char | 10 | √ | 先行课 |
主键:cno
索引: cno (升序)+ccredit(降序)
更改课程表的结构,增加属性列教师 ctech(类型是 char ,长度 20)
(3) SC(选课表)
字段名称 | 数据类型 | 长度大小 | 允许空 | 备注 |
---|---|---|---|---|
sno | char | 10 | 学号 | |
cno | char | 10 | 课程编号 | |
grade | smallint | √ | 成绩 |
主键:sno+cno
(4) 各个表的参照完整性约束
FK_SC_ student
主 键 表 :student 外 键 表 :SC 主 键 :sno 外 键 :sno
FK_SC_ course
主 键 表 :course 外 键 表 :SC 主 键 :cno 外 键 :cno
3、SQL Server Management Studio 管理工具,完成图书借阅数据库的录入、数据修改或删除操作。
1) 要求各表录入 10 条以上记录。
2) 修改某位读者的还书日期。
3) 删除指定读者的借还记录。
三、SQL语句
1.简单查询(一)
(1) 对于每个学生,求学生的选课门数和平均成绩,并把结果存入学生选课数据库中。
(2) 将数据库原理与应用课程的学分修改为 4.
(3) 将姓名为“王华”的学生选修数据库原理及应用课程的成绩增加 5 分。
(4) 删除选课表中成绩低于 40 分的记录。
(5) 删除学号为 140001 的学生记录,并讨论该删除操作所受到的约束。
(6) 删除所有选修课程“JAVA”的选课记录。
(7) 将管理学院全体学生的成绩置为空。
(8) 删除学生李萍的所有选课记录。
2、简单查询 (二)
在学生选课数据库中实现其数据查询操作。
(1)查询数计学院学生的学号和姓名。
(2)查询选修了课程的学生学号。
(3) 查询选修 C1 课程的学生学号和成绩,结果按成绩降序排列,如果成绩相同按学号升序排序。
(4)查询选修 C1 课程,成绩在 80~90 之间的学生学号和成绩,并将成绩乘以 0.8 输出。
(5)查询生工学院或数计学院系姓张的学生的信息。
(6)查询缺少了成绩的学生的学号和课程号。
3.连接查询
在学生选课库中实现其数据连接查询操作。
(1)查询学生的学号、姓名、选修的课程名称及成绩。
(2)查询数计学院学生选修的课程学分大于 2 的课程详细信息。
(3) 查询所有学生的信息以及他(她)所选课的学生学号和成绩(要求查询结果也显示出没有选修课程的学生信息)。
(4)查询选修课程号为 C1 且成绩在 90 分以上的学生学号、姓名及成绩。
(5) 查询每一门课的间接先行课(即先行课的先行课)。言的查询语句的理解。
4.嵌套查询
(1)查询选修了’c语言’课程的学生学号和姓名
(2)查询比唐伯虎年龄大的学生学号和姓名
(3)查询数据结构课程的成绩低于唐伯虎的学生学号和成绩
(4)查询其他学院中比数计学院学生年龄都小的学生
(5)查询选修了操作系统课程的学生姓名
(6)查询没有选修操作系统课程的学生姓名
(7)查询选修了全部课程的学生姓名
(8)查询至少选修了学生为张飞的学生所选修的全部课程的学生学号和姓名
(9)查询既选修了数据库原理又选修了操作系统课程的学生姓名
(10)查询既选修了数据库原理或选修了操作系统课程的学生学号
(11)查询既选修了数据库原理而没有选修操作系统课程的学生学号
(12)查询全是男同学选修的课程号
5.组合查询和统计查询
(1)使用集合运算查询既选修了数据结构课程又选修了数据库原理课程的学生姓名。
(2)使用集合运算查询选修了数据结构课程或选修了数据库原理课程的学生学号。
(3)使用集合运算查询选修了数据结构课程而没有选修了数据库原理课程的学生
(4)统计选修了课程的学生人数。
(5)查询选修成绩合格的课程超过4门以上学生的学生学号、总成绩。
(6)统计各院系的学生人数。
(7)统计各年龄的学生人数。
(8)统计每个学生的选修课程数目和平均成绩。
(9)查询每门课程的详细信息及选课人数。
四、代码:
create table student
(
sno char(10) not null comment '学号',
sname char(8) not null comment '姓名',
ssex char(2) default ' 男' not null check ( ssex = '男' or ssex = '女') comment '性别',
sdept char(30) null comment '所在院系',
sage smallint null check ( sage > 18 ) comment '年龄',
stel char(13) null comment '联系电话',
constraint student_pk
primary key (sno)
)
comment '学生';
create index student_sname_index
on student (sname);
create table Course
(
cno char(10) not null comment '课程编号',
cname char(16) not null comment '课程名称',
ccredit smallint not null comment '学分',
cpno char(10) null comment '先行课',
constraint Course_pk
primary key (cno)
)
comment '课程表';
create index Course_cno_ccredit_index
on Course (cno asc, ccredit desc);
create table SC
(
sno char(10) not null comment '学号',
grade smallint null comment '成绩',
cno char(10) not null comment '课程编号',
constraint SC_pk
primary key (sno, cno)
)
comment '选课';
create index student_sname_index
on student (sname);
create table Course
(
cno char(10) not null,
cname char(16) not null,
ccredit smallint not null,
cpno char(10) null,
constraint Course_pk
primary key (cno)
);
create index Course_cno_ccredit_index on Course (cno asc, ccredit desc);
create table SC
(
sno char(10) not null,
grade smallint null,
cno char(10) not null,
constraint SC_pk
primary key (sno, cno)
);
alter table SC
add constraint FK_SC_student foreign key (sno) references student (sno);
alter table SC
add constraint FK_SC_Course foreign key (cno) references Course (cno);
insert into student
values ('2001124001', '张飞', '男', '管理学院', 23, '1464312');
insert into student
values ('2001124002', '郭旭', '男', '管理学院', 89, '1145114');
insert into student
values ('2001124004', '周波', '男', '数计学院', 23, '4654654');
insert into student
values ('2001124005', '张柏芝', '女', '数计学院', 30, '154666');
insert into student
values ('2001124006', '风清扬', '男', '管理学院', 28, '14645463');
insert into student
values ('2001124007', '唐伯虎', '男', '材料学院', 26, '1454643');
insert into student
values ('2001124008', '于占鳌', '男', '电气学院', 25, '7842289');
insert into student
values ('2001124009', '九儿', '女', '电气学院', 19, '774512');
insert into student
values ('2001124010', '李茂贞', '女', '电气学院', 29, '6659873');
insert into student
values ('2001124011', '紫霞', '女', '材料学院', 19, '4654562');
INSERT INTO Course
VALUES ('001', 'java', '4', '003')
, ('002', 'c语言', '4', '003')
, ('003', '数据结构', '4', NULL)
, ('004', '数据库原理', '3', '002')
, ('005', '计算机组成原理', '2', NULL)
, ('006', '操作系统', '2', '005')
, ('007', '计算机网络原理', '3', NULL)
, ('008', '高等数学', '5', NULL)
, ('009', '大学英语一', '2', NULL)
, ('010', '大学物理三', '3', NULL)
;
insert into SC
values ('2001124001', null, '001'),
('2001124001', 82, '002'),
('2001124001', 78, '003'),
('2001124001', 96, '004'),
('2001124001', 76, '005'),
('2001124001', 39, '006'),
('2001124001', 88, '007'),
('2001124001', null, '008'),
('2001124001', 82, '009'),
('2001124001', 69, '010');
insert into SC
values ('2001124002', 98, '001'),
('2001124002', 92, '002'),
('2001124002', 78, '003'),
('2001124002', 96, '004'),
('2001124002', null, '005'),
('2001124002', 88, '006'),
('2001124002', 88, '007'),
('2001124002', 81, '008'),
('2001124002', 82, '009'),
('2001124002', 79, '010');
insert into SC
values ('2001124005', 66, '001'),
('2001124005', 72, '002'),
('2001124005', 88, '003'),
('2001124005', 76, '004'),
('2001124005', 76, '005'),
('2001124005', 68, '006'),
('2001124005', 68, '007'),
('2001124005', 86, '008'),
('2001124005', 80, '009'),
('2001124005', null, '010');
insert into SC
values ('2001124004', 46, '001'),
('2001124004', null, '002'),
('2001124004', null, '003'),
('2001124004', 87, '004'),
('2001124004', null, '005'),
('2001124004', 58, '006'),
('2001124004', 68, '007'),
('2001124004', 61, '008'),
('2001124004', 72, '009'),
('2001124004', 59, '010');
insert into SC
values ('2001124006', 49, '001'),
('2001124006', 52, '002'),
('2001124006', 88, '003'),
('2001124006', 87, '004'),
('2001124006', 66, '005'),
('2001124006', 88, '006'),
('2001124006', 68, '007'),
('2001124006', 61, '008'),
('2001124006', 72, '009'),
('2001124006', null, '010');
insert into SC
values ('2001124007', 86, '001'),
('2001124007', 42, '002'),
('2001124007', 69, '003'),
('2001124007', 87, '004'),
('2001124007', 65, '005'),
('2001124007', null, '006'),
('2001124007', 58, '007'),
('2001124007', 61, '008'),
('2001124007', 72, '009'),
('2001124007', 69, '010');
insert into SC
values ('2001124008', 87, '001'),
('2001124008', 72, '002'),
('2001124008', 68, '003'),
('2001124008', 87, '004'),
('2001124008', 96, '005'),
('2001124008', 58, '006'),
('2001124008', 68, '007'),
('2001124008', 61, '008'),
('2001124008', 72, '009'),
('2001124008', 59, '010');
insert into SC
values ('2001124009', 86, '001'),
('2001124009', 92, '002'),
('2001124009', 68, '003'),
('2001124009', 87, '004'),
('2001124009', 66, '005'),
('2001124009', 58, '006'),
('2001124009', 68, '007'),
('2001124009', 61, '008'),
('2001124009', 72, '009'),
('2001124009', 79, '010');
insert into SC
values ('2001124010', 96, '001'),
('2001124010', null, '002'),
('2001124010', 88, '003'),
('2001124010', 87, '004'),
('2001124010', 86, '005'),
('2001124010', null, '006'),
('2001124010', null, '007'),
('2001124010', 79, '008'),
('2001124010', 72, '009'),
('2001124010', null, '010');
insert into SC
values ('2001124011', 86, '001'),
('2001124011', 82, '002'),
('2001124011', 58, '003'),
('2001124011', 87, '004'),
('2001124011', 66, '005'),
('2001124011', 98, '006'),
('2001124011', 78, '007'),
('2001124011', 81, '008'),
('2001124011', null, '009'),
('2001124011', 79, '010');
alter table student
add 平均成绩 double;
alter table student
add 选课门数 int;
# 录入平均成绩
update student
set 平均成绩=(select round(avg(grade), 1) from SC where sno = '2001124001')
where sno = '2001124001';
update student
set 平均成绩=(select round(avg(grade), 1) from SC where sno = '2001124002')
where sno = '2001124002';
update student
set 平均成绩=(select round(avg(grade), 1) from SC where sno = '2001124004')
where sno = '2001124004';
update student
set 平均成绩=(select round(avg(grade), 1) from SC where sno = '2001124005')
where sno = '2001124005';
update student
set 平均成绩=(select round(avg(grade), 1) from SC where sno = '2001124006')
where sno = '2001124006';
update student
set 平均成绩=(select round(avg(grade), 1) from SC where sno = '2001124007')
where sno = '2001124007';
update student
set 平均成绩=(select round(avg(grade), 1) from SC where sno = '2001124008')
where sno = '2001124008';
update student
set 平均成绩=(select round(avg(grade), 1) from SC where sno = '2001124009')
where sno = '2001124009';
update student
set 平均成绩=(select round(avg(grade), 1) from SC where sno = '2001124010')
where sno = '2001124010';
update student
set 平均成绩=(select round(avg(grade), 1) from SC where sno = '2001124011')
where sno = '2001124011';
# 查询一下所有的平均成绩可以对比一下是否录入正确
select SC.sno, AVG(grade) as 平均成绩
from SC
group by SC.sno;
# 录入选课门数
update student
set 选课门数 = (select count(grade) from SC where SC.sno = '2001124001')
where sno = '2001124001';
update student
set 选课门数 = (select count(grade) from SC where SC.sno = '2001124002')
where sno = '2001124002';
update student
set 选课门数 = (select count(grade) from SC where SC.sno = '2001124004')
where sno = '2001124004';
update student
set 选课门数 = (select count(grade) from SC where SC.sno = '2001124005')
where sno = '2001124005';
update student
set 选课门数 = (select count(grade) from SC where SC.sno = '2001124006')
where sno = '2001124006';
update student
set 选课门数 = (select count(grade) from SC where SC.sno = '2001124007')
where sno = '2001124007';
update student
set 选课门数 = (select count(grade) from SC where SC.sno = '2001124008')
where sno = '2001124008';
update student
set 选课门数 = (select count(grade) from SC where SC.sno = '2001124009')
where sno = '2001124009';
update student
set 选课门数 = (select count(grade) from SC where SC.sno = '2001124010')
where sno = '2001124010';
update student
set 选课门数 = (select count(grade) from SC where SC.sno = '2001124011')
where sno = '2001124011';
# 默认情况下null值不参与计算,所以不需要过滤
select count(grade)
from SC
where SC.sno = '2001124001';
# 修改'数据库原理'的学分为4
update course
set ccredit=4
where cname = '数据库原理';
# 将姓名为唐伯虎的学生选修数据库原理课程的成绩加5
update sc
set grade =grade + 5
where sno = (select student.sno from student where sname = '唐伯虎')
and cno = (select cno from course where cname = '数据库原理');
# 删除选课表中成绩低于40分的记录
delete
from sc
where grade < 40;
# 删除学号为2001124011的学生记录(删除单行数据)
delete
from sc
where sno = 2001124011;
# 删除所有选修java课程的选课记录
delete
from sc
where cno = (select cno from course where cname = 'java');
# 将管理学院全体学生成绩置为空
select sno
from student
where sdept = '管理学院';
#先查询是管理学院学生的学号
/*2001124001
2001124002
2001124006*/
update sc
set grade=null
where sno = 2001124001;
update sc
set grade=null
where sno = 2001124002;
update sc
set grade=null
where sno = 2001124006;
# 删除学生郭旭的所有选课记录
delete
from sc
where sno = (select sno from student where sname = '郭旭');
# 实验二
# (一)
# 1.查询数据学院学生的学号和姓名
select sno as 学号, sname as 姓名
from student
where sdept = '数计学院';
# 2.查询选修了课程的学生学号
select sno as 学号
from student
where student.选课门数 > 0;
# 3.查询选修高等数学的课程的学生学号和成绩,结果按成绩降序排列,如果成绩相同按学号升序排列
select sno as 学号, grade as 成绩
from sc
where cno = (select cno from course where cname = '高等数学')
order by grade desc, sno asc;
# 4.查询选修高等数学课程,成绩在80~90之间的学生学号和成绩,并将成绩乘以0.8输出
select sno as 学号, grade * 0.8 as 成绩
from sc
where cno = (select cno from course where cname = '高等数学')
and grade between 80 and 90;
# 5.查询管理学院或数计学院姓张的学生的信息
select *
from student
where (sdept in ('管理学院', '数计学院'))
and (sname like '张%');
# 6.查询少了成绩的学生的学号和课程号
select sno 学号, cno 课程号, grade
from sc
where grade is null;
# (二)
# 1.查询学生的学号,姓名,选修的课程名称及成绩
select distinct sc.sno 学号,
student.sname 姓名,
Course.cname 选修的课程,
sc.grade 成绩
from sc
left join student on sc.sno = student.sno
left join course on sc.cno = Course.cno;
# 2.查询数计学院学生选修的课程学分大于2的课程的详细信息
select distinct Course.*
from sc
left join student on sc.sno = student.sno
left join course on sc.cno = Course.cno
where student.sdept = '数计学院'
and Course.ccredit > 2;
# 3.查询所有学生信息以及他所选课的课程号和成绩
# (1)
select distinct student.*,
Course.*,
sc.grade
from sc
left join student on sc.sno = student.sno
left join course on sc.cno = Course.cno;
# (2)要求查询结果也显示即使没有选课的学生的信息
select distinct student.*,
Course.*,
sc.grade
from student
left join sc on sc.sno = student.sno
left join course on sc.cno = Course.cno;
# 4.查询选修课课程号为002且成绩在90分以上的学生学号,姓名和成绩
select distinct sc.sno 学号,
student.sname 姓名,
sc.grade 成绩
from sc
left join student on sc.sno = student.sno
left join course on sc.cno = Course.cno
where SC.cno = 002
and sc.grade > 90;
# 5.查询每一门课的间接先行课
select c4.*,
c5.cpno 间接先行课
from course c4
left join (select distinct c1.cno,
c2.cpno
from course c1,
course c2,
course c3
where c1.cpno = c2.cno
and c2.cpno = c3.cno) c5 on c4.cno = c5.cno;
# 实验三
# 1.嵌套查询
# (1)查询选修了'c语言'课程的学生学号和姓名
select sc.sno, (select sname from student where sno = sc.sno) 姓名
from sc
where sc.cno = (select course.cno from course where cname = 'c语言');
# (2)查询比唐伯虎年龄大的学生学号和姓名
select s.sno 学号, s.sname 姓名
from student s
where s.sage > (select sage from student where sname = '唐伯虎');
# (3)查询数据结构课程的成绩低于唐伯虎的学生学号和成绩
select *
from student s
where sno in (select SC.sno
from sc
where grade < (select grade
from sc,
student s,
course c
where sc.sno = s.sno
and c.cno = sc.cno
and s.sname = '唐伯虎'
and c.cname = '数据结构'));
# (4)查询其他学院中比数计学院学生年龄都小的学生
select *
from student
where sage < all (select sage from student where sdept = '数计学院');
# (5)查询选修了操作系统课程的学生姓名
select sname
from student
where sno in (select sno from sc where cno = (select cno from course where cname = '操作系统'));
# (6)查询没有选修操作系统课程的学生姓名
select sname
from student
where sno not in (select sno from sc where cno = (select cno from course where cname = '操作系统'));
# (7)查询选修了全部课程的学生姓名
select sname
from student
where sno in (select SC.sno from sc group by sno having count(*) = 10);
# (8)查询至少选修了学生为张飞的学生所选修的全部课程的学生学号和姓名
select sno, sname
from student
where sno in (select distinct sno
from SC c
where not exists(select *
from SC b
where b.Sno = (select sno
from student
where sname = '张飞')
and not exists(select *
from SC a
where a.Sno = c.Sno
and a.Cno = b.Cno)));
# (9)查询既选修了数据库原理又选修了操作系统课程的学生姓名
select sname
from student
where sno in (select a.sno
from (select sno from sc where cno = (select cno from course where cname = '操作系统')) a
inner join (select sno from sc where cno = (select cno from course where cname = '数据库原理')) b
on a.sno = b.sno);
# (10)查询既选修了数据库原理或选修了操作系统课程的学生学号
select sno
from sc
where cno = (select cno from course where cname = '操作系统')
union
select sno
from sc
where cno = (select cno from course where cname = '数据库原理');
# (11)查询既选修了数据库原理而没有选修操作系统课程的学生学号
select sno
from student
where sno in (select a.sno
from (select sno from sc where cno = (select cno from course where cname = '数据库原理')) a
left join (select distinct sno
from sc
where cno = (select cno from course where cname = '操作系统')) b
on a.sno = b.sno
where b.sno is null);
# (12)查询全是男同学选修的课程号
select distinct cno
from SC x
where not exists(select cno
from student
where ssex = '女'
and exists(select *
from SC y
where y.cno = x.cno
and y.sno = student.sno));
# 2.组合查询和统计查询
#(1)使用集合运算查询既选修了数据结构课程又选修了数据库原理课程的学生姓名。
# mysql8.0不支持intersect
select s.sname
from student s
where s.sno in (select t1.sno
from (select sno
from SC
where cno = (select cno
from Course
where cname = '数据结构')) t1
inner join
(select sno
from SC
where cno = (select cno
from Course
where cname = '数据库原理')) t2 on t1.sno = t2.sno);
#(2)使用集合运算查询选修了数据结构课程或选修了数据库原理课程的学生学号。
select sname
from student
where sno in (select sno
from SC
where cno in (select cno
from Course
where cname = '数据结构'))
union
select sname
from student
where sno in (select sno
from SC
where cno in (select cno
from Course
where cname = '数据库原理'));
#(3)使用集合运算查询选修了数据结构课程而没有选修了数据库原理课程的学生
# mysql8.0不支持except
select sname
from student
where sno in (select sno
from SC sc1
where sc1.cno in (select cno
from Course
where cname = '数据结构'))
and sno
not in (select sc2.sno
from SC sc2
where sc2.cno in (select cno
from Course
where cname = '数据库原理'));
#(4)统计选修了课程的学生人数。
select count(distinct sno) as stusum
from SC;
#(5)查询选修成绩合格的课程超过4门以上学生的学生学号、总成绩。
select sc.sno, sum(sc.grade) gradesum
from sc
where sc.grade >= 60
group by sc.sno
having count(sc.cno) >= 4;
#(6)统计各院系的学生人数。
select s.sdept, count(s.sdept) sum
from student s
group by s.sdept;
#(7)统计各年龄的学生人数。
select s.sage, count(s.sage) sum
from student s
group by s.sage;
#(8)统计每个学生的选修课程数目和平均成绩。
select sno,
count(sc.sno) stusum,
avg(sc.grade) avggrade
from sc
group by sno;
#(9)查询每门课程的详细信息及选课人数。
select Course.*, 选课人数
from Course
left join (select cno, count(*) 选课人数
from SC
group by cno) as a on a.cno = Course.cno;
Java实验报告——教务系统(继承)
一、实验目的
使学生进一步了解Java面向对象中继承、封装、抽象、重载的运用。
二、实验内容
1、设计教师、学生、课程这三个教务系统中的对象类,包括这些对象的属性和方法。实现学生选课、删除课程、查看课程成绩、教师批准选课及录入成绩。
2、要求用到继承、封装、抽象、重载。
三、实验步骤
1抽象类Person的设计
//Person.java
package wsdchong;
public abstract class Person
protected String name;
protected String sex;
protected int age;
public final String getName()
return name;
abstract String getInfo();
2学生类的设计:继承自Person类,用两个<课程类>的动态数组保存选课列表和已经选上的课程列表,实现选课、删除选课、查看成绩、查看个人信息等方法。
//Student.java
package wsdchong;
import java.util.ArrayList;
public class Student extends Person
private String stuno;
private int grade;
private ArrayList<Course> myCourses = new ArrayList<Course>(); //我的课程
private ArrayList<Course> mySelectCourses = new ArrayList<Course>(); //我的选课
Student(String name, String sex, int age, String stuno, int grade)
super();
this.name = name;
this.sex = sex;
this.age = age;
this.stuno = stuno;
this.grade = grade;
public void selectCourse(Course c) //选课
if(!myCourses.contains(c) && !mySelectCourses.contains(c))
mySelectCourses.add(c);
public void delateSelectCourse(Course c) //删除选课
mySelectCourses.remove(c);
if (ifApprove(c)) // 如果已中签
myCourses.remove(c);
c.removeStudent(this);
c.getTeacher().removeStudent(this);
public void addCourse(Course c) //选课成功,将课程加入我的课程
myCourses.add(c);
public String getScore(Course c) //获取学生课程的成绩
if (myCourses.contains(c))
return name+"的"+c.getName()+"成绩为: "+c.searchScore(this);
else
return "你的课程列表里没有这门课";
public Boolean ifSelect(Course c) //判断学生是否选择某课程
return mySelectCourses.contains(c);
public Boolean ifApprove(Course c) //判断学生是否选择某课程
return myCourses.contains(c);
public String getCourse() //获取课程列表
String s = "课程:";
for(Course c:myCourses)
s += c.getName() + " ";
return s;
public String getScoreList() //获取成绩单
String s = "学生 "+ getName() + " 的成绩单如下:\\n";
for(Course c:myCourses)
s = s+ c.getName() + " " + c.searchScore(this) + "\\n";
return s;
public String getInfo()
return "姓名:"+name+"\\n性别:"+sex+"\\n年龄:"+age+"\\n学号:"+stuno+"\\n年级:"+grade+"\\n"+getCourse();
3、教师类的设计:继承自Person类,用<课程类>的动态数组表示教师所教课程列表,用<学生类>的动态数组表示自己的学生名单,实现设置教师所教课程、同意学生选课、为学生设置成绩等方法。
//Teacher.java
package wsdchong;
import java.util.ArrayList;
public class Teacher extends Person
private String tno;
private ArrayList<Course> myCourse = new ArrayList<Course>(); //所授课程列表
private ArrayList<Student> myStudents = new ArrayList<Student>(); //学生名单
Teacher(String name,String sex, int age, String tno)
super();
this.name = name;
this.sex = sex;
this.age = age;
this.tno = tno;
void setCourse(Course c) //为老师设置所教课程
myCourse.add(c) ;
public void approveSelectCoures (Student s,Course c)
if (s.ifSelect(c) && myCourse.contains(c)) //如果学生s申请了这门课 且老师教这门课
s.addCourse(c); //同意选课,将课程加入到学生的课程表中
c.addStudent(s); //将课程加入到课程的学生名单中
if(!myStudents.contains(s))
myStudents.add(s);
public boolean removeStudent(Student s) //移除一名学生
for(Course c:myCourse)
if (c.ifContainStudent(s))
return false;
myStudents.remove(s);
return true;
public void setScore(Student s, float score,Course c) //为一名学生设置成绩
if(s.ifApprove(c) && myCourse.contains(c))
c.setScores(s, score);
else
System.out.println("学生"+s.getName()+"的课表中没有"+c.getName());
public String getInfo()
String s = "姓名:"+name+"\\n工号:"+tno+"\\n所授课程:";
for(Course c : myCourse) //遍历老师所教课程
s += c.getName();
s +=" ";
return s;
public String getStudentList(Course c) //获取课程名单
if(!myCourse.contains(c))
return "教师"+getName()+"并不教"+c.getName()+"这门课。\\n";
String s = "教师"+getName()+"所教的"+c.getName()+"课程学生名单:\\n";
for(Student stu: myStudents)
if (stu.ifApprove(c))
s = s + stu.getName() +"\\n";
return s;
4设计课程类,用<学生类>的动态数组表示该课程学生名单,用一个<学生类、浮点型>的hashmap来保存课程每名学生的成绩单。实现向课程名单中添加学生、向成绩单中添加一名学生的成绩的接口,实现获取课程成绩表的方法。
//Course.java
package wsdchong;
import java.util.HashMap;
import java.util.ArrayList;
public class Course
private String name;
private String cno;
private Teacher itsTeacher ;
private ArrayList<Student> studentList = new ArrayList<Student>(); //课程学生名单
private HashMap<Student, Float> scoreList = new HashMap<Student, Float>(); //课程成绩单
Course(String name, String cno)
super();
this.name = name;
this.cno = cno;
public String getName() return name;
public Teacher getTeacher() return itsTeacher;
public void setTeacher (Teacher t) //为这门课设置一名教师
this.itsTeacher = t;
t.setCourse(this);
public void addStudent (Student s) //课程名单里添加一名学生
studentList.add(s);
public void setScores (Student s , float score) //给学生s设置成绩
scoreList.put(s, score);
public float searchScore (Student s) //返回学生s的成绩
return scoreList.get(s);
public void removeStudent(Student s) //移除学生
if (s.ifApprove(this))
studentList.remove(s);
public boolean ifContainStudent(Student s)
return studentList.contains(s);
public String getInfo()
return "课程名称:"+name+"课程编号:"+cno+"授课教师:"+itsTeacher;
public String getStudentList() //获取学生列表
String s = "" ;
for(Student stu : studentList)
s += stu.getName()+" ";
return s;
public String getScoreList() //课程成绩表
String s = "课程"+this.getName()+"的成绩表\\n";
for(Student stu:scoreList.keySet())
s += stu.getName()+" "+scoreList.get(stu)+"\\n";
return s;
5进行测试,经测试,各功能均可以正常实现。
五、实验总结
1、本次实验使我进一步了解了Java中继承、封装、抽象、重载的运用。
以上是关于数据库实验(学生选课系统)的主要内容,如果未能解决你的问题,请参考以下文章