形式系统

计算机专业教学
posts - 48, comments - 150, trackbacks - 0, articles - 10
  教师博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Java实验二

Posted on 2006-10-23 14:24 形式系统 阅读(712) 评论(8)  编辑 收藏 引用 网摘 所属分类: 编程开发

实验二 类的继承与多态

 

[实验目的]

1、        构造类的继承体系

2、        抽象类与多态

3、        类的静态成员使用

4、        重载与重写

 

[实验内容]

1、  (必作)利用多态性实现的简单工资单系统。基类:Employee,其派生类是:销售员类:CommissionWorker,他的收入是一小部分基本工资加上销售额的一定的百分比;计件工类:PieceworkWorker,他的收入取决他生产的工件数量;小时工类HourlyWorker:他的收入以小时计算。若超过40小时部分的报酬是平时的1.5倍。阅读后面所附代码,并补全以下代码中每个子类的的相关方法。采用硬编码方式(即不需要输入,直接用常量为参数建对象)对每一个类建立一个实例,放于Employee数组中,依次打印其工资。

2、  (必作)现规定销售员类的提成率输入必须进行检验,只能是0-1之间的一个数。否则不予接受并提示错误。按要求重构相应的方法。

3、  (选作)银行账户管理:定义一个基类 Acount 表示所有的账户。以此为基础派生出活期账户类( Saving )、定期账户类( FixSaving )、信用卡账户类( Checking )。每种账户均需提供存( deposit )和取( withdrawal )两种方法。根据下面要求设计相应的类,并提供测试主程序。

(1)       信用卡是储蓄类的一种,假设它可以在 5000 元范围内透支,它有一个用户密码,取款时,必须验证密码。定义一个信用卡类(从 Acount 帐户类中继承),然后实现取款和存款业务。

(2)       定期储蓄是储蓄的一种,假定定期分为一年期、三年期、五年期,利率分别为 5% 8% 10% 。用户在办理定期存款帐户时,必须确定其定期时段,中途不再在同一帐号上办理存款业务。取款是一次性完成,若提前取款,则全部金额的利息按活期利率 1% 计算。

(3)       活期储蓄在整个存续期内可自由存取,只要余额不为 0 ,账户不会被销户。

 

注:题 3 进行适当扩展(如输入和数据库支持),并提供相应的图形界面可作为课程设计。

Employee 类代码:

// 职员类

public abstract class Employee { 

    public Employee(String name){this.name=name;};      //constructor

    public abstract double getPay();                 //getPay

    public String getName(){return name;}

    private String   name;                

    // 以下类既是内部类,又是派生类。既避免一个包中类过多,也避免与其它包中的类同名。

    // 钟点工

    public static class HourlyWorker extends Employee{

        public HourlyWorker(String name,double wages,double hours){

           super(name);

       }

        public double getPay(){……        //calculate and return salary

        private double wages;            //wage per hour

        private double hours;            //hours worked for week

    };

    // 销售员

    public static class CommissionWorker extends Employee {

        public CommissionWorker(String name,double salary,double commission,double quantity){ super(name);}

        public double getPay(){...}

        public void setSalary(double salary){

           this.salary=salary;

       }

       // 按题 2 的要求,本方法需要重构

        public void setCommission(double commision){

           this.commission=commision;

       }

        public void setSellValue(double value){

           this.sellValue=value;

       }

        double salary;        // 底薪

        double commission;    // 提成率

        double sellValue;    // 销售额

    }  

    // 计件工

    public static class PieceWorker extends Employee {

        public PieceWorker(String name,double commission,double quantity){ super(name);}

        public double getPay(){……}

        public void setWage(double wage){……}

        public void setQuantity(int quantity){……}

        private double wage;    // 每件报酬

