Java第十次作业

发布时间 2023-06-09 19:23:19作者: coldlane
  • 设计三个类,分别如下:(知识点:抽象类及抽象方法) [必做题]
  • 3.1 设计Shape表示图形类,有面积属性area、周长属性per,颜色属性clr,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、getPer计算周长、shwAll输出所有信息,还有一个求颜色的方法getClr。
  • 3.2 设计 2个子类:
  • 3.2.1 Rectangle表示矩形类,增加两个属性,Width表示长度、height表示宽度,重写getPer、getArea和shwAll三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。
  • 3.2.2 Circle表示圆类,增加1个属性,radius表示半径,重写getPer、getArea和shwAll三个方法,另外又增加两个构造方法(为半径、颜色赋值的)。
  • 3.3 在main方法中,声明创建每个子类的对象,并调用2个子类的shwAll方法
     1 package test;
     2  
     3 public class Shape {
     4     double area;
     5     double per;
     6     char color;
     7     public Shape() {
     8         super();
     9     }
    10     public Shape(char color) {
    11         super();
    12         this.color = color;
    13     }
    14 }
     1 package test;
     2  
     3 public class Rectangle extends Shape {
     4     public double getPer() {
     5         // TODO Auto-generated method stub
     6         return (width + height) * 2;
     7     }
     8  
     9     public double getArea() {
    10         // TODO Auto-generated method stub
    11         return width * height;
    12     }
    13  
    14     double width;
    15     double height;
    16  
    17     public Rectangle(double width, double height, char color) {
    18         super();
    19         this.width = width;
    20         this.height = height;
    21         this.color = color;
    22     }
    23  
    24     public Rectangle() {
    25         super();
    26     }
    27     public void showAll() {
    28         System.out.println("矩形的长是" + width + ",宽是" + height + ",颜色是" + color + ",周长是" + getPer() + ",面积是" + getArea());
    29     }
    30  
    31 }
    32 
    33 
    34     package test;
    35  
    36 public class Circle extends Shape{
    37     double radius;
    38  
    39     public void showAll(){
    40         System.out.println("圆形的半径是"+radius+",颜色是"+color+",周长是"+getPer()+",面积是"+getArea());
    41     }
    42     public double getPer() {
    43         // TODO Auto-generated method stub
    44         return 2*3.14*radius;
    45     }
    46     public double getArea() {
    47         // TODO Auto-generated method stub
    48         return 3.14*radius*radius;
    49     }
    50     public Circle(double radius,char color) {
    51         super();
    52         this.radius = radius;
    53         this.color=color;
    54     }
    55  
    56 }
    57 
    58 
    59     package test;
    60  
    61 public class Test {
    62  
    63     public static void main(String[] args) {
    64         // TODO Auto-generated method stub
    65         Rectangle r = new Rectangle(2, 3, '黑');
    66         r.showAll();
    67         Circle c = new Circle(4, '红');
    68         c.showAll();
    69  
    70     }
    71  
    72 }
    73 
    74  

     

    4、 Cola 公司的雇员分为以下若干类:(知识点:多态)[必做题]

    ·4.1 ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法: getSalary ( int month )根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。

    4.2 SalariedEmployee :ColaEmployee 的子类,拿固定工

    4.3 HourlyEmployee : ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。属性:每小时的工资、每月工作的小时数

    ·4.4 SalesEmployee : ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率.4.5定义一个类 Company ,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类 TestCompany ,在 main法,把若干各种类型的员工在一个 ColaEmployee 数组里,并单元出数组中每个员工当月的工资。资的员工。属性:月薪

    //ColaEmployee类

      1 package test;
      2  
      3 public class ColaEmployee {
      4     protected String name;
      5     protected int month;
      6  
      7     public ColaEmployee() {
      8         super();
      9     }
     10  
     11     public ColaEmployee(String name, int month) {
     12         super();
     13         this.name = name;
     14         this.month = month;
     15     }
     16  
     17     public int getMonth() {
     18         return month;
     19     }
     20  
     21     public double getSalary(int month) {
     22         return 0;
     23     }
     24 }package test;
     25  
     26 public class ColaEmployee {
     27     protected String name;
     28     protected int month;
     29  
     30     public ColaEmployee() {
     31         super();
     32     }
     33  
     34     public ColaEmployee(String name, int month) {
     35         super();
     36         this.name = name;
     37         this.month = month;
     38     }
     39  
     40     public int getMonth() {
     41         return month;
     42     }
     43  
     44     public double getSalary(int month) {
     45         return 0;
     46     }
     47 }
     48 
     49 
     50 package test;
     51  
     52 public class HourlyEmployee extends ColaEmployee {
     53     protected int hourmoney;
     54     protected int daymonth;
     55  
     56     public HourlyEmployee() {
     57         super();
     58     }
     59  
     60     public HourlyEmployee(String name, int month, int hourmoney, int daymonth) {
     61         super(name, month);
     62         this.hourmoney = hourmoney;
     63         this.daymonth = daymonth;
     64     }
     65  
     66     public double getSalary(int month) {
     67         if (super.month == month) {
     68             if (daymonth > 160) {
     69                 return hourmoney * 160 + hourmoney * (daymonth - 160) * 1.5
     70                         + 100;
     71             } else {
     72                 return hourmoney * daymonth + 100;
     73             }
     74         } else {
     75             if (daymonth > 160) {
     76                 return hourmoney * 160 + hourmoney * (daymonth - 160) * 1.5;
     77             } else {
     78                 return hourmoney * daymonth;
     79             }
     80         }
     81     }
     82  
     83 }
     84 
     85 
     86 package test;
     87  
     88 public class SalariedEmployee extends ColaEmployee {
     89     protected double money;
     90  
     91     public SalariedEmployee() {
     92         super();
     93     }
     94  
     95     public SalariedEmployee(String name, int month, double money) {
     96         super(name, month);
     97         this.money = money;
     98     }
     99  
    100     public double getSalary(int month) {
    101         if (super.month == month) {
    102             return money + 100;
    103         } else {
    104             return money;
    105         }
    106     }
    107  
    108 }
    109 
    110 
    111     package test;
    112  
    113 public class SalesEmployee extends ColaEmployee {
    114     protected int monthsales;
    115     protected double rate;
    116  
    117     public SalesEmployee() {
    118         super();
    119     }
    120  
    121     public SalesEmployee(String name, int month, int monthsales, double rate) {
    122         super(name, month);
    123         this.monthsales = monthsales;
    124         this.rate = rate;
    125     }
    126  
    127     public double getSalary(int month) {
    128         if (super.month == month) {
    129             return monthsales * rate + 100;
    130         } else {
    131             return monthsales * rate;
    132         }
    133     }
    134  
    135 }
    136 
    137 
    138 package test;
    139  
    140 public class Company {
    141     public void getSalary(ColaEmployee c, int month) {
    142         System.out.println(c.name + "在" + month + "月的月薪为" + c.getSalary(month) );
    143     }
    144 }
    145 
    146 
    147 package test;
    148  
    149 public class Test {
    150  
    151     public static void main(String[] args) {
    152         // TODO Auto-generated method stub
    153         ColaEmployee c[] = { new SalariedEmployee("salar", 3, 15000), new HourlyEmployee("hourly", 5, 20, 200),
    154                 new SalesEmployee("sales", 8, 10000, 0.6) };
    155         for (int i = 0; i < c.length; i++) {
    156             new Company().getSalary(c[i], 6);
    157  
    158         }
    159     }
    160 }