java_SSD3_实验报告_面向对象——多态

第一题

【Person、Student、Employee类】(注:此题在书上原题基础上有修改)设计一个名为Person的类和它的两个名为Student和Employee子类。

每个人都有姓名和电话号码。学生有年级状态(大一、大二、大三或大四)。将这些状态定义为常量。一个雇员有工资和受聘日期。定义一个名为MyDate的类,包含数据域:year(年)、month(月)和day(日)。将各个类的数据域进行封装,并设置合理的读写访问器。
覆盖每个类中的toString方法,返回类名及相应的类中可以获取的所有信息构成的字符串,如Person类的toString方法返回“Person类:姓名为*** 电话为***”;Student类的toString方法返回“Student类: 姓名为*** 电话为*** 年级为***”。

在Student和Employee两个类中分别加入displayObject()方法,可以打印出对学生和雇员的提示消息,提示学生“to ***(学生姓名):请按时交实验报告”,提示雇员“to ***(雇员姓名):请按时上班”。

目标输出任务:

  • 画出这些类的UML图。
  • 实现这些类。
  • 编写一个测试类,
    1)创建方法public static void m1(Person p),显示p的姓名;
    2)创建方法public static void m2(Person p),打印p的toString方法返回的字符串;
    3)创建方法public static void m3(Person p),如果p是Student类或者Employee类的实例,分别调用它们的displayObject();
    4)在主函数中创建Person、Student、Employee的对象实例,将它们均声明为Person类对象。将它们分别传入m1、m2和m3,体会多态。

(1)UML图:

在这里插入图片描述

(2)运行结果

在这里插入图片描述

(3)结果分析

对每个对象依次调用m1,m2,m3方法,依次打印出对象的名字和toString方法,对于student类和Employee类还会打印出displayObject方法。

(4)心得体会

代码量真的多,难度不大但是很繁琐。本题除了主函数所在的Program1类外还有四个类,每个类的定义与实现都是一个需要花时间的过程。

(5)源代码

Person类:

package 实验5;

public class Person {
	//私有数据域
	private String name;
	private String phoneNumber;
	//修改器和访问器
	public void setName(String name){
		this.name = name;
	}
	public String getName(){
		return name;
	}
	public void setPhoneNumber(String phoneNumber){
		this.phoneNumber = phoneNumber;
	}
	public String getPhoneNumber(){
		return phoneNumber;
	}
	//方法重写
	public String toString(){
		return "Person类: 姓名为:"+name+" 电话号码为: "+phoneNumber;
	}
}

Student类:

package 实验5;

public class Student extends Person {
	public final static String grade1 = "大一";
	public final static String grade2 = "大二";
	public final static String grade3 = "大三";
	public final static String grade4 = "大四";
	//私有数据域
	private static String grade;
	//构造函数
	Student(String grade){
		this.grade = grade;
	}
	//修改器与访问器
	public void setGrade(String grade){
		this.grade = grade;
	}
	public String getGrade(){
		return grade;
	}
	//方法重写
	public String toString(){
		return "Student类: 姓名为:"+getName()+" 电话号码为: "+getPhoneNumber()+" 年级为: "+grade;
	}
	//提示信息
	public void displayObject(){
		System.out.println("to"+getName()+":请按时交实验报告");
	}
}

Employee类:

package 实验5;

public class Employee extends Person{
	//私有数据域
	private int salary;
	private int year;
	private int month;
	private int day;
	private MyDate date;
	//构造函数
	Employee(int salary,int year,int month,int day){
		this.salary = salary;
		this.year   = year;
		this.month  = month;
		this.day    = day;
		date = new MyDate(year,month,day);
	}
	//修改器与访问器
	public void setSalary(int salary){
		this.salary = salary;
	}
	public int getSalary(){
		return salary;
	}
	//方法重写
	public String toString(){
		return "Employee类: 姓名为:"+getName()+" 电话号码为: "+getPhoneNumber()+" 工资为 "+salary+" 受聘日期为: "+date.toString();
	}
	//提示信息
	public void displayObject(){
		System.out.println("to"+getName()+":请按时上班");
	}
}

MyDate类:

package 实验5;

