長方形的周長面積

import java.util.Scanner;

class Rect {
	private int length;
	private int width;

	public Rect(int length) {
		this(length, length);
	}

	public Rect(int length, int width) {
		if (length < 0) {
			length = 0;
		}
		if (width < 0) {
			width = 0;
		}
		this.length = length;
		this.width = width;
	}

	/*public int getlength() {
		return length;
	}

	public void setlength(int length) {
		if (length < 0) {
			length = 0;
		}
		this.length = length;
	}

	public int getwidth() {
		return width;
	}

	public void setwidth(int width) {
		if (width < 0) {
			width = 0;
		}
		this.width = width;
	}*/

	public int length() {
		return 2 * (length + width);
	}

	public int area() {
		return length * width;
	}

	public String toString() {
		String result = length + " " + width + " " + length() + " " + area();
		return result;
	}
}

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		while (cin.hasNext()) {
			Rect rect;
			String str = cin.nextLine();
			String[] arr = str.split(" ");
			int count = arr.length;
			if (count == 1) {
				int length = Integer.parseInt(arr[0]);
				rect = new Rect(length);
			} else {
              int length=Integer.parseInt(arr[0]);
              int width=Integer.parseInt(arr[1]);
              rect = new Rect(length,width);
			}
			System.out.println(rect.toString());
			
		}
		cin.close();
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章