Java第十五周作业

 


 1 import java.util.Scanner;
 2 
 3 public class test2 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         
11                 garderner g = new garderner();
12                 g.creat();
13             }
14 
15         }
16         class apple implements fruit{
17          public apple()
18          {
19          System.out.println("种了一个苹果");
20         }
21          }
22         class banana implements fruit{
23              public banana()
24              {
25              System.out.println("种了一个香蕉");
26             }
27              }
28         class putao implements fruit{
29              public putao()
30              {
31              System.out.println("种了一个葡萄");
32             }
33              }
34         class garderner{
35             private Scanner input;
36 
37             public fruit creat() {
38                 fruit f = null;
39                 input = new Scanner(System.in);
40                 String name = input.next();
41                 if(name.equals("苹果")) {
42                     f = (fruit) new apple();
43                 }
44                 if(name.equals("香蕉")) {
45                     f = (fruit) new banana();
46                 }
47                 if(name.equals("葡萄")) {
48                     f = (fruit) new putao();
49                 }
50                 return f;
51             }
52             
53         }
54         interface fruit{
55             
56         
57     }

 

 1 package sg.fgj.fhj;
 2 
 3 public class test {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         
11                 ColaEmployee TestCompany[] = new ColaEmployee[4];
12                 TestCompany[0] = new SalariedEmployee("A",766,700,30);
13                 TestCompany[1] = new HourlyEmployee("B",62,6,60,10);
14                 TestCompany[2] = new HourlyEmployee("C",79,40,80,10);
15                 TestCompany[3] = new SalesEmployee("D",6,6,10,0.3);
16             }
17 
18         }
19         class  ColaEmployee{
20             String name;
21             int birthdaymonth;
22             int mongth;
23             
24             public ColaEmployee(String name, int birthdaymonth, int mongth) {
25                 super();
26                 this.name = name;
27                 this.birthdaymonth = birthdaymonth;
28                 this.mongth = mongth;
29             }
30 
31             void getSalary(int month) {
32                 
33                 }
34             }
35 
36         class SalariedEmployee extends ColaEmployee{
37            int sale ;
38 
39         public SalariedEmployee(String name, int birthdaymonth, int mongth, int sale) {
40             super(name, birthdaymonth, mongth);
41             this.sale = sale;
42             if (super.birthdaymonth==super.mongth) {
43                 sale = sale+100;
44                 System.out.println(name+"员工的工资为:"+sale);
45             } else {
46                System.out.println(name+"员工的工资为:"+sale);
47             }
48         }
49 
50         }
51         class HourlyEmployee extends ColaEmployee{
52            double sale ;
53            int hoursale;
54            int hour;
55         public HourlyEmployee(String name, int birthdaymonth, int mongth, double hoursale, int hour) {
56             super(name, birthdaymonth, mongth);
57             
58             this.hoursale = (int) hoursale;
59             this.hour = hour;
60             if (super.birthdaymonth==super.mongth&&hour<160) {
61                 sale = hoursale*hour+100;
62                 System.out.println(name+"员工的工资为:"+sale);
63             }
64             if (super.birthdaymonth==super.mongth&&hour>160) {
65                 sale = hoursale*160+1.5*(hour-160)+100;
66                 System.out.println(name+"员工的工资为:"+sale);
67             } 
68             if (super.birthdaymonth!=super.mongth&&hour<160) {
69                 sale = hoursale*hour;
70                 System.out.println(name+"员工的工资为:"+sale);
71             }
72             if (super.birthdaymonth!=super.mongth&&hour>160) {
73                 sale = hoursale*160+1.5*(hour-160)+100;
74                 System.out.println(name+"员工的工资为:"+sale);
75             } 
76         }
77          
78         }
79         class  SalesEmployee extends ColaEmployee{ 
80             int monthmonye;
81             double tichenglv;
82             int sale;
83             public SalesEmployee(String name, int birthdaymonth, int mongth, int monthmonye, double tichenglv) {
84                 super(name, birthdaymonth, mongth);
85                 this.monthmonye = monthmonye;
86                 this.tichenglv = tichenglv;
87                 
88                 if (super.birthdaymonth==super.mongth) {
89                     sale = (int) (monthmonye*tichenglv+100);
90                     System.out.println(name+"员工的工资为:"+sale);
91                 } else {
92                     sale = (int) (monthmonye*tichenglv);
93                    System.out.println(name+"员工的工资为:"+sale);
94                 }
95             }
96             
97             
98     }

 

 

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 数组里,并单元出数组中每个员工当月的
工资。

 

 

 

课后作业
5、利用接口实现动态的创建对象[选做题]
5.1 创建4个类:
• 苹果
• 香蕉
• 葡萄
• 园丁
5.2 在三种水果的构造方法中打印一句话.
• 以苹果类为例
class apple
{
public apple()
{
System.out.println(―创建了一个苹果类的对象‖);
}
}
课后作业
• 类图如下:
5.3 要求从控制台输入一个字符串,根据字符串的
值来判断创建三种水果中哪个类的对象

 

posted @ 2021-06-19 13:31  董澳  阅读(33)  评论(0编辑  收藏  举报