Java实验5 面向抽象与接口编程

面向抽象编程实验内容:
(1)利用面向抽象的编程思想,计算各种柱体的体积。
在这里插入图片描述
下图为该程序的UML类图。编写测试类PillarTest,输出底为圆形、长方形、三角形的柱体体积。
在这里插入图片描述
程序设计思路:
Pillar 面向 对象设计, 柱体, 只负责计算 体积,一个abstract类,类名为Geometry,Geometry的子类有Circle,rectangle和Triangle

程序代码:

package java_experiment_five;
abstract class Geometry{
	public abstract double getArea();
}
class Pillar{
	Geometry bottom;
	private double height;
	Pillar(Geometry bottom,double height){
		this.bottom=bottom;
		this.height=height;
	}
	public double getVolume() {
		return bottom.getArea()*this.height;
	}
}
class Rectangle extends Geometry{
	private double a,b;
	Rectangle(double a,double b){
		this.a=a;
		this.b=b;
	}
	public double getArea() {
		return a*b;
	}
}
class Circle extends Geometry{
	private double r;
	Circle(double r){
		this.r=r;
	}
	public double getArea() {
		return Math.PI*r*r;
	}
}
class Triangle extends Geometry{
	private double x,y;
	Triangle(double x,double y){
		this.x=x;
		this.y=y;
	}
	public double getArea() {
		return x*y/2;
	}
}
public class PillarTest {

	public static void main(String[] args) {
		Pillar pillar;
		Geometry bottom;
		bottom = new Rectangle(2,3);
        pillar = new Pillar(bottom,6);
        System.out.println("长方体体积: "+pillar.getVolume());
		bottom=new Circle(4);
		pillar = new Pillar(bottom,6);
        System.out.println("圆柱体体积: "+pillar.getVolume());
        bottom = new Triangle(2,4);
        pillar = new Pillar(bottom,6);
        System.out.println("三角形体积: "+pillar.getVolume());
	}
}

运行截图:
在这里插入图片描述
面向抽象编程实验内容:
(2)要求有一个abstract类,类名为Employee。Employee的子类有YearWorker,MonthWorker和WeekWorker。YearWorker对象按年领取薪水,MonthWorker按月领取薪水,WeekWorker按周领取薪水。主程序HardWork能输出一年需要支付的薪水总额。
Employee类有一个abstract方法:public abstract double earnings(); 子类必须重写父类的earnings()方法,给出各自领取报酬的具体方法。
Employee[] e=new Employee[3];
e[0]=new YearWorker();
e[1]=new MonthWorker();
e[2]=new WeekWorker ();
程序设计思路:
一个abstract类名为Employee,Employee的子类有YearWorker,MonthWorker和WeekWorker,子类必须重写父类的earnings()方法。
程序代码:

package java_experiment_five;
abstract class Employee{
	abstract double earnings();
}
class YearWorker extends Employee{
	int year;
	double yearSalary;
	YearWorker(int y,double s){
		this.year=y;
		this.yearSalary=s;
	}
	double earnings() {
		return year*yearSalary;
	}
	
}
class MonthWorker extends Employee{
	int month;
	double monthSalary;
	MonthWorker(int m,double s){
		this.month=m;
		this.monthSalary=s;
	}
	double earnings() {
		return month*monthSalary;
	}
	
}
class WeekWorker extends Employee{
	int week;
	double weekSalary;
	WeekWorker(int w,double s){
		this.week=w;
		this.weekSalary=s;
	}
	double earnings() {
		return week*weekSalary;
	}
	
}
public class HardWork {
	public static void main(String[] args) {
		Employee[] e=new Employee[3];
		e[0]=new YearWorker(1, 100000);
		e[1]=new MonthWorker(12, 6000);
		e[2]=new WeekWorker (48, 1400);
		double sum;
		sum=e[0].earnings()+e[1].earnings()+e[2].earnings();
		System.out.println("总工资为:"+sum);
	}
}

运行截图:
在这里插入图片描述
面向抽象编程实验内容:
(3)多态性是指统一的接口,不同的表现形式。在Game类是定义了play方法,其子类Football、Basketball和Popolong从Game类派生而来。
public abstract class Game {
protected abstract void play();
}
请实现类Football、Basketball、Popolong,各个类的调用方式如下:
public class Games {
public static void main(String[] args) {
Game[] games = new Game[3];
games[0] = new Basketball();
games[1] = new Football();
games[2] = new Popolong();

for(int i=0;i<games.length;i++){
if(games[i]!=null)
games[i].play();
}
}
}
请运行以上代码,观察代码的输出。另外,请考虑多态性是如何提高程序的扩展能力的?
程序设计思路:
一个abstract类名为Game,Game的子类有Football、Basketball、Popolong。
程序代码:

