Java面向对象实验

Posted 康小庄

tags:

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

1.某公司的员工分为5类,每类员工都有相应的封装类,这5个类的信息如下。
(1) Employee:这是所有员工的父类。
① 属性:员工的姓名、员工的生日月份。
②方法: getSolary(in month)根据参数月份确确定工资。如果该月员工过生日,则公司会额外发放100元
(2) SaridEmlye Employee的子类,拿固定工资的员工。
属性:月薪。
(3) HourlyEmployee Employe的子类,按小时拿工资的员工,每月工作超出160h的部分按照1.5倍工资发放。
属性:每小时的工资、每月工作的小时数。
(4) SalesEmployee Employee的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率。
(5) BasePlusSalesEmployee SalesEmployee 的子类,有固定底薪的销售人员,工资为底薪加上销售提成。
属性:底薪。
本题要求根据上述员工分类,编写个程序,实现以下 功能:
(1)创建一个Employee数组,分别创建若F不同的Employee 对象,并打印某个月的工资。
(2)每个类都完全封装,不允许有非私有化属性。
Employee

package com.zhuang.chapter04.work.work1;

public class Employee 
	private String name;

	private int month;

	public String getName() 
		return name;
	

	public void setName(String name) 
		this.name = name;
	

	public int getMonth() 
		return month;
	

	public void setMonth(int month) 
		this.month = month;
	

	public Employee(String name, int month) 
		super();
		this.name = name;
		this.month = month;
	

	public Employee() 
		super();
	

	// 如果该月员工过生日,则公司会额外发放100元
	public int getSalary(int month) 
		int salary=0;
        if(this.month==month)
            salary+=100;
        
        return salary;
	


SaridEmlye

package com.zhuang.chapter04.work.work1;

public class SaridEmlye extends Employee 
	private int salary;

	public SaridEmlye() 
	

	public SaridEmlye(String name, int month, int salary) 
		super(name, month);
		this.salary = salary;
	

	public int getSalary() 
		return salary;
	

	public void setSalary(int salary) 
		this.salary = salary;
	

	@Override
	public int getSalary(int month) 
		return salary + super.getSalary(month);
	


HourlyEmployee

package com.zhuang.chapter04.work.work1;

public class HourlyEmployee extends Employee 
	// 每小时的工资
	private int hourSalary;
	// 每月工作的小时数
	private int workHour;

	public HourlyEmployee(String name, int month, int hourSalary, int workHour) 
		super(name, month);
		this.hourSalary = hourSalary;
		this.workHour = workHour;
	

	public int getHourSalary() 
		return hourSalary;
	

	public void setHourSalary(int hourSalary) 
		this.hourSalary = hourSalary;
	

	public int getWorkHour() 
		return workHour;
	

	public void setWorkHour(int workHour) 
		this.workHour = workHour;
	

	@Override
	public int getSalary(int month) 
		int salary = 0;
		if (this.workHour > 160) 
			salary = (int) ((160 + (this.workHour - 160) * 1.5) * this.hourSalary);
		 else 
			salary = workHour * hourSalary;
		
		return salary + super.getSalary(month);
	


SalesEmployee

package com.zhuang.chapter04.work.work1;

public class SalesEmployee extends Employee 
	// 月销售额
	private int MonthVal;
	// 提成率
	private double decrease;

	public SalesEmployee(String name, int month, int monthVal, double decrease) 
		super(name, month);
		MonthVal = monthVal;
		this.decrease = decrease;
	

	public int getMonthVal() 
		return MonthVal;
	

	public void setMonthVal(int monthVal) 
		MonthVal = monthVal;
	

	public double getDecrease() 
		return decrease;
	

	public void setDecrease(double decrease) 
		this.decrease = decrease;
	

	public int  getSalary(int month) 
		return (int) (MonthVal * decrease + super.getSalary(month));
	


BasePlusSalesEmployee

package com.zhuang.chapter04.work.work1;