public class MyDate {
	//私有数据域
	private int year;
	private int month;
	private int day;
	//构造函数
	MyDate(int year,int month,int day){
		this.year  = year;
		this.month = month;
		this.day   = day;
	}
	//修改器与访问器
	public void setYear(int year){
		this.year = year;
	}
	public int getYear(){
		return year;
	}
	public void setMonth(int month){
		this.month = month;
	}
	public int getMonth(){
		return month;
	}
	public void setDay(int day){
		this.day = day;
	}
	public int getDay(){
		return day;
	}
	//方法重写
	public String toString(){
		return year+"年"+month+"月"+day+"日";
	}
}

Program1:

package 实验5;

public class Program1 {
	
	public final static String grade1 = "大一";
	public final static String grade2 = "大二";
	public final static String grade3 = "大三";
	public final static String grade4 = "大四";
	
	public static void m1(Person p){
		System.out.println(p.getName());
	}
	public static void m2(Person p){
		System.out.println(p.toString());
	}
	public static void m3(Person p){
		if(p instanceof Student){
			((Student) p).displayObject();
		}
		else if(p instanceof Employee){
			((Employee) p).displayObject();
		}
	}
	public static void main(String[] args) {
			Person person   = new Person();
			person.setName("张三");
			person.setPhoneNumber("13694562696");
			Person student  = new Student(grade1);
			student.setName("李四");
			student.setPhoneNumber("13745659693");
			Person employee = new Employee(10000, 2012, 10, 20);
			employee.setName("王五");
			employee.setPhoneNumber("13974569636");
			m1(person);
			m2(person);
			m3(person);
			m1(student);
			m2(student);
			m3(student);
			m1(employee);
			m2(employee);
			m3(employee);
	}

}

第二题

【课程类Course】改写程序清单10-6中的Course类。 使用ArrayList代替数组来存储学生。不应该改变Course类的原始合约(即不要改变构造方法和方法的方法头定义,包括返回类型、方法名及参数列表,但私有的成员可以改变)。

程序清单10-6:

public class Course {
  private String courseName;
  private String[] students = new String[100];
  private int numberOfStudents;
    
  public Course(String courseName) {
    this.courseName = courseName;
  }
  
  public void addStudent(String student) {
    students[numberOfStudents] = student;
    numberOfStudents++;
  }
  
  public String[] getStudents() {
    return students;
  }

  public int getNumberOfStudents() {
    return numberOfStudents;
  }  

  public String getCourseName() {
    return courseName;
  }  
  
  public void dropStudent(String student) {
    // Left as an exercise in Exercise 9.9
  }
}

程序清单10-5
【注意:请参考以下程序来测试Course类,同时注意在此段程序基础上,增加必要的代码,以完整地测试Course类中定义的所有方法】

public class TestCourse {
  public static void main(String[] args) {
    Course course1 = new Course("Data Structures");
    Course course2 = new Course("Database Systems");

    course1.addStudent("Peter Jones");
    course1.addStudent("Brian Smith");
    course1.addStudent("Anne Kennedy");

    course2.addStudent("Peter Jones");
    course2.addStudent("Steve Smith");

    System.out.println("Number of students in course1: "
      + course1.getNumberOfStudents());
    String[] students = course1.getStudents();
    for (int i = 0; i < course1.getNumberOfStudents(); i++)
      System.out.print(students[i] + ", ");
    
    System.out.println();
    System.out.print("Number of students in course2: "
      + course2.getNumberOfStudents());
  }
}

(1)运行结果

在这里插入图片描述

(2)结果分析

此结果在原有的基础上增加了对getName方法和dropStudent方法的测试,执行dropStudent(“Peter Jones”)后,学生个数少了一个,学生列表里也少了Peter Jones。

(3)心得体会

直接在源代码上修改,使工程量减少了很多。直接用ArrayList类中的remove方法比数组中删除元素更加快捷。

(4)源代码

Course类:

package 实验5;

import java.util.ArrayList;

public class Course {
	  private String courseName;
	  ArrayList<String> students = new ArrayList<>();
	  private int numberOfStudents;
	    
	  public Course(String courseName) {
	    this.courseName = courseName;
	  }
	  
	  public void addStudent(String student) {
	    students.add(student);
	    numberOfStudents++;
	  }
	  
	  public ArrayList<String> getStudents() {
	    return students;
	  }

	  public int getNumberOfStudents() {
	    return numberOfStudents;
	  }  

	  public String getCourseName() {
	    return courseName;
	  }  
	  
	  public void dropStudent(String student) {
		  for(int i=0;i<numberOfStudents;i++){
			  if(students.get(i).equals(student)){
				  students.remove(i);
				  numberOfStudents--;
			  }
		  }
	  }
	}

