课程报告二:面向抽象类与接口的编程

课程报告二:面向抽象类与接口的编程

一、实验目的

(1)掌握抽象类和接口的设计
(2)掌握面向对象编程的基本方法
(3)掌握面向抽象编程的原则

二、实验内容

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

程序设计思路:
一个abstract类名为Employee,Employee的子类有YearWorker,MonthWorker和WeekWorker,子类必须重写父类的earnings()方法。

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;

   //请参考以上程序,补全代码	
}

public class HardWork {
	public static void main(String[] args) {
	   //请补全程序,计算出总工资
//############
		System.out.println("总工资为:"+sum);
	}
}

2、编程题

要求有一个ComputeTotalSales接口,该接口中有一个方法:public double totalSalesByYear( );有三个实现该接口的类:Television,Computer和Mobile。这三个类通过实现ComputeTotalSales接口,给出自己的年销售额。定义一个Shop类,该类用ComputeTotalSales数组作为成员,该数组可以存放Television,Computer或Mobile对象的引用。利用接口回调技术计算Shop对象的年销售额。

程序设计思路:
ComputeTotalSales接口,三个实现该接口的类:Television,Computer和Mobile,定义一个Shop类里面写一个方法计算年销售额,主类Sale利用接口回调技术计算Shop对象的年销售额。
程序参考如下:

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(######) { //商品Television初始化
				goods[i]=new Television();
			}
			else if(######) {//商品Computer初始化
				goods[i]=new Computer();
			}
			else if(######) {//商品Mobile初始化
				goods[i]=new Mobile();
			}
		}
		Shop shop=new Shop(goods);
		System.out.println("所有商品的年销售额为:"+shop.giveTotalSales());
	}
}

三、实验代码清单与实验结果截图

1.编程题一代码和实验结果截图

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;
    }
    @Override
    double earnings() {
        return week*weekSalary;
    }

}

public class HardWork {
    public static void main(String[] args) {

        Employee[] e=new Employee[3];
        e[0]=new YearWorker(1,80000);
        e[1]=new MonthWorker(13,8000);
        e[2]=new WeekWorker (12,2000);

        int sum=0;

        for(Employee emp:e){
            sum+=emp.earnings();
        }

        System.out.println("总工资为:"+sum);
    }
}

在这里插入图片描述

2.编程题二代码和实验结果截图

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 (ComputeTotalSales good : goods) {
            totalSales = totalSales + good.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++){


            int random=(int) (Math.random()*3);   //生成0到2的随机数

            if(random==0) { //商品Television初始化
                goods[i]=new Television();
            }
			else if(random==1) {//商品Computer初始化
                goods[i]=new Computer();
            }
			else if(random==2) {//商品Mobile初始化
                goods[i]=new Mobile();
            }
        }
        Shop shop=new Shop(goods);
        System.out.println("所有商品的年销售额为:"+shop.giveTotalSales());
    }
}

在这里插入图片描述

四、实验小结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值