第九次Java作业
- 3、设计三个类,分别如下:(知识点:抽象类及抽象方法) [必做题]
- 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方法
-
1234567891011121314
package
test;
public
class
Shape {
double
area;
double
per;
char
color;
public
Shape() {
super
();
}
public
Shape(
char
color) {
super
();
this
.color = color;
}
}
12345678910111213141516171819202122232425262728293031package
test;
public
class
Rectangle
extends
Shape {
public
double
getPer() {
// TODO Auto-generated method stub
return
(width + height) *
2
;
}
public
double
getArea() {
// TODO Auto-generated method stub
return
width * height;
}
double
width;
double
height;
public
Rectangle(
double
width,
double
height,
char
color) {
super
();
this
.width = width;
this
.height = height;
this
.color = color;
}
public
Rectangle() {
super
();
}
public
void
showAll() {
System.out.println(
"矩形的长是"
+ width +
",宽是"
+ height +
",颜色是"
+ color +
",周长是"
+ getPer() +
",面积是"
+ getArea());
}
}
1234567891011121314151617181920212223package
test;
public
class
Circle
extends
Shape{
double
radius;
public
void
showAll(){
System.out.println(
"圆形的半径是"
+radius+
",颜色是"
+color+
",周长是"
+getPer()+
",面积是"
+getArea());
}
public
double
getPer() {
// TODO Auto-generated method stub
return
2
*
3.14
*radius;
}
public
double
getArea() {
// TODO Auto-generated method stub
return
3.14
*radius*radius;
}
public
Circle(
double
radius,
char
color) {
super
();
this
.radius = radius;
this
.color=color;
}
}
1234567891011121314package
test;
public
class
Test {
public
static
void
main(String[] args) {
// TODO Auto-generated method stub
Rectangle r =
new
Rectangle(
2
,
3
,
'黑'
);
r.showAll();
Circle c =
new
Circle(
4
,
'红'
);
c.showAll();
}
}
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类
-
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
package
test;
public
class
ColaEmployee {
protected
String name;
protected
int
month;
public
ColaEmployee() {
super
();
}
public
ColaEmployee(String name,
int
month) {
super
();
this
.name = name;
this
.month = month;
}
public
int
getMonth() {
return
month;
}
public
double
getSalary(
int
month) {
return
0
;
}
}
package
test;
public
class
ColaEmployee {
protected
String name;
protected
int
month;
public
ColaEmployee() {
super
();
}
public
ColaEmployee(String name,
int
month) {
super
();
this
.name = name;
this
.month = month;
}
public
int
getMonth() {
return
month;
}
public
double
getSalary(
int
month) {
return
0
;
}
}
12345678910111213141516171819202122232425262728293031323334package
test;
public
class
HourlyEmployee
extends
ColaEmployee {
protected
int
hourmoney;
protected
int
daymonth;
public
HourlyEmployee() {
super
();
}
public
HourlyEmployee(String name,
int
month,
int
hourmoney,
int
daymonth) {
super
(name, month);
this
.hourmoney = hourmoney;
this
.daymonth = daymonth;
}
public
double
getSalary(
int
month) {
if
(
super
.month == month) {
if
(daymonth >
160
) {
return
hourmoney *
160
+ hourmoney * (daymonth -
160
) *
1.5
+
100
;
}
else
{
return
hourmoney * daymonth +
100
;
}
}
else
{
if
(daymonth >
160
) {
return
hourmoney *
160
+ hourmoney * (daymonth -
160
) *
1.5
;
}
else
{
return
hourmoney * daymonth;
}
}
}
}
1234567891011121314151617181920212223package
test;
public
class
SalariedEmployee
extends
ColaEmployee {
protected
double
money;
public
SalariedEmployee() {
super
();
}
public
SalariedEmployee(String name,
int
month,
double
money) {
super
(name, month);
this
.money = money;
}
public
double
getSalary(
int
month) {
if
(
super
.month == month) {
return
money +
100
;
}
else
{
return
money;
}
}
}
12345678910111213141516171819202122232425package
test;
public
class
SalesEmployee
extends
ColaEmployee {
protected
int
monthsales;
protected
double
rate;
public
SalesEmployee() {
super
();
}
public
SalesEmployee(String name,
int
month,
int
monthsales,
double
rate) {
super
(name, month);
this
.monthsales = monthsales;
this
.rate = rate;
}
public
double
getSalary(
int
month) {
if
(
super
.month == month) {
return
monthsales * rate +
100
;
}
else
{
return
monthsales * rate;
}
}
}
1234567package
test;
public
class
Company {
public
void
getSalary(ColaEmployee c,
int
month) {
System.out.println(c.name +
"在"
+ month +
"月的月薪为"
+ c.getSalary(month) );
}
}
1234567891011121314package
test;
public
class
Test {
public
static
void
main(String[] args) {
// TODO Auto-generated method stub
ColaEmployee c[] = {
new
SalariedEmployee(
"salar"
,
3
,
15000
),
new
HourlyEmployee(
"hourly"
,
5
,
20
,
200
),
new
SalesEmployee(
"sales"
,
8
,
10000
,
0.6
) };
for
(
int
i =
0
; i < c.length; i++) {
new
Company().getSalary(c[i],
6
);
}
}
}