Java第九次作业
3、设计三个类,分别如下:(知识点:抽象类及抽象方法)[必做题]
•3.1设计Shape表示图形类,有面积属性area、周长属性per,颜色属性color,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、getPer计算周长、showAll输出所有信息,还有一个求颜色的方法getColor。
•3.2设计2个子类:
•3.2.1 Rectangle表示矩形类,增加两个属性,Width表示长度、height表示宽度,重写getPer、getArea和showAll三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。
•3.2.2 Circle表示圆类,增加1个属性,radius表示半径,重写getPer、getArea和showAll三个方法,另外又增加两个构造方法(为半径、颜色赋值的)。
•3.3在main方法中,声明创建每个子类的对象,并调用2个子类的showAll方法。
1 package test2; 2 3 4 5 public abstract class shape { 6 7 double area; 8 9 double per; 10 11 String color; 12 13 public shape() { 14 15 super(); 16 17 } 18 19 public shape(String color) { 20 21 super(); 22 23 this.color = color; 24 25 } 26 27 public abstract double getarea(); 28 29 public abstract double getper(); 30 31 public abstract void showall(); 32 33 34 35 public String getcolor() { 36 37 return color; 38 39 40 41 } 42 43 } 44 45 public class rectangle extends shape { 46 47 double width; 48 49 double heigth; 50 51 public rectangle() { 52 53 super(); 54 55 } 56 57 public rectangle(String color, double width, double heigth) { 58 59 super(color); 60 61 this.width = width; 62 63 this.heigth = heigth; 64 65 } 66 67 public double getarea() { 68 69 double area=width*heigth; 70 71 return area; 72 73 } 74 75 public double getper() { 76 77 double per=(width+heigth)*2; 78 79 return per; 80 81 } 82 83 public void showall() { 84 85 System.out.println("长"+heigth); 86 87 System.out.println("宽"+width); 88 89 System.out.println("面积"+getarea()); 90 91 System.out.println("周长"+getper()); 92 93 System.out.println("颜色"+color); 94 95 } 96 97 } 98 99 100 101 102 103 public class circle extends shape { 104 105 double radius; 106 107 108 109 public circle(String color, double radius) { 110 111 super(color); 112 113 this.radius = radius; 114 115 } 116 117 118 119 public circle() { 120 121 super(); 122 123 } 124 125 126 127 public double getper() { 128 129 double per=2*3.14*radius; 130 131 return per; 132 133 } 134 135 public double getarea() { 136 137 double area=3.14*radius*radius; 138 139 return area; 140 141 } 142 143 public void showall() { 144 145 System.out.println("半径"+radius); 146 147 System.out.println("面积"+getarea()); 148 149 System.out.println("周长"+getper()); 150 151 } 152 153 154 155 } 156 157 public class A { 158 159 160 161 public static void main(String[] args) { 162 163 // TODO Auto-generated method stub 164 165 shape t=new rectangle("红色",2,3); 166 167 shape t1=new circle("白色",3); 168 169 t.showall(); 170 171 t1.showall(); 172 173 } 174 175 176 177 }
3、 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 数组里,并单元出数组中每个员工当月的工资。资的员工。属性:月薪
1 package test3; 2 3 4 5 public abstract class colaemployee { 6 7 String name; 8 9 int birthday; 10 11 12 13 public colaemployee() { 14 15 super(); 16 17 } 18 19 20 21 public colaemployee(String name, int birthday) { 22 23 super(); 24 25 this.name = name; 26 27 this.birthday = birthday; 28 29 } 30 31 32 33 public abstract double getsalary(int month); 34 35 36 37 } 38 39 package test3; 40 41 42 43 public class salariedemployee extends colaemployee { 44 45 double salary; 46 47 48 49 public salariedemployee() { 50 51 super(); 52 53 } 54 55 56 57 public salariedemployee(String name, int birthday, double salary) { 58 59 super(name, birthday); 60 61 this.salary = salary; 62 63 } 64 65 public double getsalary(int month) { 66 67 if(birthday==month) { 68 69 salary=salary+100; 70 71 } 72 73 return salary; 74 75 } 76 77 78 79 } 80 81 package test3; 82 83 84 85 public class hourlyemployee extends colaemployee{ 86 87 double gz; 88 89 double time; 90 91 public hourlyemployee() { 92 93 super(); 94 95 } 96 97 public hourlyemployee(String name, int birthday, double gz, double time) { 98 99 super(name, birthday); 100 101 this.gz = gz; 102 103 this.time = time; 104 105 } 106 107 public double getsalary(int month) { 108 109 double salary=0; 110 111 if(time>160) { 112 113 salary=gz*time+(time-160)*gz*1.5; 114 115 }else 116 117 if(time<=160){ 118 119 salary=gz*time; 120 121 } 122 123 if(birthday==month) { 124 125 salary=salary+100; 126 127 } 128 129 return salary; 130 131 } 132 133 134 135 } 136 137 package test3; 138 139 140 141 public class salaryemployee extends colaemployee{ 142 143 int moon; 144 145 double commission; 146 147 public salaryemployee() { 148 149 super(); 150 151 } 152 153 public salaryemployee(String name, int birthday, int moon, double commission) { 154 155 super(name, birthday); 156 157 this.moon = moon; 158 159 this.commission = commission; 160 161 } 162 163 public double getsalary(int month) { 164 165 double salary=0; 166 167 salary=moon*commission; 168 169 if(birthday==month) { 170 171 salary=salary+100; 172 173 } 174 175 return salary; 176 177 } 178 179 180 181 } 182 183 package test3; 184 185 186 187 public class company { 188 189 public void showsalary(colaemployee test,int month) { 190 191 System.out.println("员工:"+test.name+"\n月份:"+month+"\n工资状况:"+test.getsalary(month)); 192 193 } 194 195 } 196 197 package test3; 198 199 200 201 public class testcompany { 202 203 204 205 public static void main(String[] args) { 206 207 // TODO Auto-generated method stub 208 209 colaemployee[]t=new colaemployee[3]; 210 211 t[0]=new salaryemployee("gg",10,3500,0.3); 212 213 t[1]=new hourlyemployee("cc",5,200,300); 214 215 t[2]=new salariedemployee("qq",40,4000); 216 217 company f=new company(); 218 219 f.showsalary(t[0], 5); 220 221 f.showsalary(t[1], 3); 222 223 f.showsalary(t[1], 4); 224 225 f.showsalary(t[2], 2); 226 227 f.showsalary(t[2], 10); 228 229 230 231 232 233 } 234 235 236 237 }