设计一个矩形类,有长和宽两个属性.java
1.设计一个矩形类,有长和宽两个属性,能够计算每一个矩形的面积和周长;再编写一个主类,在其主方法main中创建一个具体的矩形,求出该矩形的周长和面积。...
1. 设计一个矩形类,有长和宽两个属性,能够计算每一个矩形的面积和周长;再编写一个主类,在其主方法main中创建一个具体的矩形,求出该矩形的周长和面积。
展开
2个回答
展开全部
// 矩形
public class RectangleDemo {
public static void main(String[] args) {
RectangleDemo demo = new RectangleDemo(12, 32);
System.out.println(demo.getPerimeter());
System.out.println(demo.getArea());
demo = new RectangleDemo();
System.out.println(demo.getArea());
System.out.println(demo.getPerimeter());
demo.setHeight(50);
demo.setWidth(30);
System.out.println(demo.getArea());
System.out.println(demo.getPerimeter());
}
// 求周长
public double getPerimeter() {
return (height + width) * 2;
}
// 求面积
public double getArea() {
return height * width;
}
public RectangleDemo(double height, double width) {
this.height = height;
this.width = width;
}
public RectangleDemo() {
this.height = 10;
this.width = 10;
}
private double height;// 高度
private double width;// 宽度
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
}
展开全部
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Rectangle {
//矩形的宽
static double width;
//矩形的长
static double length;
//矩形周长
double circumference;
//矩形面积
double area;
public double circumference(double width,double length){
circumference =2* width + 2*length;
return circumference;
}
public double area(double width,double length){
area = width*length;
return area;
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建矩形的对象
Rectangle rectangle = new Rectangle();
//带缓冲区的读取器
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
//输入矩形的长
System.out.println("请输入矩形的长:");
length =Double.parseDouble(br.readLine());
//输入矩形的宽
System.out.println("请输入矩形的宽:");
width = Double.parseDouble(br.readLine()) ;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("矩形的周长为:" + rectangle.circumference(width, length));
System.out.println("矩形的面积为:" + rectangle.area(width, length));
}
}
import java.io.IOException;
import java.io.InputStreamReader;
public class Rectangle {
//矩形的宽
static double width;
//矩形的长
static double length;
//矩形周长
double circumference;
//矩形面积
double area;
public double circumference(double width,double length){
circumference =2* width + 2*length;
return circumference;
}
public double area(double width,double length){
area = width*length;
return area;
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建矩形的对象
Rectangle rectangle = new Rectangle();
//带缓冲区的读取器
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
//输入矩形的长
System.out.println("请输入矩形的长:");
length =Double.parseDouble(br.readLine());
//输入矩形的宽
System.out.println("请输入矩形的宽:");
width = Double.parseDouble(br.readLine()) ;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("矩形的周长为:" + rectangle.circumference(width, length));
System.out.println("矩形的面积为:" + rectangle.area(width, length));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |