栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

实验类型:设计型

实验内容

(一)面向抽象编程

  1. 利用面向抽象的编程思想,计算各种柱体的体积。

下图为该程序的UML类图。编写测试类PillarTest,输出底为圆形、长方形、三角形的柱体体积。

abstract class Geometry{
    public abstract double getArea();
}
//抽象类Geometry,其子类Circle,rectangle,Triangle

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(4,5);
        pillar=new Pillar(bottom,6);
        System.out.println("长方体体积: "+pillar.getVolume());
        bottom=new Circle(5);
        pillar = new Pillar(bottom,6);
        System.out.println("圆柱体体积: "+pillar.getVolume());
        bottom = new Triangle(3,4);
        pillar = new Pillar(bottom,6);
        System.out.println("三角形体积: "+pillar.getVolume());
    }
}
//测试类PillarTest
  1. 要求有一个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 ();

  1. 多态性是指统一的接口,不同的表现形式。在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

   if(games[i]!=null)

      games[i].play();

  } 

 }

}

请运行以上代码,观察代码的输出。另外,请考虑多态性是如何提高程序的扩展能力的?

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);
	}
}

(二)面向接口编程

(1)为某研究所编写一个通用程序,用来计算每一种交通工具运行1000公里所需的时间,已知每种交通工具的参数都是3个浮点数A、B、C的表达式。现有两种工具:Car和Plane,其中Car的速度运算公式为:A*B/C,Plane 的速度运算公式为:A+B+C;需要编写三个类:ComputeTime、Plane、Car和接口Common,要求在未来如果增加第3种交通工具的时候,不必修改以前的任何程序,只需要编写新的交通工具的程序。

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对象的年销售额。

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 

实验要求

  1. 掌握上转型对象的应用技巧。
  2. 掌握抽象类和抽象方法的具体应用。
  3. 掌握软件设计原则:开-闭原则的具体应用方法及面向抽象的编程思想。
  4. 掌握接口如何定义以及接口在程序扩展性中的应用。
  5. 掌握接口回调技术的应用。
  6. 实验报告中写出程序的设计思路,关键代码,调试错误及解决方案。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/924713.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号