public
class
Rectangle{
private
double
length;//長
private
double
width;//寬
public
Rectangle(double
length,
double
width)
{
super();
this.length
=
length;
this.width
=
width;
}
public
double
getLength()
{
return
length;
}
public
void
setLength(double
length)
{
this.length
=
length;
}
public
double
getWidth()
{
return
width;
}
public
void
setWidth(double
width)
{
this.width
=
width;
}
/**
*
計算面積
*
@param
length
*
@param
width
*
@return
*/
public
double
area(double
length,double
width){
return
(length*width);
}
/**
*
計算周長
*
@param
length
*
@param
width
*
@return
*/
public
double
permcter(double
length,double
width){
return
(length+width)*2;
}
public
static
void
main(String[]
args)
{
Rectangle
rectangle
=
new
Rectangle(6,
5);
System.out.println("長為6,寬為5的長方形的面積為:"+rectangle.area(6,
5));
System.out.println("長為6,寬為5的長方形的周長為:"+rectangle.permcter(6,
5));
}
}
4-2:
public
class
Fan{
private
String
speed;
private
boolean
on;
private
String
radius;
public
Fan()
{
}
public
String
getSpeed()
{
return
speed;
}
public
void
setSpeed(String
speed)
{
this.speed
=
speed;
}
public
boolean
isOn()
{
return
on;
}
public
void
setOn(boolean
on)
{
this.on
=
on;
}
public
String
getRadius()
{
return
radius;
}
public
void
setRadius(String
radius)
{
this.radius
=
radius;
}
public
static
void
main(String[]
args)
{
Fan
fan
=
new
Fan();
fan.setOn(true);
fan.setSpeed("122");
System.out.println("fan's
on"+fan.isOn());
System.out.println("fan's
speed"
+
fan.getSpeed());
}
}