標籤:java
/**實驗3.4
* 使用上題的矩形類,編程統計若干塊土地的相關資訊
* 由使用者輸入每塊兒土地的長與寬,程式將相關結果輸出
**/
import java.util.*;public class adu{public static void main (String[] args){Rectangle rectangle1 = new Rectangle();Rectangle rectangle2 = new Rectangle();System.out.print("Input length and width of the rectanle1: ");Scanner in = new Scanner (System.in);int rectangle1Length = in.nextInt();int rectangle1Width = in.nextInt();System.out.println("The perimeter of rectangle1 is: " + rectangle1.perimeter(rectangle1Length, rectangle1Width));System.out.println("The area of rectangle1 is: " + rectangle1.area(rectangle1Length, rectangle1Width));System.out.print("Input length and width of the rectanle2: ");int rectangle2Length = in.nextInt();int rectangle2Width = in.nextInt();System.out.println("The perimeter of rectangle2 is: " + rectangle2.perimeter(rectangle2Length, rectangle2Width));System.out.println("The area of rectangle2 is: " + rectangle2.area(rectangle2Length, rectangle2Width));}}class Rectangle{private int length;private int width;Rectangle(int l, int w){this.length = l;this.width = w;}Rectangle (){} int perimeter (int length, int width){return (length + width) * 2;} int area (int length, int width){return length * width;}}
那些年,一起學的Java 3-4