设计一个形状类(接口)Shape,方法:求周长和求面积和某公司的雇员工资明细

本文展示了如何使用Java实现一个形状接口,包括矩形、圆形和正方形类的定义,并通过测试类计算它们的周长和面积。此外,还介绍了员工类及其子类,如月薪员工、小时工和销售员工,实现了根据工作时间和生日计算工资的功能。
摘要由CSDN通过智能技术生成
package demon05;
//接口

public interface Shape {
    //计算面积的方法
    double getArea();

    //计算周长的方法
    double getPerimeter();
}
package demon05;
//设计一个形状类(接口)Shape,方法:求周长和求面积
//形状类
//矩形类
public class Rect implements Shape{
    //宽度
    private double width;
    //高
    private double height; 

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    //一个有参构造,将用于子类正方形
    public Rect(double width){
        this.width = width;
    }

    //有参构造
    public Rect(double width, double height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public double getArea() {
        //面积
        return  width * height;
    }

    @Override
    public double getPerimeter() {
        //周长
        return (width + height) * 2;
    }
}
package demon05;
//设计一个形状类(接口)Shape,方法:求周长和求面积

public class Circle implements Shape{

    private final double PI = 3.14; //圆周率
    private double r; //半径

    public Circle(double r) {
        this.r = r;
    }

    public double getPI() {
        return PI;
    }

    public double getR() {
        return r;
    }

    public void setR(double r) {
        this.r = r;
    }

    @Override
    public double getArea() {
        //面积
        return PI * r * r;
    }

    @Override
    public double getPerimeter() {
        //周长
        return PI * r * 2;
    }
}
package demon05;
//设计一个形状类(接口)Shape,方法:求周长和求面积
//形状类
//正方形类
//继承父类矩形Rect
public class Square extends Rect{

    public Square(double width) {
        super(width);
    }

    @Override
    public double getArea() {
        //面积
        return  getWidth() * getWidth();
    }

    @Override
    public double getPerimeter() {
        //周长
        return getWidth() * 4;
    }
}
package demon05;
//测试类
public class Test {
    public static void main(String[] args) {

        Shape[] shape = new Shape[3];
        shape[0] = new Circle(3);

        System.out.println("------------圆形周长和面积-----------------");
        System.out.println(String.format("%.2f", shape[0].getArea()));
        System.out.println(shape[0].getPerimeter());

        shape[1] = new Rect(4, 8);
        System.out.println("------------矩形周长和面积-----------------");
        System.out.println(shape[1].getArea());
        System.out.println(shape[1].getPerimeter());

        shape[2] = new Square(9);
        System.out.println("------------正方形周长和面积-----------------");
        System.out.println(shape[2].getArea());
        System.out.println(shape[2].getPerimeter());
    }
}

 

某公司的雇员工资明细底薪和加班费

package demon06;
//Employee类
public class Employee {
    //员工姓名
    private String name;
    //生日
    private int month;
    //工资
    private double salary;

    //构造器

    public Employee() {
    }

    public Employee(String name, int month, double salary) {
        this.name = name;
        this.month = month;
        this.salary = salary;
    }

    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 double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public double getSalary(int month){
        //判断是否生日,如果生日额外奖励100元
        if (this.month == month) {
            salary += 100;
        }
        System.out.println("该员工"+getName()+"的工资薪水为:"+salary);
        return salary;
    }
}

 

package demon06;
//SalariedEmployee类
//继承父类Employee
public class SalariedEmployee extends Employee{
    private double salary01;    //工资

    //构造器

    public SalariedEmployee() {
    }

    public SalariedEmployee(double salary01) {
        this.salary01 = salary01;
    }

    public double getSalary01() {
        return salary01;
    }

    public void setSalary01(double salary01) {
        this.salary01 = salary01;
    }

    public double getSalary(int month){
        //判断是否生日,如果生日额外奖励100元
        if (this.getMonth() == month) {
            salary01 += 100;
        }
        System.out.println("该员工"+getName()+"的工资薪水为:"+salary01);
        return salary01;
    }
}
package demon06;
//HourlyEmployee类
//继承父类Employee
public class HourlyEmployee extends Employee{
    private int hour;   //小时
    private double price;   //代价
    private double salary02;    //工资

    //构造器

    public HourlyEmployee() {
    }

    public HourlyEmployee(int hour, double price, double salary02) {
        this.hour = hour;
        this.price = price;
        this.salary02 = salary02;
    }

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getSalary02() {
        return salary02;
    }

