- 设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声,要求如下。
(1)编写一个接口Animal,Animal接口有2个抽象方法cry()和getAnimalName(),即要求实现该接口的各种具体的动物类给出自己的叫声和种类名称。
(2)编写模拟器类Simulator,该类有一个playSound(Animal animal)方法,该方法的参数是Animal类型。即参数animal可以调用实现Animal接口类重写的cry()方法播放具体动物的声音,调用重写的getAnimalName()方法显示动物种类的名称。
(3)编写实现Animal接口的Dog类和Cat类,下图是Simulator、Animal、Dog、Cat的UML图。
(4)编写主类Application(用户程序),在主类Application的main方法中至少包含如下代码:
Simulator simulator=new Simulator();
simulator.playSound(new Dog());
simulator.playSound(new Cat());
Animal
public abstract class Animal {
abstract void cry();
abstract void getAnimalName();
}
Dog
public class Dog extends Animal {
@Override
void cry() {
System.out.println("我是小脑斧");
}
@Override
void getAnimalName() {
System.out.println("我叫大黄");
}
}
Cat
public class Cat extends Animal{
@Override
void cry() {
System.out.println("我是小猫咪");
}
@Override
void getAnimalName() {
System.out.println("我叫小喵");
}
}
Simulator
public class Simulator {
public void playSound(Animal animal) {
animal.cry();
animal.getAnimalName();
}
}
main
public static void main(String[] args) {
System.out.println("----------------------");
Simulator simulator = new Simulator();
simulator.playSound(new Dog());
simulator.playSound(new Cat());
}
2. 设计一个简单的学校人事管理系统,管理教师和学生信息。实现教师与学生基本信息的添加、删除、修改、查询。其中编号、姓名、性别、出生日期为共有的,教师包括部门、职称、工资;学生包括高考分数、专业field、班级等。
BasicInfo
public class BasicInfo {
private int id;
private String name;
private String gender;
private String birthday;
public BasicInfo(int id, String name, String gender, String birthday) {
super();
this.id = id;
this.name = name;
this.gender = gender;
this.birthday = birthday;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
Student
public class Student extends BasicInfo {
private int grade;
private int classes;
private String field;
public Student(int id, String name, String gender, String birthday) {
super(id, name, gender, birthday);
}
public Student(int id, String name, String gender, String birthday, int grade, int classes, String field) {
super(id, name, gender, birthday);
this.grade = grade;
this.classes = classes;
this.field = field;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public int getClasses() {
return classes;
}
public void setClasses(int classes) {
this.classes = classes;
}
public String getField() {
return field;
}
public void getField(String professional) {
this.field = field;
}
public void show() {
System.out.println("编号:" + super.getId() + ",姓名:" + super.getName() + ",性别:" + super.getGender() + ",出生日期:"
+ super.getBirthday() + ",高考分数:" + grade + ",专业:" + field + ",班级:" + classes);
}
}
Teacher
public class Teacher extends BasicInfo {
private String department;
private String job;
private double salary;
public Teacher(int id, String name, String gender, String birthday) {
super(id, name, gender, birthday);
}
public Teacher(int id, String name, String gender, String birthday, String department, String job, double salary) {
super(id, name, gender, birthday);
this.department = department;
this.job = job;
this.salary = salary;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void show() {
System.out.println("编号:" + super.getId() + ",姓名:" + super.getName() + ",性别:" + super.getGender() + ",出生日期:"
+ super.getBirthday() + ",部门名称:" + department + ",职称:" + job + ",薪水:" + salary);
}
}
SchoolManagerSystem
@SuppressWarnings("all")
public class SchoolManagerSystem {
public static Scanner sc = new Scanner(System.in);
// 创建集合对象,用于存储学生数据
static ArrayList<Student> array = new ArrayList<Student>();
// 创建集合对象,用于存储老师数据
static ArrayList<Teacher> teacherArray = new ArrayList<Teacher>();
public static void main(String[] args) {
// 用Scanner实现键盘录入数据
boolean flag = true;
if (flag) {
System.out.println("----欢迎来到学校人事管理系统,请先登录----");
// 用循环完成再次回到主界面
while (true) {
// 用输出语句完成主界面的编写
System.out.println("请根据对应序号进行选择");
System.out.println("1 添加信息");
System.out.println("2 删除信息");
System.out.println("3 修改信息");
System.out.println("4 查看信息");
System.out.println("5 退出");
System.out.println("请输入你的选择:");
String line = sc.nextLine();
// 用switch语句完成操作的选择
switch (line) {
case "1":
chooseAdd(array, teacherArray);
break;
case "2":
chooseDelete(array, teacherArray);
break;
case "3":
chooseUpdate(array, teacherArray);
break;
case "4":
chooseFindAll(array, teacherArray);
break;
case "5":
System.out.println("谢谢使用");
System.exit(0); // JVM退出
default:
System.out.println("没有对应的操作,请重新选择");
}
}
}
}
public static void chooseAdd(ArrayList<Student> array, ArrayList<Teacher> teacherArray) {
Scanner sc = new Scanner(System.in);
System.out.println("输入添加的类别 1 添加学生 2 添加老师");
if (sc.nextInt() == 1) {
addStudent(array);
} else {
addTeacher(teacherArray);
}
}
public static void chooseDelete(ArrayList<Student> array, ArrayList<Teacher> teacherArray) {
Scanner sc = new Scanner(System.in);
System.out.println("输入删除的类别 1 删除学生 2 删除老师");
if (sc.nextInt() == 1) {
deleteStudent(array);
} else {
deleteTeacher(teacherArray);
}
}
public static void chooseUpdate(ArrayList<Student> array, ArrayList<Teacher> teacherArray) {
Scanner sc = new Scanner(System.in);
System.out.println("输入更新的类别 1 更新学生 2 更新老师");
if (sc.nextInt() == 1) {
updateStudent(array);
} else {
updateTeacher(teacherArray);
}
}
public static void chooseFindAll(ArrayList<Student> array, ArrayList<Teacher> teacherArray) {
Scanner sc = new Scanner(System.in);
System.out.println("输入查找的类别 1 查找学生 2 查找老师");
if (sc.nextInt() == 1) {
findAllStudent(array);
} else {
findAllTeacher(teacherArray);
}
}
// 定义一个方法,用于修改学生信息
public static void updateStudent(ArrayList<Student> array) {
if (array == null || array.size() == 0) {
System.out.println("没有数据,请先添加再进行修改");
return;
}
// 调用查询所有
findAllStudent(array);
// 键盘录入要修改的学生学号,显示提示信息
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要修改的学生的学号:");
int id = sc.nextInt();
boolean b = false;
// 遍历集合修改对应的学生信息
for (int i = 0; i < array.size(); i++) {
Student student = array.get(i);
if (student.getId() == id) {
student.show();
System.out.println("输入学生信息并用,分隔: 编号,姓名,性别,生日,分数,班级,专业");
Scanner sc2 = new Scanner(System.in);
String[] strings = sc2.nextLine().split(",");
// 键盘录入要修改的学生信息
int updateId = Integer.parseInt(strings[0]);
String name = strings[1];
String gender = strings[2];
String bir = strings[3];
int grade = Integer.parseInt(strings[4]);
int classes = Integer.parseInt(strings[5]);
String pro = strings[6];
Student newStudent = new Student(updateId, name, gender, bir, grade, classes, pro);
array.set(i, newStudent);
// 给出修改成功提示
System.out.println("修改学生成功");
b = true;
break;
}
}
if (b) {
// 调用查询所有方法
findAllStudent(array);
System.out.println("----------------------------------------");
System.out.println("----------------------------------------");
} else {
System.out.println("您要修改的学员不存在!");
System.out.println("----------------------------------------");
System.out.println("----------------------------------------");
}
}
public static void updateTeacher(ArrayList<Teacher> teacherArray) {
if (teacherArray == null || teacherArray.size() == 0) {
System.out.println("没有数据,请先添加再进行修改");
return;
}
// 调用查询所有
findAllTeacher(teacherArray);
// 键盘录入要修改的教师编号,显示提示信息
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要修改的教师的编号:");
int id = sc.nextInt();
boolean b = false;
// 遍历集合修改对应的学生信息
for (int i = 0; i < teacherArray.size(); i++) {
Teacher teacher = teacherArray.get(i);
teacher.show();
if (teacher.getId() == id) {
System.out.println("输入教师信息并用,分隔: 编号,姓名,性别,生日,部门,职称,工资");
Scanner sc2 = new Scanner(System.in);
String[] strings = sc2.nextLine().split(",");
// 键盘录入要修改的教师信息
String name = strings[1];
String gender = strings[2];
String bir = strings[3];
String department = strings[4];
String job = strings[5];
double salary = Double.parseDouble(strings[6]);
Teacher newTeacher = new Teacher(id, name, gender, bir, department, job, salary);
teacherArray.set(i, newTeacher);
// 给出修改成功提示
System.out.println("修改老师成功");
b = true;
break;
}
}
if (b) {
// 调用查询所有方法
findAllTeacher(teacherArray);
System.out.println("----------------------------------------");
System.out.println("----------------------------------------");
} else {
System.out.println("您要修改的教师不存在!");
System.out.println("----------------------------------------");
System.out.println("----------------------------------------");
}
}
// 定义一个方法,用于删除学生信息
public static void deleteStudent(ArrayList<Student> array) {
if (!checkStudent(array)) {
System.out.println("----------------------------------------");
System.out.println("------------无数据,请先添加-------------");
System.out.println("----------------------------------------");
return;
}
findAllStudent(array);
// 键盘录入要删除的学生学号,显示提示信息
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要删除的学生的学号:");
int id = sc.nextInt();
// 在删除/修改学生操作前,对学号是否存在进行判断
// 如果不存在,显示提示信息
// 如果存在,执行删除/修改操作
int index = -1;
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
if (s.getId() == id) {
// int index = i;
index = i; // 把集合的索引赋值给 变量
break;
}
}
if (index == -1) {
System.out.println("该信息不存在,请重新输入");
} else {
array.remove(index); // 根据索引删除集合的元素
// 给出删除成功提示
System.out.println("删除学生成功");
findAllStudent(array);
System.out.println("----------------------------------------");
System.out.println("----------------------------------------");
}
}
public static void deleteTeacher(ArrayList<Teacher> teacherArray) {
if (!checkTeacher(teacherArray)) {
System.out.println("----------------------------------------");
System.out.println("------------无数据,请先添加-------------");
System.out.println("----------------------------------------");
return;
}
findAllTeacher(teacherArray);
// 键盘录入要删除的老师学号,显示提示信息
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要删除的教师的学号:");
int id = sc.nextInt();
// 在删除/修改教师操作前,对教师编号是否存在进行判断
// 如果不存在,显示提示信息
// 如果存在,执行删除/修改操作
int index = -1;
for (int i = 0; i < teacherArray.size(); i++) {
Teacher t = teacherArray.get(i);
if (t.getId() == id) {
// int index = i;
index = i; // 把集合的索引赋值给 变量
break;
}
}
if (index == -1) {
System.out.println("该信息不存在,请重新输入");
} else {
teacherArray.remove(index); // 根据索引删除集合的元素
// 给出删除成功提示
System.out.println("删除老师成功");
findAllTeacher(teacherArray);
System.out.println("----------------------------------------");
System.out.println("----------------------------------------");
}
}
public static void addStudent(ArrayList<Student> array) {
System.out.println("输入学生信息并用,分隔: 编号,姓名,性别,生日,分数,班级,专业");
// 键盘录入学生对象所需要的数据,显示提示信息,提示要输入何种信息
Scanner sc = new Scanner(System.in);
String[] strings = sc.nextLine().split(",");
if (strings.equals(" ")) {
System.out.println("请输入信息");
} else {
int id = Integer.parseInt(strings[0]);
String name = strings[1];
String gender = strings[2];
String bir = strings[3];
int grade = Integer.parseInt(strings[4]);
int classes = Integer.parseInt(strings[5]);
String field = strings[6];
Student student = new Student(id, name, gender, bir, grade, classes, field);
array.add(student);
System.out.println("添加学生成功");
// 调用查询所有方法
findAllStudent(array);
System.out.println("----------------------------------------");
System.out.println("----------------------------------------");
}
}
public static void addTeacher(ArrayList<Teacher> teacherArray) {
System.out.println("输入教师信息并用,分隔: 编号,姓名,性别,生日,部门,职称,工资");
// 键盘录入老师对象所需要的数据,显示提示信息,提示要输入何种信息
Scanner sc = new Scanner(System.in);
String[] strings = sc.nextLine().split(",");
if (strings.equals(" ")) {
System.out.println("请输入信息");
} else {
int id = Integer.parseInt(strings[0]);
String name = strings[1];
String gender = strings[2];
String bir = strings[3];
String department = strings[4];
String job = strings[5];
double salary = Double.parseDouble(strings[6]);
Teacher teacher = new Teacher(id, name, gender, bir, department, job, salary);
teacherArray.add(teacher);
System.out.println("添加老师成功");
// 调用查询所有方法
findAllTeacher(teacherArray);
System.out.println("----------------------------------------");
System.out.println("----------------------------------------");
}
}
// 定义一个方法,用于查看学生信息
public static void findAllStudent(ArrayList<Student> array) {
// 判断集合中是否有数据,如果没有显示提示信息
if (array.size() == 0) {
System.out.println("无信息,请先添加信息再查询");
// 为了让程序不在往下执行,给出return;
return;
}
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
s.show();
}
System.out.println("----------------------------------------");
System.out.println("----------------------------------------");
}
// 定义一个方法,用于查看学生信息
public static void findAllTeacher(ArrayList<Teacher> teacherArray) {
// 判断集合中是否有数据,如果没有显示提示信息
if (teacherArray.size() == 0) {
System.out.println("无信息,请先添加信息再查询");
// 为了让程序不在往下执行,给出return;
return;
}
for (int i = 0; i < teacherArray.size(); i++) {
Teacher t = teacherArray.get(i);
t.show();
}
System.out.println("----------------------------------------");
System.out.println("----------------------------------------");
}
public static boolean checkStudent(ArrayList<Student> array) {
if (array == null || array.size() == 0) {
return false;
}
return true;
}
public static boolean checkTeacher(ArrayList<Teacher> teacherArray) {
if (teacherArray == null || teacherArray.size() == 0) {
return false;
}
return true;
}
}
3.设计 3 个类 , 分别是学生类 Student, 本科生类 Undergaduate, 研究生类 Postgraduate, 其中 Student 类是一个抽象类 , 它包含一些基本的学生信息如姓名、所学课程、课程成绩等 , 而 Undergraduate 类和 Postgraduate 都是 Student 类的子类 , 它们之间的主要差别是计算课程成绩等级的方法有所不同 , 研究生的标准要比本科生的标准高一些 , 如下表所示。
本科生标准 研究生标准
80-100 优秀 90-100 优秀
70-80 良好 80-90 良好
60-70 一般 70-80 一般
50-60 及格 60-70 及格
50以下 不及格 60以下 不及格
假设某班级里既有本科生也有研究生 , 请编写程序统计出全班学生的成绩等级并显示出来。此题关键是设计一个学生数组 , 既能存放本科生对象 , 又能存放研究生对象。
Student
public abstract class Student {
private String name;
private String course;
private int score;
public Student(String name, String course, int score) {
super();
this.name = name;
this.course = course;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
Postgraduate
public class Postgraduate extends Student {
public Postgraduate(String name, String course, int score) {
super(name, course, score);
if (score > 90 && score <= 100) {
System.out.println("优秀");
} else if (score > 80 && score <= 90) {
System.out.println("良好");
} else if (score > 70 && score <= 80) {
System.out.println("一般");
} else if (score > 60 && score <= 70) {
System.out.println("及格");
}
}
}
Undergaduate
public class Undergaduate extends Student {
public Undergaduate(String name, String course, int score) {
super(name, course, score);
if (score > 80 && score <= 100) {
System.out.println("优秀");
} else if (score > 70 && score <= 80) {
System.out.println("良好");
} else if (score > 60 && score <= 70) {
System.out.println("一般");
} else if (score > 50 && score <= 60) {
System.out.println("及格");
}
}
}
TestStudent
public class TestStudent {
public static void main(String[] args) {
Student student[] = { new Undergaduate("小红", "Math", 100), new Postgraduate("小芳", "English", 70) };
for (int i = 0; i < student.length; i++) {
System.out.println(
"姓名:" + student[i].getName() + ",学科:" + student[i].getCourse() + ",分数:" + student[i].getScore());
}
}
}