写在前面
1、基于2022级软件工程/计算机科学与技术实验指导书
2、代码仅提供参考
3、如果代码不满足你的要求,请寻求其他的途径
运行环境
window11家庭版
IntelliJ IDEA 2023.2.2
jdk17.0.6
实验要求
1、 控制台菜单。在 Exp02_x 类中添加一个方法 menu_x,其他要求要同实验 01-1。
2、 编写一个矩形类 Exp02Rectangle_x,要求如下:
1) 矩形的宽度和高度由构造方法来确定,要求长和高的范围为 0~30。为此类的方法 有 getArea_x 返回矩形的面积;getPerimeter_x 返回矩形的周长;draw_x 使用星号 (*)作为描绘字符画出该矩形。
2) 在 Exp02_x 类中添加一个方法 rectangleExec_x 调用 Exp02Rectangle_x 对象的方法, 它被 menu_x 调用。
3、 编写一个三角形类 Exp02Triangle_x,要求如下:
1) 能根据三个实数(范围为 0~30)构造三角形对象,如果三个实数不满足三角形的条 件,则自动构造以最小值为边的等边三角形。输入任意三个数,输出构造的三角形 面积。
2) 在 Exp02_x 类中添加一个方法 triangleExec_x 调用 Exp02Triangle_x 对象的方法, 它被 menu_x 调用。
4、 编写一个二维平面上的点的类 Exp02Point_x,要求如下:
1) 表示坐标的两个成员变量 x_x,y_x;
2) 两个构造方法,不带参数的功能是置成员变量为 0;带参数的传值给成员变量,要 使用 this 关键字;
3) 定义 distance_x 方法,计算两个坐标点的距离;定义 graph_x 方法,参照上面的 draw_x 绘制与两个坐标点有关的图形,如矩形、圆形等(选做,加分)
4) 在 Exp02_x 类中添加一个方法 pointExec_x 调用 Exp02Point_x 对象的方法,它被 menu_x 调用。
5、 设计一个成绩管理系统。要求如下:
1) 系统的主要功能有(1)初始化至少三位教师、三门课程、五位同学和 N 个成绩数 据,(2)使用教师编号、学生学号登录,(3)教师录入、显示、查询、修改、统计 (选做)成绩,(4)学生查看成绩(同时显示总分和平均分);
2) 新建类文件 Exp02Score_x,类 Exp02Score_x 的成员变量至少有分数。在此类文件 中现分别添加:(1)Exp02Teacher_x 类,成员变量至少有教师编号、教师姓名;(2) Exp02Student_x 类,成员变量至少有学生学号、学生姓名;(3)Exp02Course_x 类, 成员变量至少有课程编号、课程名称。根据自己的理解,为前述 4 个类设计成员方 法以实现 1)中的功能;
3) 新建类文件 Exp02ScoreManage_x,类 Exp02ScoreManage_x 的主要功能初始化数据 和用方法 scoreManageExec_x 调用 2)中各个(不一定是所有)类的成员方法,它 被 menu_x 调用以及其他方法;
4) 其他要求:(1)定义的成员变量必须在恰当的地方使用;(2)要能保证数据合法, 如成绩应该在 0~100 之间;录入或修改时超过范围应能提示;(3)功能应该符合逻 辑,如教师未录入成绩前,学生应该查询不到成绩等。
源代码
Exp02_x.java
import java.util.Scanner;
//实验题目1:编写一个矩形类
class Exp02Rectangle_x {
private int width;
private int height;
// 构造方法确定矩形的宽度和高度
public Exp02Rectangle_x(int width, int height) {
if (width >= 0 && width <= 30 && height >= 0 && height <= 30) {
this.width = width;
this.height = height;
} else {
System.out.println("宽度和高度必须在0~30之间");
}
}
// 计算矩形的面积
public int getArea_x() {
return width * height;
}
// 计算矩形的周长
public int getPerimeter_x() {
return 2 * (width + height);
}
// 绘制矩形
public void draw_x() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
//实验题目2:编写一个三角形类
class Exp02Triangle_x {
private double side1;
private double side2;
private double side3;
// 构造方法确定三角形的三条边
public Exp02Triangle_x(double side1, double side2, double side3) {
// 如果输入的边长不满足三角形的条件,则构造以最小值为边的等边三角形
if (isValidTriangle(side1, side2, side3)) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
System.out.println("输入的边长满足条件,按输入的三边长构建三角形");
} else {
this.side1 = this.side2 = this.side3 = findShortestSide(side1,side2,side3);
System.out.println("输入的边长不满足条件,按最小值的边长构建普通三角形");
}
}
// 判断三边是否构成三角形
private boolean isValidTriangle(double side1, double side2, double side3) {
return side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1;
}
//找出最小值的边
private double findShortestSide(double side1,double side2,double side3){
if (side1<=side2&&side1<=side3){
return side1;
}
else if (side2<=side1&&side2<=side3){
return side2;
}
else {
return side3;
}
}
// 计算三角形的面积
public double getArea_x() {
// 海伦公式计算面积
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
}
//实验题目3:编写一个二维平面上的点类
class Exp02Point_x {
private double x_x;
private double y_x;
// 不带参数的构造方法,将坐标置为0
public Exp02Point_x() {
this.x_x = 0;
this.y_x = 0;
}
// 带参数的构造方法,传值给成员变量
public Exp02Point_x(double x_x, double y_x) {
this.x_x = x_x;
this.y_x = y_x;
}
// 计算两个点之间的距离
public double distance_x(Exp02Point_x other) {
double dx = this.x_x - other.x_x;
double dy = this.y_x - other.y_x;
return Math.sqrt(dx * dx + dy * dy);
}
}
public class Exp02_x {
// 调用 Exp02Rectangle_x 对象的方法
public void rectangleExec_x() {
Scanner scanner = new Scanner(System.in);
int width, height;
// 获取矩形的宽度和高度
System.out.print("请输入矩形的宽度:");
width = scanner.nextInt();
System.out.print("请输入矩形的高度:");
height = scanner.nextInt();
// 创建矩形对象
Exp02Rectangle_x rectangle = new Exp02Rectangle_x(width, height);
// 输出矩形的面积和周长
System.out.println("矩形的面积为:" + rectangle.getArea_x());
System.out.println("矩形的周长为:" + rectangle.getPerimeter_x());
// 绘制矩形
System.out.println("绘制的矩形为:");
rectangle.draw_x();
}
// 调用 Exp02Triangle_x 对象的方法
public void triangleExec_x() {
Scanner scanner = new Scanner(System.in);
double side1, side2, side3;
// 获取三角形的三条边长
System.out.print("请输入三角形的第一条边长:");
side1 = scanner.nextDouble();
System.out.print("请输入三角形的第二条边长:");
side2 = scanner.nextDouble();
System.out.print("请输入三角形的第三条边长:");
side3 = scanner.nextDouble();
// 创建三角形对象
Exp02Triangle_x triangle = new Exp02Triangle_x(side1, side2, side3);
// 输出三角形的面积
System.out.println("三角形的面积为:" + triangle.getArea_x());
}
// 调用 Exp02Point_x 对象的方法
public void pointExec_x() {
Scanner scanner = new Scanner(System.in);
double x1, y1, x2, y2;
// 获取两个点的坐标
System.out.print("请输入第一个点的 x 坐标:");
x1 = scanner.nextDouble();
System.out.print("请输入第一个点的 y 坐标:");
y1 = scanner.nextDouble();
System.out.print("请输入第二个点的 x 坐标:");
x2 = scanner.nextDouble();
System.out.print("请输入第二个点的 y 坐标:");
y2 = scanner.nextDouble();
// 创建两个点对象
Exp02Point_x point1 = new Exp02Point_x(x1, y1);
Exp02Point_x point2 = new Exp02Point_x(x2, y2);
// 输出两点之间的距离
System.out.println("两点之间的距离为:" + point1.distance_x(point2));
}
// 显示菜单
public void menu_x() {
int choice;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择实验题目:");
System.out.println("0.结束");
System.out.println("1.编写一个矩形类");
System.out.println("2.编写一个三角形类");
System.out.println("3.编写一个二维平面上的点的类");
System.out.println("4.设计一个成绩管理系统");
System.out.print("请输入你的选择:");
choice = scanner.nextInt();
switch (choice) {
case 0:
System.out.println("程序已退出");
return;
case 1:
rectangleExec_x();
break;
case 2:
triangleExec_x();
break;
case 3:
pointExec_x();
break;
case 4:
Exp02ScoreManage_x scoreManage = new Exp02ScoreManage_x();
scoreManage.scoreManageExec_x();
break;
default:
System.out.println("输入的数据无效,请重新输入");
break;
}
}
}
}
Exp02Score_x.java
import java.util.HashMap;
import java.util.Map;
// 实验题目5:设计一个成绩管理系统
class Exp02Teacher_x {
private String teacherId;
private String teacherName;
private String courseId;
public Exp02Teacher_x(String teacherId, String teacherName, String courseId) {
this.teacherId = teacherId;
this.teacherName = teacherName;
this.courseId = courseId;
}
public String getTeacherId() {
return teacherId;
}
public String getTeacherName() {
return teacherName;
}
public String getCourseId() {
return courseId;
}
}
// Exp02Student_x 类
class Exp02Student_x {
private String studentId;
private String studentName;
private int score1;
private int score2;
private int score3;
public Exp02Student_x(String studentId, String studentName) {
this.studentId = studentId;
this.studentName = studentName;
this.score1 = -1;
this.score2 = -1;
this.score3 = -1;
}
public String getStudentId() {
return studentId;
}
public String getStudentName() {
return studentName;
}
public int getScore1() {
return score1;
}
public int getScore2() {
return score2;
}
public int getScore3() {
return score3;
}
public void setScore1(int score1) {
this.score1 = score1;
}
public void setScore2(int score2) {
this.score2 = score2;
}
public void setScore3(int score3) {
this.score3 = score3;
}
}
// Exp02Course_x 类
class Exp02Course_x {
private String courseId;
private String courseName;
public Exp02Course_x(String courseId, String courseName) {
this.courseId = courseId;
this.courseName = courseName;
}
public String getCourseId() {
return courseId;
}
public String getCourseName() {
return courseName;
}
}
// Exp02Score_x 类
public class Exp02Score_x {
private Map<String, Exp02Student_x> students;
private Map<String, Exp02Teacher_x> teachers;
private Map<String, Exp02Course_x> courses;
private Map<String, Integer> scores;
public Exp02Score_x() {
students = new HashMap<>();
teachers = new HashMap<>();
courses = new HashMap<>();
scores = new HashMap<>();
}
// 初始化学生、老师、课程及成绩
public void initialize() {
initializeTeachers();
initializeStudents();
initializeCourses();
initializeScores();
}
// 初始化老师
public void initializeTeachers() {
teachers.put("T1", new Exp02Teacher_x("T1", "张老师", "C1"));
teachers.put("T2", new Exp02Teacher_x("T2", "李老师", "C2"));
teachers.put("T3", new Exp02Teacher_x("T3", "王老师", "C3"));
}
// 初始化学生
public void initializeStudents() {
students.put("S1", new Exp02Student_x("S1", "小明"));
students.put("S2", new Exp02Student_x("S2", "小红"));
students.put("S3", new Exp02Student_x("S3", "小刚"));
students.put("S4", new Exp02Student_x("S4", "小亮"));
students.put("S5", new Exp02Student_x("S5", "小美"));
}
// 初始化课程
public void initializeCourses() {
courses.put("C1", new Exp02Course_x("C1", "语文"));
courses.put("C2", new Exp02Course_x("C2", "数学"));
courses.put("C3", new Exp02Course_x("C3", "英语"));
}
// 初始化成绩,默认为未录入成绩,显示为-1
public void initializeScores() {
for (Exp02Student_x student : students.values()) {
scores.put(student.getStudentId(), -1);
}
}
// 教师登录
public boolean teacherLogin(String teacherId) {
return teachers.containsKey(teacherId);
}
// 学生登录
public boolean studentLogin(String studentId) {
return students.containsKey(studentId);
}
// 录入成绩
public void addScore(String studentId, int score, String teacherId) {
Exp02Student_x student = students.get(studentId);
Exp02Teacher_x teacher = teachers.get(teacherId);
if (student != null && score >= 0 && score <= 100) {
String courseId = teacher.getCourseId();
if (courseId.equals("C1")) {
student.setScore1(score);
} else if (courseId.equals("C2")) {
student.setScore2(score);
} else if (courseId.equals("C3")) {
student.setScore3(score);
}
System.out.println(student.getStudentName() + "成绩录入成功!");
} else if (student == null) {
System.out.println("学生学号不存在!");
} else {
System.out.println("成绩录入失败!成绩必须在0到100之间,请重新录入。");
}
}
// 显示学生的成绩
public void displayStudentScores(String studentId,String teacherId) {
Exp02Teacher_x teacher=teachers.get(teacherId);
Exp02Student_x student = students.get(studentId);
if (student != null) {
String courseId = teacher.getCourseId();
Exp02Course_x course=courses.get(courseId);
if (course != null) {
if (courseId.equals("C1")) {
int score = student.getScore1();
if (score == -1) {
System.out.println("学生的成绩还未录入!");
} else {
System.out.println("学生 " + student.getStudentName() + " " + course.getCourseName() + " 的成绩:" + score);
}
} else if (courseId.equals("C2")) {
int score = student.getScore2();
if (score == -1) {
System.out.println("学生的成绩还未录入!");
} else {
System.out.println("学生 " + student.getStudentName() + " " + course.getCourseName() + " 的成绩:" + score);
}
} else if (courseId.equals("C3")) {
int score = student.getScore3();
if (score == -1) {
System.out.println("学生的成绩还未录入!");
} else {
System.out.println("学生 " + student.getStudentName() + " " + course.getCourseName() + " 的成绩:" + score);
}
}
} else {
System.out.println("课程编号不存在!");
}
} else {
System.out.println("学生学号不存在!");
}
}
// 修改学生成绩
public void modifyScore(String studentId, int newScore, String teacherId) {
Exp02Student_x student = students.get(studentId);
Exp02Teacher_x teacher = teachers.get(teacherId);
if (student != null && newScore >= 0 && newScore <= 100) {
String courseId = teacher.getCourseId();
if (courseId.equals("C1")) {
student.setScore1(newScore);
} else if (courseId.equals("C2")) {
student.setScore2(newScore);
} else if (courseId.equals("C3")) {
student.setScore3(newScore);
}
System.out.println(student.getStudentName() + "成绩修改成功!");
} else if (student == null) {
System.out.println("学生学号不存在!");
} else {
System.out.println("成绩修改失败!新成绩必须在0到100之间,请重新输入。");
}
}
// 删除学生成绩
public void deleteScore(String studentId,String teacherId) {
Exp02Student_x student = students.get(studentId);
Exp02Teacher_x teacher = teachers.get(teacherId);
if (student != null) {
String courseId=teacher.getCourseId();
if (courseId.equals("C1")) {
student.setScore1(-1);
} else if (courseId.equals("C2")) {
student.setScore2(-1);
} else if (courseId.equals("C3")) {
student.setScore3(-1);
}
} else {
System.out.println("学生学号不存在!");
}
}
// 学生查看所有成绩
public void displayAllScores(String studentId) {
Exp02Student_x student = students.get(studentId);
if (student != null) {
System.out.println("学生 " + student.getStudentName() + " 的成绩单:");
if (student.getScore1() != -1) {
System.out.println("语文成绩:" + student.getScore1());
} else {
System.out.println("语文成绩:成绩还未录入");
}
if (student.getScore2() != -1) {
System.out.println("数学成绩:" + student.getScore2());
} else {
System.out.println("数学成绩:成绩还未录入");
}
if (student.getScore3() != -1) {
System.out.println("英语成绩:" + student.getScore3());
} else {
System.out.println("英语成绩:成绩还未录入");
}
} else {
System.out.println("学生学号不存在!");
}
}
// 计算学生总分
public int calculateTotalScore(String studentId) {
Exp02Student_x student = students.get(studentId);
if (student != null) {
if (student.getScore1() == -1 || student.getScore2() == -1 || student.getScore3() == -1) {
return -1; // 表示成绩未录入完毕
} else {
return student.getScore1() + student.getScore2() + student.getScore3();
}
} else {
return 0;
}
}
// 计算学生平均分
public double calculateAverageScore(String studentId) {
Exp02Student_x student = students.get(studentId);
if (student != null) {
int totalScore = calculateTotalScore(studentId);
if (totalScore == -1) {
return -1; // 表示成绩未录入完毕
} else {
return totalScore / 3.0;
}
} else {
return 0;
}
}
}
Exp02ScoreManage_x.java
import java.util.Scanner;
public class Exp02ScoreManage_x {
private Exp02Score_x scoreManager;
public Exp02ScoreManage_x() {
scoreManager = new Exp02Score_x();
initializeData();
}
// 初始化数据
public void initializeData() {
scoreManager.initialize();
}
// 调用 Exp02Score_x 中各个类的成员方法
public void scoreManageExec_x() {
Scanner scanner = new Scanner(System.in);
int role = 0;
boolean exitProgram = false;
while (!exitProgram) {
System.out.println("请选择您的身份:");
System.out.println("1. 教师");
System.out.println("2. 学生");
System.out.println("-1. 退出程序");
System.out.printf("你的选择是:");
role = scanner.nextInt();
switch (role) {
case 1:
System.out.println("请输入教师编号登录:");
String teacherId = scanner.next();
if (scoreManager.teacherLogin(teacherId)) {
System.out.println("登录成功!");
boolean exitTeacherMenu = false;
while (!exitTeacherMenu) {
System.out.println("请选择操作:");
System.out.println("1. 录入学生成绩");
System.out.println("2. 显示学生成绩");
System.out.println("3. 修改学生成绩");
System.out.println("4. 删除学生成绩");
System.out.println("0. 返回上一级菜单");
System.out.printf("你的选择是:");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入学生学号:");
String studentId = scanner.next();
System.out.println("请输入成绩:");
int score = scanner.nextInt();
scoreManager.addScore(studentId, score, teacherId);
break;
case 2:
System.out.println("请输入学生学号:");
String queryStudentId = scanner.next();
scoreManager.displayStudentScores(queryStudentId, teacherId);
break;
case 3:
System.out.println("请输入学生学号:");
String modifyStudentId = scanner.next();
System.out.println("请输入新的成绩:");
int newScore = scanner.nextInt();
scoreManager.modifyScore(modifyStudentId, newScore, teacherId);
System.out.println("成绩修改成功!");
break;
case 4:
System.out.println("请输入学生学号:");
String deleteStudentId = scanner.next();
scoreManager.deleteScore(deleteStudentId, teacherId);
System.out.println("成绩删除成功!");
break;
case 0:
exitTeacherMenu = true;
break;
default:
System.out.println("请输入有效的操作选项!");
break;
}
}
} else {
System.out.println("教师编号不存在!");
}
break;
case 2:
System.out.println("请输入学生学号登录:");
String studentId = scanner.next();
if (scoreManager.studentLogin(studentId)) {
System.out.println("登录成功!");
boolean exitStudentMenu = false;
while (!exitStudentMenu) {
System.out.println("请选择操作:");
System.out.println("1. 查看成绩");
System.out.println("0. 返回上一级菜单");
System.out.printf("你的选择是:");
int choice = scanner.nextInt();
switch (choice) {
case 1:
scoreManager.displayAllScores(studentId);
int totalScore = scoreManager.calculateTotalScore(studentId);
double averageScore = scoreManager.calculateAverageScore(studentId);
if (totalScore != -1 && averageScore != -1) {
System.out.println("总分:" + totalScore);
System.out.println("平均分:" + averageScore);
} else {
System.out.println("总分:成绩还未录入完毕,请耐心等候!");
System.out.println("平均分:成绩还未录入完毕,请耐心等候!");
}
break;
case 0:
exitStudentMenu = true;
break;
default:
System.out.println("请输入有效的操作选项!");
break;
}
}
} else {
System.out.println("学生学号不存在!");
}
break;
case -1:
exitProgram = true;
System.out.println("程序已退出!");
break;
default:
System.out.println("请选择有效的选项!");
break;
}
}
scanner.close();
}
}
Main.java
public class Main {
public static void main(String[] args) {
Exp02_x exp02 = new Exp02_x();
exp02.menu_x(); // 调用菜单方法
}
}