    public void setSalary02(double salary02) {
        this.salary02 = salary02;
    }

    @Override
    public double getSalary(int month) {
        //判断工作时间是否超过160小时,超出的部分按照1.5的工资计算
        if (hour <= 160) {
            salary02 = price * hour;
        }else {
            hour = hour - 160;
            salary02 = 160 * price + hour * 1.5 * price;
        }
        //判断是否生日,如果生日额外奖励100元
        if (getMonth() == month) {
            salary02 += 100;
        }
        System.out.println("该员工"+getName()+"的工资薪水为:"+salary02);
        return salary02;
    }
}
package demon06;
//SalesEmployee类
//继承父类Employee
public class SalesEmployee extends  Employee{
    private double salemonth;   //销售额
    private double rate;    //提成率

    //构造器

    public SalesEmployee() {
    }

    public SalesEmployee(double salemonth, double rate) {
        this.salemonth = salemonth;
        this.rate = rate;
    }

    public double getSalemonth() {
        return salemonth;
    }

    public void setSalemonth(double salemonth) {
        this.salemonth = salemonth;
    }

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }

    @Override
    public double getSalary(int month) {
        //判断是否生日,如果生日额外奖励100元
        if (this.getMonth() == month) {
            //如果生日,工资=月销售额*提成率+奖励100
            System.out.println("该员工"+getName()+"的工资薪水为:"+ (100 + salemonth * rate));
            return (salemonth * rate) + 100;
        }else {
            //如果没有生日,工资=月销售额*提成率
            System.out.println("该员工"+getName()+"的工资薪水为:"+ salemonth * rate);
            return salemonth * rate;
        }
    }
}
package demon06;
//BasePlusSalesEmployee类
//继承父类SalesEmployee
public class BasePlusSalesEmployee extends SalesEmployee{
    private double minsalary;   //底薪
    private double salemonth01;     //销售额
    private double rate01;      //提成率

    //构造器

    public BasePlusSalesEmployee() {
    }

    public BasePlusSalesEmployee(double minsalary, double salemonth01, double rate01) {
        this.minsalary = minsalary;
        this.salemonth01 = salemonth01;
        this.rate01 = rate01;
    }

    public double getMinsalary() {
        return minsalary;
    }

    public void setMinsalary(double minsalary) {
        this.minsalary = minsalary;
    }

    public double getSalemonth01() {
        return salemonth01;
    }

    public void setSalemonth01(double salemonth01) {
        this.salemonth01 = salemonth01;
    }

    public double getRate01() {
        return rate01;
    }

    public void setRate01(double rate01) {
        this.rate01 = rate01;
    }

    @Override
    public double getSalary(int month) {
      //  判断是否生日,如果生日额外奖励100元
        if (this.getMonth() == month) {
            //如果生日,工资=底薪+月销售额*提成率+奖励100
            System.out.println("该员工"+getName()+"的工资薪水为:"+ (minsalary + salemonth01 * rate01 + 100));
            return minsalary + salemonth01 * rate01 + 100;
        }else {
            //如果没有生日,工资=底薪+月销售额*提成率
            System.out.println("该员工"+getName()+"的工资薪水为:"+ (minsalary + salemonth01 * rate01));
            return minsalary + salemonth01 * rate01;
        }
    }
}
package demon06;
//测试类
//某公司
public class Test {
    public static void main(String[] args) {
        Employee e1 = new Employee("达达" , 10 , 8500);
        e1.getSalary(10);

        SalariedEmployee s1 = new SalariedEmployee();
        s1.setName("小鲲");
        s1.setSalary01(9000);
        s1.setMonth(2);
        s1.getMonth();
        s1.getSalary(10);

        HourlyEmployee h1 = new HourlyEmployee(213 , 36 , 0);
        h1.setName("小天");
        h1.setMonth(8);
        h1.getMonth();
        h1.getSalary(10);

        SalesEmployee s2 = new SalesEmployee(13000 , 0.8);
        s2.setName("小亮");
        s2.setMonth(10);
        s2.getMonth();
        s2.getSalary(10);

        BasePlusSalesEmployee b1 = new BasePlusSalesEmployee(4500 , 9000 , 0.3);
        b1.setName("小飞");
        b1.setMonth(10);
        b1.getMonth();
        b1.getSalary(10);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值