版权声明:文章都是原创,转载请注明~~~~ https://blog.csdn.net/SourDumplings/article/details/88391855
创建一个简单的表示矩形的Rectangle类,满足以下条件:
1、定义两个成员变量height和width,表示矩形的长和宽,类型为整型 2、定义一个getArea方法,返回矩形的面积 3、定义一个getPerimeter方法,返回矩形的周长 4、在main函数中,利用输入的2个参数分别作为矩形的长和宽,调用getArea和getPermeter方法,计算并返回矩形的面积和周长
输入:
输入2个正整数,中间用空格隔开,分别作为矩形的长和宽,例如:5 8
输出:
输出2个正整数,中间用空格隔开,分别表示矩形的面积和周长,例如:40 26
Java:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Rectangle r = new Rectangle();
Scanner sc = new Scanner(System.in);
r.height = sc.nextInt();
r.width = sc.nextInt();
sc.close();
System.out.println(r.getArea() + " " + r.getPerimeter());
return;
}
}
class Rectangle
{
int height, width;
public int getArea()
{
return height * width;
}
public int getPerimeter()
{
return 2 * (height + width);
}
}