Java实验五

《Java程序设计》课程第五周实验题目和代码。

第一题

1、 编写一个程序,通过面向抽象编程思想,实现学位颁发功能。具体设计要求如下。
2、 设计一个抽象的 Student 类,具体要求如下:
(1) 该抽象类所包含的属性为:
String name;
char gender;
Date birthDay;
String school;
String stuId;
String category;
double[] score;
boolean status;
分别对应学生的姓名、性别、出生日期、学校、学号、学生类别、课程成绩、
学位颁发状态。分别要求包含对应属性的 set/get 方法。
(2) 同时,定义如下三个抽象方法:
public abstract boolean getPass();
public abstract double computeAverage();
public abstract String getDegree();
分别用于判定是否满足学位颁发条件、计算学生课程平均值、以及判定是否颁
发学位。
3、 设计一个继承 Student 抽象类的具体类 Undergraduate 类,具体要求如下:
(1) 定义一个 double 型变量 aver,用于保存该类中大学生课程成绩的平均值;
(2) 重写 Student 类中定义的三个抽象方法。
(3) 在重写 computeAverage()方法过程中,要求通过算术平均值计算大学生课程
平均分。
(4) 在重写 getPass()方法过程中,根据大学生课程平均分判定,若课程成绩平
均分 aver 大于等于 60 分,则满足学位颁发要求,改变学位颁发状态。
(5) 在重写 getDegree()方法过程中,根据学生的学位颁发状态,确定是否授予
“学士学位”的决定。
4、 设计一个继承 Student 抽象类的具体类 Master 类,具体要求如下:
(1) 定义一个 double 型变量 aver,用于保存硕士研究生课程成绩的平均值;定
义字符型一个变量 thesisLevel,用于保存硕士研究生学位论文等级(取值
范围为’A’,’B’,’C’,’D’,’E’)。
(2) 重写 Student 类中定义的三个抽象方法。
(3) 在重写 computeAverage()方法过程中,要求通过几何平均值计算硕士研究生
课程平均分。
(4) 在重写 getPass()方法过程中,根据硕士研究生课程平均分和学位论文两个
方面判定,若课程成绩平均分 aver 大于等于 80 分且学位论文等级为’C’及其
以上,则满足学位颁发要求,改变学位颁发状态。
(5) 在重写 getDegree()方法过程中,根据硕士研究生的学位颁发状态,确定是
否授予“硕士学位”的决定。
5、 设计一个具体类 StudentDegree,用于管理各类学生的学位授予。具体要求如下:
(1) 该类包含两个方法:
public void printStudentInfo(Student student)
public void issueDegree(Student student)
分别用于输出学生的基本信息(包括姓名、性别、出生日期、学校、学号、
学生类别、平均成绩、平均分)、输出学生授予学位情况。
(2) 提示:在第 1 个方法 printStudentInfo 中,通过抽象类作为各类学生的
上转型对象,调用重写方法 computeAverage(),计算对应学生类别的对象
平均课程成绩;
(3) 提示:在第 2 个方法 issueDegree 中,通过抽象类作为各类学生的上转型
对象,调用重写方法 getDegree(),输出对应学生类型的对象的学位授予情
况。
6、 设计一个测试类 StudentApplication,用于测试学生学位授予情况。具体要求如下:
(1) 创建一个 Student 对象 student,用于作为上转型对象。
(2) 创建一个 Undergraduate 类对象 undergraduate,采用如下赋值:
String name=”王小二”;
char gender=’男’;
Date birthday=”1995-6-1”;
String school=”上海大学”;
String stuId=”11128981”;
String category=”大学本科生”;
double[] score={89.5, 82, 87, 73};
boolean status=false;
(3) 创建一个 Master 对象 master,采用如下赋值:
String name=”李燕”;
char gender=”女”;
Date birthday=”1991-6-12”;
String school=”上海大学”;
String stuId=”10306”;
String category=’硕士研究生’;
double[] score={70, 52.5, 95, 88, 89, 91};
char thesisLevel=’B’;
boolean status=false;
(4) 创建一个 StudentDegree 类的对象 studentDegree,用于测试学位授予。
(5) 分别使用 student 作为 undergraduate 和 master 的上转型对象,测试
studentDegree 使用 student 上转型对象,输出学生信息和学位授予情况。
为了便于各类常量符合管理,我们可以创建一个类 Constants,里面定义各类在此
Java 程序中使用到的常量,分别如下:
public class Constants {
public final static String UNDERGRADUATE = “大学本科生”;
public final static String MASTER = “硕士研究生”;
public final static String ISSUE_UNDERGRADUATE_DEGREE=”颁发学士学位”;
public final static String ISSUE_MASTER_DEGREE=”颁发硕士学位”;
public final static String UNSATISFACTORY_ISSUE_UNDERGRADUATE_DEGREE=”不满足颁发学士学位要”;
public final static String UNSATISFACTORY_ISSUE_MASTER_DEGREE=”不满足颁发硕士学位要求”;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;


//创建一个类 Constants,里面定义各类在此Java 程序中使用到的常量,分别如下:
class Constants {
public final static String UNDERGRADUATE = "大学本科生";
public final static String MASTER = "硕士研究生";
public final static String ISSUE_UNDERGRADUATE_DEGREE="颁发学士学位";
public final static String ISSUE_MASTER_DEGREE="颁发硕士学位";
public final static String UNSATISFACTORY_ISSUE_UNDERGRADUATE_DEGREE="不满足颁发学士学位要求";
public final static String UNSATISFACTORY_ISSUE_MASTER_DEGREE="不满足颁发硕士学位要求";
}


//抽象类Student
abstract class Student{
private String name; //学生姓名
private char gender; //学生性别
private Date birthDay; //学生出生日期
private String school; //学校
private String stuId; //学号
private String category; //学生类别
private double []score; //课程成绩
private boolean status; //学位颁发状态

//设置和获得学生姓名
void setName(String name) {
this.name=name;
}
String getName() {
return name;
}

//设置和获得学生性别
void setGender(char gender) {
this.gender=gender;
}
char getGender() {
return gender;
}

//设置和获得学生出生日期
void setBirthDay(Date birthday) {
birthDay=birthday;
}
Date getBirthDay() {
return birthDay;
}

//设置和获得学生学院
void setSchool(String school) {
this.school=school;
}
String getSchool() {
return school;
}

//设置和获得学生学号
void setStuId(String stuId) {
this.stuId=stuId;
}
String getStuId() {
return stuId;
}

//设置和获得学生类别
void setCategory(String category) {
this.category=category;
}
String getCategory() {
return category;
}

//设置和获得学生成绩
void setScore(double []s) {
score=s;
}
double[] getScore() {
return score;
}

//设置和获得学生学位颁发状态
void setStatus(boolean s) {
status=s;
}
boolean getStatus() {
return status;
}



public abstract boolean getPass(); //判断是否满足学位颁发条件
public abstract double computeAverage(); //计算学生课程平均值
public abstract String getDegree(); //判定是否发学位

}


class Undergraduate extends Student{
private double aver; //大学生课程成绩的平均值


//判断是否满足学位颁发条件
public boolean getPass() {
double a;
a=computeAverage();
if(a>=60) {
boolean k=true;
setStatus(k);
return true;
}
else
return false;
}

//计算学生课程平均值
public double computeAverage() {
double []score;
int i,n;
double undergraduatem=0;

score=getScore();
n=score.length;
for(i=0;i<n;i++) {
undergraduatem+=score[i];
}
aver=undergraduatem/n;
return aver;
}

//判定是否发学位
public String getDegree() {
boolean k=getStatus();
if(k==true)
return Constants.ISSUE_UNDERGRADUATE_DEGREE;
else
return Constants.UNSATISFACTORY_ISSUE_UNDERGRADUATE_DEGREE;
}
}


class Master extends Student{
private double aver; //硕士研究生课程成绩的平均值
char thesislevel; //硕士研究生学位论文等级

void setThesislevel(char t) {
thesislevel=t;
}

//判断是否满足学位颁发条件
public boolean getPass() {
double a;
a=computeAverage();
if(a>=80 && (thesislevel=='C'||thesislevel=='B'||thesislevel=='A')) {
boolean k=true;
setStatus(k);
return true;
}
else
return false;
}

//计算硕士研究生课程几何平均值
public double computeAverage() {
double []score;
int i,n;
double undergraduatem=1;

score=getScore();
n=score.length;
for(i=0;i<n;i++) {
undergraduatem*=score[i];
}
aver=Math.pow(undergraduatem,1.0/n);
return aver;
}

//判定是否发学位
public String getDegree() {
boolean k=getStatus();
if(k==true)
return Constants.ISSUE_MASTER_DEGREE;
else
return Constants.UNSATISFACTORY_ISSUE_MASTER_DEGREE;
}
}

class StudentDegree{
//输出学生的基本信息(包括姓名、性别、出生日期、学校、学号、学生类别、平均成绩、平均分)
public void printStudentInfo(Student student) {
String name; //学生姓名
char gender; //学生性别
Date birthDay; //学生出生日期
String school; //学校
String stuId; //学号
String category; //学生类别
double avg; //平均成绩

//获得学生的基本信息(包括姓名、性别、出生日期、学校、学号、学生类别、平均成绩、平均分)
name=student.getName();
gender=student.getGender();
birthDay=student.getBirthDay();
school=student.getSchool();
stuId=student.getStuId();
category=student.getCategory();
avg=student.computeAverage();

//输出学生的基本信息(包括姓名、性别、出生日期、学校、学号、学生类别、平均成绩、平均分)
System.out.println("学生姓名: "+name);
System.out.println("学生性别: "+gender);
System.out.println("学生出生日期: "+birthDay);
System.out.println("学生学校: "+school);
System.out.println("学生学号: "+stuId);
System.out.println("学生学号: "+category);
System.out.println("学生成绩: "+avg);

}

//输出学生授予学位情况
public void isundergraduateeDegree(Student student) {
String s;
student.getPass();
student.computeAverage();
s=student.getDegree();
System.out.println("学生学位授予情况: "+s);

}

}



public class StudentApplication {
public static void main(String []args)throws ParseException {
//创建一个 Student 对象 student,用于作为上转型对象
Student student;

//创建一个 Undergraduate 类对象 undergraduate,采用如下赋值
Undergraduate undergraduate =new Undergraduate();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date myDate1 = dateFormat.parse("2009-06-01");
double []score1= {89.5, 82, 87, 73};

undergraduate.setName("王小二");
undergraduate.setGender('男');
undergraduate.setBirthDay(myDate1);
undergraduate.setSchool("上海大学");
undergraduate.setStuId("11128981");
undergraduate.setCategory(Constants.UNDERGRADUATE);
undergraduate.setScore(score1);
undergraduate.setStatus(false);

//创建一个 Master 对象 master,采用如下赋值
Master master=new Master();
DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
Date myDate2 = dateFormat1.parse("2009-06-01");
double []score2= {70, 52.5, 95, 88, 89, 91};

master.setName("李燕");
master.setGender('女');
master.setBirthDay(myDate2);
master.setSchool("上海大学");
master.setStuId("10306");
master.setCategory(Constants.MASTER);
master.setScore(score2);
master.setThesislevel('B');
master.setStatus(false);

//创建一个 StudentDegree 类的对象 studentDegree,用于测试学位授予。
StudentDegree studentDegree=new StudentDegree();
studentDegree.isundergraduateeDegree(undergraduate);
studentDegree.isundergraduateeDegree(master);

//使用student作为 undergraduate的上转型对象,测试studentDegree 使用 student 上转型对象,输出学生信息和学位授予情况。
student=undergraduate;
studentDegree.printStudentInfo(student);
studentDegree.isundergraduateeDegree(student);

//使用 student作为 master 的上转型对象,测试studentDegree 使用 student 上转型对象,输出学生信息和学位授予情况。
student=master;
studentDegree.printStudentInfo(student);
studentDegree.isundergraduateeDegree(student);


}

}


Tips

关于Date类型

a. 获取当前日期和时间

这是一种在Java中获取当前日期和时间的简单方法。可以使用Date对象toString()方法来打印当前日期和时间,如下图所示。

1
2
3
4
5
6
7
8
9
10
11
import java.util.Date;
public class Test {

public static void main(String args[]) {
// 实例化Date对象
Date date = new Date();

// display time and date using toString()
System.out.println(date.toString());
}
}
b. 使用SimpleDateFormat设置日期格式

SimpleDateFormat是一个具体的类,用于以区域设置的方式格式化和解析日期。 SimpleDateFormat用于从为日期时间格式选择用户定义的模式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

public static void main(String args[]) {
Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat("yyyy.MM.dd (E)'at' hh:mm:ss a zzz");

System.out.println("Current Date: " + ft.format(dNow));

SimpleDateFormat ft2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

System.out.println("Current Datetime: " + ft2.format(dNow));

}
}
打赏
  • © 2025 Aoxue
  • Hexo Theme Ayer by shenyu
    • PV:
    • UV:

请我喝杯咖啡吧~

支付宝
微信