以下是矩形测试主类,请不要修改main方法也不要在主类中添加其他方法。你只需要复制主类代码并补充其他类的定义,使其通过主类的测试。详见main方法代码及注释。
输入格式:
有n组测试用例。 每组测试用例从标准输入读取一行四个整数到w1,h1,w2,h2,分别表示两个矩形的宽和高。
输出格式:
对每一组输入,在一行中分别输出该矩形的周长和面积以及判断两个对象是否相等。 请参考main方法代码及样例输出。
输入样例:
在这里给出一组输入。例如:
10 20 30 40
输出样例:
在这里给出相应的输出。例如:
Rectangle(10,20):Perimeter= 60 Area= 200 Rectangle(30,40):Perimeter= 140 Area= 1200 Equals=false
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int w1 = sc.nextInt();
int h1 = sc.nextInt();
int w2 = sc.nextInt();
int h2 = sc.nextInt();
Rectangle r1 = new Rectangle(w1, h1);
Rectangle r2 = new Rectangle();
r2.setWidth(w2);
r2.setHeight(h2);
System.out.printf(r1 + ":Perimeter=%6d\t", r1.getPerimeter());
System.out.printf("Area=%6d\t", r1.getArea());
System.out.printf(r2 + ":Perimeter=%6d\t", r2.getPerimeter());
System.out.printf("Area=%6d\t", r2.getArea());
System.out.printf("Equals=%b\n", r1.equals(r2));
}
}
}
class Rectangle{
private int width;
private int height;
private int perimeter;
private int area;
public Rectangle() {
}
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public int getPerimeter() {
perimeter = 2*(width+height);
return perimeter;
}
public int getArea() {
area = width*height;
return area;
}
@Override
public String toString() {
return "Rectangle(" +
+ width +
"," + height +
')';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Rectangle rectangle = (Rectangle) o;
return width == rectangle.width &&
height == rectangle.height &&
perimeter == rectangle.perimeter &&
area == rectangle.area;
}
}