目录
一、实验目的
二、实验内容
1.卖车-接口与多态编程
代码实现:
输出显示:
2.继承-Student类及其子类
代码实现:
输出显示:
2. 编程实现运算类—工厂设计模式
代码实现:
结果显示:
一、实验目的
1. 掌握类的继承机制
2. 掌握接口的定义方法
3. 熟悉成员方法或构造方法多态性
二、实验内容
1.卖车-接口与多态编程
【问题描述】
(1) 汽车接口(Car):有两个方法, getName()、getPrice()(接口源文件可以自己写,也可以直接从题目中下载)
(2) BMW类,实现汽车接口
(3) 奇瑞QQ类,实现汽车接口
(4) 桑塔那类,实现汽车接口
(4) 汽车出售店(CarShop)类,有成员变量:count(售出数量) 和total(总价),有方法:
void sellCar(Car car); (调用方法获取售出车的车型、售价;获取售出数量及总价)
int getCount(); int getTotal();
(5) 编写测试类:卖出一辆BMW、一辆奇瑞QQ、一辆桑塔那,输出结果.
【输入形式】
无
【输出形式】
卖出的第一辆车车型、单价
汽车店的总收入、卖出车数量
卖出的第二辆车车型、单价
汽车店的总收入、卖出车数量
卖出的第三辆车车型、单价
汽车店的总收入、卖出车数量
表4.1实验题目1测试用例
序号 |
样例输入 |
样例输出 |
1 |
无 |
carModel:BMW price:300000 totalIncome:300000 countOfSold:1 carModel:CherryQQ price:20000 totalIncome:320000 countOfSold:2 carModel:Santana price:280000 totalIncome:600000 countOfSold:3 |
代码实现:
//Car.java public interface Car { String getName(); int getPrice(); }
//BMW.java public class BMW implements Car{ @Override public String getName() { return "BMW"; } @Override public int getPrice() { return 300_000; } }
//CherryQQ.java public class CherryQQ implements Car{ @Override public String getName() { return "CherryQQ"; } @Override public int getPrice() { return 20_000;//可用下划线便于区分 } }
//Santana.java public class Santana implements Car{ @Override public String getName() { return "Santana"; } @Override public int getPrice() { return 280_000; } }
//CarShop.java public class CarShop { //设置成static后就能在Test中不用创建实例来调用CarShop中的方法 //即系统已给其分配内存 private static int count= 0; private static int totalIncome = 0; //接口car 这里的形参可以放入BMW CherryQQ public static void sellCar(Car car){ count++; totalIncome+= car.getPrice(); String info; info = "carModel:"+car.getName()+" price:"+car.getPrice()+" "+ "totalIncome:"+getTotalIncome()+" countOfSold:"+getCount(); System.out.println(info); } public static int getCount() { return count; } public static int getTotalIncome() { return totalIncome; } }
//Test.java public class Test { public static void main(String[] args) { BMW bmw = new BMW(); CherryQQ cherryQQ =new CherryQQ(); Santana santana = new Santana(); CarShop.sellCar(bmw); CarShop.sellCar(cherryQQ); CarShop.sellCar(santana); } }
输出显示:
carModel:BMW price:300000
totalIncome:300000 countOfSold:1
carModel:CherryQQ price:20000
totalIncome:320000 countOfSold:2
carModel:Santana price:280000
totalIncome:600000 countOfSold:3进程已结束,退出代码0
2.继承-Student类及其子类
【问题描述】类的声明:
(1)声明Student类:
属性包括:学号、姓名、英语成绩、数学成绩、Java成绩、总成绩(成绩为double型,学号、姓名为String类型)
方法包括:构造方法、标准方法、toString()方法、compare()方法(比较两个学生的总成绩,结果分大于、等于、小于)、sum()方法(计算总成绩)、testScore()方法(计算评测成绩)
注:评测成绩取三门课程成绩的平均分(评测成绩保留2位小数)
(2)声明StudentSJ(参与综合设计的学生)类为Student的子类,增加任务属性,并重写testScore()方法(计算评测成绩,评测成绩=三门课的平均分+3)
(3)声明StudentZZ(综合设计组长)类为Student的子类,增加责任属性,并重写testScore()方法(计算评测成绩,评测成绩=三门课的平均分+5)
(4)声明测试类:在主方法中声明Student类数组(含3个元素),生成3个对象存入数组,其中一个为Student类的对象,一个StudentSJ类的对象,一个StudentZZ类的对象,将方法testScore()发送给数组中的每一个元素,输出结果,并分析具体执行的是哪一个类中的方法。比较StudentSJ类对象和StudentZZ类对象的总成绩,输出结果。
【输入形式】
三行:第一行,学生的信息;
第二行,参加综合设计的学生信息
第三行,综合设计组长信息
【输出形式】
学生对象的详细信息
综合设计学生对象的详细信息
综合设计组长对象的详细信息
StudentSJ类对象和StudentZZ类对象的总成绩的比较结果
表4.2实验题目2测试用例
序号 |
1 |
样例输入 |
1001 Zhangsan 89 87 84 1002 Lisi 78 65 90 SystemFramework 1003 Wangwu 90 87 92 GroupLeader |
样例输出 |
stuNo:1001 name:Zhangsan english:89.0 math:87.0 Java:84.0 testScore:86.67 stuNo:1002 name:Lisi english:78.0 math:65.0 Java:90.0 testScore:80.67 task:SystemFramework stuNo:1003 name:Wangwu english:90.0 math:87.0 Java:92.0 testScore:94.67 duty:GroupLeader Lisi's total score is less than Wangwu (一个学生的信息占一行) |
代码实现:
//Student.java package experiment_4_2; public class Student { private String ID,name; private double english,math,java,total; public Student(String ID, String name, double english, double math, double java) { this.ID = ID; this.name = name; this.english = english; this.math = math; this.java = java; } public double sum(){ total = english+math+java; return total; } public static void compare(Student stu1,Student stu2){ if (stu1.sum()> stu2.sum()){ System.out.println(stu1.name+"'s total score is greater than "+stu2.name); } else if (stu1.sum()< stu2.sum()){ System.out.println(stu1.name+"'s total score is less than "+stu2.name); } else { System.out.println(stu1.name+"'s total score is equal to "+stu2.name); } } public double testScore(){ return sum()/3; } public String toString(){ String info; info = "stuNo:"+ID+" name:"+name+" english:"+english+" math:"+math+" Java:"+java+ " testScore:"+String.format("%.2f",testScore()); return info; } }
//StudentSJ.java package experiment_4_2; public class StudentSJ extends Student{ private String task; public StudentSJ(String ID, String name, double english, double math, double java,String task) { super(ID, name, english, math, java); this.task = task; } @Override public double testScore() { return 3+sum()/3; } @Override public String toString() { return super.toString()+" task:"+task; } }
//StudentZZ.java package experiment_4_2; public class StudentZZ extends Student{ private String duty; public StudentZZ(String ID, String name, double english, double math, double java,String duty) { super(ID, name, english, math, java); this.duty = duty; } @Override public double testScore() { return sum()/3+5; } @Override public String toString() { return super.toString()+" duty:"+duty; } }
//Test.java package experiment_4_2; import java.util.Scanner; public class Test { public static void main(String[] args) { Student[] students = new Student[3]; Scanner sc = new Scanner(System.in); students[0] = new Student(sc.next(), sc.next(), sc.nextDouble(), sc.nextDouble(), sc.nextDouble()); students[1] = new StudentSJ(sc.next(), sc.next(),sc.nextDouble(), sc.nextDouble(), sc.nextDouble(), sc.next()); students[2] = new StudentZZ(sc.next(), sc.next(),sc.nextDouble(), sc.nextDouble(), sc.nextDouble(), sc.next()); for (int i = 0; i < 3; i++) { System.out.println(students[i]); } Student.compare(students[1],students[2]); } }
输出显示:
2. 编程实现运算类—工厂设计模式
【问题描述】
(1)定义抽象类Operation,有double型数据成员numberA、numberB,有抽象方法getResult()
(2)定义Operation的4个子类,分别实现加、减、除运算,二个数的平均数运算
(3)定义类OperationFactory:有静态方法Operation createOperate(String operate); 根据形参operate的值创建相应加、减、除、平均值类的对象,赋给抽象类引用后返回
(4)定义测试类及主方法:用户从键盘输入运算数及运算符,根据运算符调用OperationFactory类的静态方法,创建相应实例,设置运算数,输出运行结果
【输入形式】
一个表达式(数据与运算符之间空格间隔)
【输出形式】
表达式运算的结果,保留3位小数
【样例输入】
表4.3实验题目3测试用例
序号 |
样例输入 |
样例输出 |
1 |
45.8 + 80.9 |
Result:126.700 |
2 |
100 / 3 |
Result:33.333 |
3 |
123 / 0 |
Devide by zero! |
代码实现:
//Operation.java package experiment_4_3; public abstract class Operation { protected double numberA,numberB; public abstract double getResult(); public void setNumberA(double numberA) { this.numberA = numberA; } public void setNumberB(double numberB) { this.numberB = numberB; } }
//Plus.java package experiment_4_3; public class Plus extends Operation{ @Override public double getResult() { return numberA+numberB; } }
//Subtraction.java package experiment_4_3; public class Subtraction extends Operation{ @Override public double getResult() { return numberA - numberB; } }
//Average.java package experiment_4_3; public class Average extends Operation{ @Override public double getResult() { return (numberA+numberB)/2; } }
//Division.java package experiment_4_3; public class Division extends Operation{ @Override public double getResult() { return numberA/numberB; } }
//OperationFactory.java package experiment_4_3; public class OperationFactory { public static Operation createOperation(String operate){ Operation op = null; switch (operate){ case "+":op = new Plus();break; case "-":op = new Subtraction();break; case "avg":op = new Average();break; case "/":op = new Division();break; } return op; } }
//Test.java package experiment_4_3; import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc =new Scanner(System.in); double a,b; String c; a= sc.nextDouble(); //一定要用空格隔开 要不然sc.next sc.nextDouble无法分辨 //System.out.println(a); c=sc.next(); //System.out.println(c) ; b = sc.nextDouble(); //System.out.println(b); Operation operation = OperationFactory.createOperation(c); operation.setNumberA(a); operation.setNumberB(b); if (c.equals("/") &&b==0){//字符的比较要用到equals System.out.println("Divide by zero!"); } else { System.out.println("Result:" + String.format("%.3f", operation.getResult())); } //System.out.println("result:"+String.format("%.3f",operation.getResult())); //上一行结果是Infinity //System.out.println(1/0);该行报错 } } //输入1+2不可以 //in:1 /2 //out:1.0 // 2
结果显示:
123 / 0
Divide by zero!进程已结束,退出代码0
45.8 + 80.9
Result:126.700进程已结束,退出代码0
100 / 3
Result:33.333进程已结束,退出代码0