TestCourse类:

TestCourse类:
package 实验5;

import java.util.ArrayList;

public class TestCourse {
	  public static void main(String[] args) {
	    Course course1 = new Course("Data Structures");
	    Course course2 = new Course("Database Systems");

	    course1.addStudent("Peter Jones");
	    course1.addStudent("Brian Smith");
	    course1.addStudent("Anne Kennedy");

	    course2.addStudent("Peter Jones");
	    course2.addStudent("Steve Smith");

	    System.out.println("Name of course1 is: "+ course2.getCourseName());
	    System.out.println("Before dropping the student 'Peter Jones',the number of students in course1: "+ course1.getNumberOfStudents());
	    ArrayList<String> students = course1.getStudents();
	    for (int i = 0; i < course1.getNumberOfStudents(); i++)
	      System.out.print(students.get(i) + ", ");
	    System.out.println();
	    course1.dropStudent("Peter Jones");
	    System.out.println("After dropping the student 'Peter Jones',the number of students in course1: "+ course1.getNumberOfStudents());
	    for (int i = 0; i < course1.getNumberOfStudents(); i++)
		      System.out.print(students.get(i) + ", ");
	    System.out.println();
	    System.out.println("Name of course2 is: "+ course2.getCourseName());
	    System.out.println("Number of students in course2: "+ course2.getNumberOfStudents());
	  }
	}

第三题

(利用继承实现MyStack)在程序清单11-10中,MyStack是用组合实现的。扩展ArrayList创建一个新的栈类。实现MyStack类。编写一个测试程序,提示用户输入5个字符串,然后以逆序显示这些字符串。

(1)运行结果

在这里插入图片描述

(2)结果分析

将五串字符倒过来就是结果

(3)心得体会

弄懂题目意思花了我最长时间。一开始用ArrayList继承Stack,然后开始套娃,不知道这样做的意义。后面认真读题才发现是MyStack继承ArrayList,这道题有难度。

(4)源代码

MyStack 类:

package 实验5;

import java.util.ArrayList;

public class MyStack extends ArrayList{
	
	public Object pop(){
		Object o = get(this.size()-1);
		this.remove(this.size()-1);
		return o;
	}
	
	public void push(Object o){
		this.add(o);
	}
	
	public boolean isEmpty(){
		if(this.size()==0) return true;
		return false;
	}


Program3:

package 实验5;

import java.util.Scanner;

public class Program3 {

	public static void main(String[] args) {
		MyStack s = new MyStack();
		
		Scanner input = new Scanner(System.in);
		
		System.out.println("请依次输入五个字符串:");
		for(int i=0;i<5;i++){
			s.push(input.next());
		}
		
		System.out.println("将这五个字符串倒过来为:");
		while(!s.isEmpty()){
			System.out.print(s.pop()+" ");
		}
	}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Multiple-Choice Quiz 1 aaaba aadda 1.Which method must exist in every Java application? (a) main (b) paint (c) begin (d) init 2.Which of the following is the string concatenation operator in Java? (a) + (b) ^ (c) & (d) ++ 3.Which of the following statements is (are) true about the use of an asterisk (*) in a Java import statement? I. It does not incur run-time overhead. II. It can be used to import multiple packages with a single statement. III. It can be used to import multiple classes with a single statement. (a) I and III only (b) III only (c) I only (d) I, II, and III 4.A difference between the methods print and println of the class java.io.PrintWriter is that (a) print inserts a new line at the beginning of its output, but println does not (b) println appends a new line to the end of its output, but print does not (c) println inserts a new line at the beginning of its output, but print does not (d) print appends a new line to the end of its output, but println does not 5.What will be output when the following Java program segment is executed? int x = 5; int y = 2; System.out.println(x + y);//这种运算是顺序进行的,试试System.out.println(x + y + “1”); (a) 7 (b) 5 2 (c) 5+2 (d) 52 6.What is the right way to handle abnormalities in input on Java? (a) By handling these problems by providing exception handlers (b) By writing while loops to guard against bad input (c) By using the class FileFilter which gracefully filters out bad input data (d) By always specifying the throws clause in every method header where file I/O is performed 7.All Java exception classes are derived from the class (a) java.lang.Throwable (b) java.lang.Error (c) java.io.IOException (d) java.lang.RuntimeException

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值