Java面向抽象,接口编程实验

Posted 康小庄

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java面向抽象,接口编程实验相关的知识,希望对你有一定的参考价值。

  1. 设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声,要求如下。
    (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) 
				// 用输出语句完成主界面的编写
				// 1,庄康,男,2001年,800,1,计算机
				// 1,庄康,男,2001年,北京,经理,3400.00
				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 

以上是关于Java面向抽象,接口编程实验的主要内容,如果未能解决你的问题,请参考以下文章

学号20182335 2019-2020-1 《数据结构与面向对象程序设计》实验四报告

Java面向抽象,接口编程实验

2019JAVA第五次实验报告

第七周Java总结&实验总结

实验4

实验4