求,用java编写一个求圆、长方形、正方形的面积和周长的程序((要求使用类、对象的方式实现) 50
2个回答
展开全部
class ChangFangXing{
private int length;
private int width;
public ChangFangXing(){}
public void setLength(int length){
this.length = length;
}
public void setWidth(int width){
this.width = width;
}
//求周长
public int getZhouChang(){
return (length + width)*2;
}
public int getArea(){
return length * width;
}
}
class ChangFangXingDemo{
public static void main(String[] args){
ChangFangXing s = new ChangFangXing();
s.setLength(5);
s.setWidth(6);
System.out.println("长方形的周长为:"+s.getZhouChang());
System.out.println("长方形的面积为:"+s.getArea());
}
}
private int length;
private int width;
public ChangFangXing(){}
public void setLength(int length){
this.length = length;
}
public void setWidth(int width){
this.width = width;
}
//求周长
public int getZhouChang(){
return (length + width)*2;
}
public int getArea(){
return length * width;
}
}
class ChangFangXingDemo{
public static void main(String[] args){
ChangFangXing s = new ChangFangXing();
s.setLength(5);
s.setWidth(6);
System.out.println("长方形的周长为:"+s.getZhouChang());
System.out.println("长方形的面积为:"+s.getArea());
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询