java类的简单应用-矩形的面积和周长

创建一个简单的表示矩形的Rectangle类,满足以下条件:

1、定义两个成员变量height和width,表示矩形的长和宽,类型为整型 2、定义一个getArea方法,返回矩形的面积 3、定义一个getPerimeter方法,返回矩形的周长 4、在main函数中,利用输入的2个参数分别作为矩形的长和宽,调用getArea和getPermeter方法,计算并返回矩形的面积和周长

输入:
输入2个正整数,中间用空格隔开,分别作为矩形的长和宽,例如:5 8

输出:

输出2个正整数,中间用空格隔开,分别表示矩形的面积和周长,例如:40 26

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
         Rectangle obj=new Rectangle();
         Scanner in=new Scanner(System.in);
         int a,b;
         a=in.nextInt();
         b=in.nextInt();
         obj.setHeight(a);
         obj.setWidth(b);
		System.out.println(obj.getArea()+" "+obj.getPerimeter());
   }
}

class Rectangle{
	private int height;
	private int width;
	
	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getArea() {
		return height*width;
	}
	public int getPerimeter() {
    	return 2*(height+width);
    }
	
}


你可能感兴趣的:(java基础题)