package java_experiment_five;
abstract class Game{
	 protected abstract void play();
}
class Basketball extends Game{
	protected void play() {
		System.out.println("Play Basketball");
	}
	
}
class Football extends Game{
	protected void play() {
		System.out.println("Play football");
	}
	
}
class Popolong extends Game{
	protected void play() {
		System.out.println("Play Popolong");
	}
	
}
public class Games {

	public static void main(String[] args) {
		Game[] games = new Game[3];
		games[0] = new Basketball();
		games[1] = new Football();
		games[2] = new Popolong();		 
		for(int i=0;i<games.length;i++){
		   if(games[i]!=null)
		       games[i].play();
		 }
	}
}

运行截图:
在这里插入图片描述
面向接口编程实验内容:
(1)为某研究所编写一个通用程序,用来计算每一种交通工具运行1000公里所需的时间,已知每种交通工具的参数都是3个浮点数A、B、C的表达式。现有两种工具:Car和Plane,其中Car的速度运算公式为:A*B/C,Plane 的速度运算公式为:A+B+C;需要编写三个类:ComputeTime、Plane、Car和接口Common,要求在未来如果增加第3种交通工具的时候,不必修改以前的任何程序,只需要编写新的交通工具的程序。
程序设计思路:
接口Common,三个类:ComputeTime、Plane、Car,(Common)Class.forName(args[0]).newInstance();提高程序的扩展能力
程序代码:

package java_experiment_five;
interface Common{
	//抽象方法速度公式
	double speed(int A,int B,int C);
}
class Plane implements Common{
	public double speed(int A, int B, int C) {
		return A+B+C;
	}
}
class Car implements Common{
	public double speed(int A, int B, int C) {
		return A*B/C;
	}
}
public class ComputeTime {
	public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		double s=1000;
		double v;
		double time;
		int A=Integer.parseInt(args[1]);
		int B=Integer.parseInt(args[2]);
		int C=Integer.parseInt(args[3]);
		Common common=(Common)Class.forName(args[0]).newInstance();//java_experiment_five.Car
		v=common.speed(A, B, C);
		time=s/v;
		System.out.println(args[0]+"运行1000公里所需的时间为:"+time);
	}
}

运行截图:
在这里插入图片描述
面向接口编程实验内容:
(2)接口回调技术
要求有一个ComputeTotalSales接口,该接口中有一个方法:
public double totalSalesByYear( );
有三个实现该接口的类:Television,Computer和Mobile。这三个类通过实现ComputeTotalSales接口,给出自己的年销售额。
定义一个Shop类,该类用ComputeTotalSales数组作为成员,该数组可以存放Television,Computer或Mobile对象的引用。
利用接口回调技术计算Shop对象的年销售额。
程序设计思路:
ComputeTotalSales接口,三个实现该接口的类:Television,Computer和Mobile,定义一个Shop类里面写一个方法计算年销售额,主类Sale利用接口回调技术计算Shop对象的年销售额
程序代码:

package java_experiment_five;
interface ComputeTotalSales{
	public double totalSalesByYear( );
}
class Television implements ComputeTotalSales{
	public double totalSalesByYear() {
		return 10000;
	}
}
class Computer implements ComputeTotalSales{
	public double totalSalesByYear() {
		return 20000;
	}
}
class Mobile implements ComputeTotalSales{
	public double totalSalesByYear() {
		return 30000;
	}
}
class Shop{
	double totalSales;
	ComputeTotalSales[] goods;
	Shop(ComputeTotalSales[] goods){
		this.goods=goods;
	}
	public double giveTotalSales(){
		totalSales=0;
		for(int i=0;i<goods.length;i++){
			totalSales=totalSales+goods[i].totalSalesByYear();
		}
		return totalSales;
	}
}
public class Sale {
	public static void main(String[] args) {
		ComputeTotalSales[] goods=new ComputeTotalSales[40];
		for(int i=0;i<goods.length;i++){
			if(i%3==0) {
				goods[i]=new Television();
			}
			else if(i%3==1) {
				goods[i]=new Computer();
			}
			else if(i%3==2) {
				goods[i]=new Mobile();
			}
		}
		Shop shop=new Shop(goods);
		System.out.println("所有商品的年销售额为:"+shop.giveTotalSales());
	}
}

运行截图:
在这里插入图片描述

  • 10
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值