public class BasePlusSalesEmployee extends SalesEmployee 
	// 底薪
	private int baseSalary;

	public BasePlusSalesEmployee(String name, int month, int monthVal, double decrease, int baseSalary) 
		super(name, month, monthVal, decrease);
		this.baseSalary = baseSalary;
	

	public int getBaseSalary() 
		return baseSalary;
	

	public void setBaseSalary(int baseSalary) 
		this.baseSalary = baseSalary;
	

	public int getSalary(int month) 
		return baseSalary + super.getSalary(month);
	


EmployeeMain

package com.zhuang.chapter04.work.work1;

public class EmployeeMain 
	public static void main(String[] args) 
		Employee employee1 = new SaridEmlye("李四", 3, 6000);
		Employee employee2 = new HourlyEmployee("王五", 7, 50, 180);
		Employee employee3 = new SalesEmployee("赵六", 11, 6500, 0.2);
		Employee employee4 = new BasePlusSalesEmployee("田七", 4, 5000, 0.3, 2000);
		Employee[] employees = new Employee[]  employee1, employee2, employee3, employee4 ;

		for (Employee employee : employees) 
			System.out.println(employee.getName() + "工资是-->" + employee.getSalary(employee.getMonth()) + "元");
		
	


2.设计一个 Shape 接口和它的两个实现类 Square 和 Circle 。要求如下:
(1)Shape 接口中有一个抽象方法 area (),方法接收有一个 double 类型的参数,返回一个 double 类型的结果。
(2)Square 和 Circle 中实现了 Shape 接口的 area()抽象方法,分别求正方形和圆形的面积并返回。
(3)在测试类Test中创建 Square 和 Circle 对象,计算边长为2的正方形面积和半径为3的圆形面积。
Shape

package com.zhuang.chapter04.work.work2;

public interface Shape 
	abstract double area(double args);


package com.zhuang.chapter04.work.work2;

public class Circle implements Shape 

	@Override
	public double area(double args) 
		// TODO Auto-generated method stub
		// 圆形的方法
		return Math.PI * Math.pow(args, 2);
	



Square

package com.zhuang.chapter04.work.work2;

public class Square implements Shape 

	@Override
	public double area(double args) 
		// TODO Auto-generated method stub
		// 正方形的方法
		return args * args;
	



ShapeMain

package com.zhuang.chapter04.work.work2;

public class ShapeMain 

	public static void main(String[] args) 
		Square square = new Square();
		System.out.println(square.area(2.0));
		Circle circle = new Circle();
		System.out.println(circle.area(2));
	



3.通过一个计算圆的面积的实例,具体要求如下:
(1)定义一个父类Area,该类中有getarea方法根据输入的半径用来计算圆的面积。
(2)定义子类Circle,该子类在继承父类已有功能的基础上覆盖了getarea方法,返回半圆的面积。
(3)定义主类AreaDemo,在主类AreaDemo中分别将父类和子类实例化,分别输出圆和半圆的面积。

package com.zhuang.chapter04.work.work3;

public class Area 
	public double getarea(double radius) 
		return Math.PI * Math.pow(radius, 2);
	


Circle

package com.zhuang.chapter04.work.work3;

public class Circle extends Area 
	@Override
	public double getarea(double radius) 
		// TODO Auto-generated method stub
		return super.getarea(radius) / 2;
	


AreaDemo

package com.zhuang.chapter04.work.work3;

public class AreaDemo 
	public static void main(String[] args) 
		Area area = new Area();
		Circle circle = new Circle();
		System.out.println(area.getarea(2));
		System.out.println(circle.getarea(2));
	


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

20165336 实验二 Java面向对象程序设计

20172304 实验二 《Java面向对象程序设计》 实验报告

实验二 Java面向对象程序设计

20165323 实验二 Java面向对象程序设计

20165332实验二 Java面向对象程序设计

2017-2018-2 20165231实验二《Java面向对象程序设计》实验报告