        private int quantity;    // 件数 ;

    }

}

 

实验报告要求:附上的代码要体现自己设计的部分,其余部分可打省略号。选作题不要求所有同学都作。不要求写在实验报告上。但从学习的角度,建议多加练习。若有完成者,也可将源代码打包并发送至电子邮箱 hong_yao@126.com 可获加分参考。

Feedback

# re: Java实验二  回复  更多评论   

2006-10-28 19:12 by 韦双周
import java.io.*;
public abstract class Employee {

public Employee(String name){this.name=name;}; //constructor

public abstract double getPay(); //getPay

public String getName(){return name;}

private String name;

// 以下类既是内部类,又是派生类。既避免一个包中类过多,也避免与其它包中的类同名。

// 钟点工

public static class HourlyWorker extends Employee{

public HourlyWorker(String name,double wages,double hours){


super(name);
this.wages=wages;
this.hours=hours;

}

public double getPay(){

if(hours<=40)
return (hours*wages);
else
return (40*wages+(hours-40)*1.5*wages);

}

private double wages; //wage per hour

private double hours; //hours worked for week

};

// 销售员

public static class CommissionWorker extends Employee {

public CommissionWorker(String name,double salary,double commission,double quantity)
{
super(name);
}

public double getPay(){
return (salary+sellValue*commission);

}

public void setSalary(double salary){

this.salary=salary;

}

// 按题 2 的要求,本方法需要重构

public void setCommission(double commision){

this.commission=commision;

}

public void setSellValue(double value){

this.sellValue=value;

}

double salary; // 底薪

double commission; // 提成率

double sellValue; // 销售额

}

// 计件工

public static class PieceWorker extends Employee {

public PieceWorker(String name,double commission,double quantity)
{
super(name);

}

public double getPay(){
return (quantity*wage);
}

public void setWage(double wage){
this.wage=wage;
}

public void setQuantity(int quantity){
this.quantity=quantity;
}

private double wage; // 每件报酬

private int quantity; // 件数 ;

}



public static void main(String[] args) {

HourlyWorker r1;
r1=new HourlyWorker("wang",34,41);
CommissionWorker r2;
r2=new CommissionWorker("li",0,0,0);
r2.setCommission(0.05);
r2.setSalary(500);
r2.setSellValue(10000);
PieceWorker r3;
r3=new PieceWorker("zhao",0,0);
r3.setQuantity(2100);
r3.setWage(1.5);
System.out.println(r1.getPay());
System.out.println(r2.getPay());
System.out.println(r3.getPay());
}

}

# re: Java实验二  回复  更多评论   

2006-10-31 10:36 by 学生
运行结果
1411.0
1000.0
3150.0

# 我写了一个不用内部类的 发现确实不够精简  回复  更多评论   

2007-10-25 12:20 by 张祖旺

public class HourlyWorker extends Employee
{
private double money1;

private double wages1;

private double hours1;

public HourlyWorker(String name,double wages,double hours)
{

super(name);
setHours(hours);
setWages(wages);

}
public void setHours(double hours)
{
hours1=hours;


}
public double getHours( )
{

return hours1;

}
public void setWages(double wages)
{
wages1=wages;

}
public double getWages( )
{

return wages1;
}
public double getPay()
{

if(hours1>40)
{

money1= wages1 * 40 + wages1 * (hours1-40) * 1.5;
}
else
{
money1 = wages1 * hours1;
}
return money1;

}
public String toString( )
{
return String.format("HourWorker:\n%s\n moeny is:%.2f",super.toString(),getPay());
}

}

# 这个是记件工人的类  回复  更多评论   

2007-10-25 12:22 by 张祖旺
public class PieceWorker extends Employee

{
private double commission1;

private double money1;

private int quantity1;

public PieceWorker(String name,double commission,int quantity)
{
super(name);
setCommission(commission);
setQuantity(quantity);
}



public void setCommission(double commission)
{
commission1 = commission;

}
public double setCommission()
{

return commission1;
}

public void setQuantity(int quantity)
{
quantity1 = quantity;

}
public int getQuantity()
{
return quantity1 ;

}

public double getPay()
{
money1 = quantity1 * commission1;
return money1;
}

public String toString( )
{
return String.format("PieceWorker:\n%s\n moeny is:%.2f",super.toString(),getPay());
}

}

# 这个是记提成员工的类  回复  更多评论   

2007-10-25 12:23 by 张祖旺
import java.util.Scanner;
public class CommissionWorker extends Employee
{
double salary1;

double commission1;

int sellValue1;

Scanner input = new Scanner(System.in);
public CommissionWorker(String name,double salary,double commission,int value )
{
super(name);
setSalary(salary);
setCommission(commission);
setSellValue(value);
}

public void setSalary(double salary)
{

salary1 = salary;

}
public double getSalary( )
{

return salary1;

}
public void setCommission(double commision)
{
if(commision>0&&commision<1)
{
commission1 = commision;

}
else
{
while(commision>=1||commision<=0)
{
System.out.println("you must input the commision between 0 to 1");

commision = input.nextDouble( );
}
commission1 = commision;
}

}
public double getCommission( )
{

return commission1;

}


public void setSellValue(int value)
{

sellValue1 = value;

}
public int setSellValue( )
{

return sellValue1;

}
public double getPay( )
{
return commission1 * sellValue1 + salary1;
}

public String toString( )
{
return String.format("CommissionWorker:\n%s\n moeny is:%.2f",super.toString(),getPay());
}

}

# 这个是员工的基类  回复  更多评论   

2007-10-25 12:24 by 张祖旺
public abstract class Employee
{
private String name;

public Employee(String name)
{this.name=name;}; //constructor

public String getName()
{ return name;}
public String toString( )
{
return String.format("name:%s",getName());
}


public abstract double getPay(); //getPay


}



# 这个是main  回复  更多评论   

2007-10-25 12:25 by 张祖旺
import java.util.Scanner;
public class WorkerMoney
{
public static void main(String args[])
{
HourlyWorker h= new HourlyWorker("zhangsan",1,50);
System.out.printf("%s\n",h);
PieceWorker p= new PieceWorker("lisi",1,2);
System.out.printf("%s\n",p);
CommissionWorker c= new CommissionWorker("wangwu",10,0.5,2);
System.out.printf("%s\n",c);

}
}

# 这个是结果  回复  更多评论   

2007-10-25 12:29 by 张祖旺
结果:
HourWorker:
name:zhangsan
money is:55.0
PieceWorker:
name:lisi
money is:2.00
CommmissionWorker:
name:wangwu
money is:11.0

只有注册用户登录后才能发表评论。