面向对象代码作业

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了面向对象代码作业相关的知识,希望对你有一定的参考价值。

/*
 * * 在前一章作业的基础上,进行修改。在Employee类中添加一个抽象方法以计算薪资总额。
 * 定义一个方法基于休假天数leave计算要从基本薪资中扣除的部分。
 * 计算薪资扣除部分的公式为:lessPay=(leave<=5)?(0.25*basic):(0.5*basic)
 * Manager和Director类重写父类的抽象方法。 计算Manager的薪资总额totalAmount的公式为:
 * totalAmount=basic+houseRentAllowance+dearnessAllowance+medicalAllowance;
 * 其中:houseRentAllowance为basic的8% dearnessAllowance为basic的30% medicalAllowance为1500 
 * 计算Director的薪资总额的公式为:
 * totalAmount=basic+houseRentAllowance+dearnessAllowance+medicalAllowance
 * +entertainmentAllowance+transportAllowance
 * 其中:houseRentAllowance为basic的20% dearnessAllowance为basic的50% medicalAllowance为4500 entertainmentAllowance为5000
 * 该程序还应该计算应付薪资,其公式为:NetPay=totalAmount-lessPay
 * 请在show方法中显示name,address,basic,totalAmount和netPay属性中储存的值。
 * 
 * 
 * */

abstract class Employee {
	double basic;
	String name;
	String address;
	int leave;
	double lessPay;
	double NetPay;
	public Employee() {}
	public Employee(double basic,String name,String address,int leave) {
		this();
		this.basic = basic;
		this.name = name;
		this.address = address;
		this.leave = leave;
		
	}
	public abstract double TotalAmount();
	public double lessPay(){
		 return this.lessPay = (leave <= 5)?(0.25*basic):(0.5*basic);
	}
	public double NetPay(){
		 return this.NetPay = this.TotalAmount() - this.lessPay();
	}
	public void show(){
		System.out.println("name:"+ name+"\t" + "address:"+ address+"\t"+
	"TotalAmount:"+ this.TotalAmount() +"\t" + "NetPay:"+ this.NetPay());
	}
	
	
}

class Manager extends Employee{
	private String department;
	public Manager(){
		super();
	}
	public Manager(String name, double basic, String address, int leave,String department) {
		super(basic, name, address, leave);
		this.department = department;
	}
	public double TotalAmount(){
		double houseRentAllowance = basic * 0.08;
		double dearnessAllowance = basic * 0.3;
		int medicalAllowance = 1500;
		return basic + houseRentAllowance + dearnessAllowance + medicalAllowance;
	}
	
}
class Director extends Employee{
	private double transportAllowance;
	public Director(){
		super();
	}
	public Director(String name, double basic, String address, int leave, double transportAllowance) {
		super(basic, name, address, leave);
		this.transportAllowance = transportAllowance;
	}
	public double TotalAmount(){
		double houseRentAllowance = basic * 0.2;
		double dearnessAllowance = basic * 0.5;
		int medicalAllowance = 4500;
		int entertainmentAllowance = 5000;
		return basic + houseRentAllowance + dearnessAllowance + medicalAllowance
				  + entertainmentAllowance + transportAllowance;
	}
}

public class Pay{
	public static void main(String[] args) {
		Employee a = new Manager("张三",3000,"中国",6,"总部");
		Employee b = new Director("李四",5000,"美国",3,2000);
		a.show();
		b.show();
				
	}
}

  

以上是关于面向对象代码作业的主要内容,如果未能解决你的问题,请参考以下文章

面向对象代码作业

2021面向对象第四单元作业

2017级面向对象程序设计寒假作业第2次成绩排行

面向对象程序设计 作业一

2017级面向对象程序设计寒假作业3

《面向对象程序设计》第一次作业