/**实验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; } }
时间: 2024-10-14 00:20:46