//filename:Rectangular.java
// javac Rectangular.java
public class Rectangular {
public double getDistance() {
double result = 0;
if (this.a == null || this.a <= 0) {
System.out.println("Edge a Invalid");
} else if (this.b == null || this.b <= 0) {
System.out.println("Edge b Invalid");
} else {
result = (a + b) * 2;
}
return result;
}
public double getArea() {
double result = 0;
if (this.a == null || this.a <= 0) {
System.out.println("Edge a Invalid");
} else if (this.b == null || this.b <= 0) {
System.out.println("Edge b Invalid");
} else {
result = a * b;
}
return result;
}
public void setA(double a) {
this.a = a;
}
public double getA() {
return a;
}
public void setB(double b) {
this.b = b;
}
public double getB() {
return b;
}
public Rectangular() {}
public Rectangular(double a, double b) {
this.a = a;
this.b = b;
}
private double a;
private double b;
}
//filename:TestRectangular.java
// 记得导包
public class TestRectangular {
@Test
public void testGetDistance() {
double a, b, distance;
Rectangular r;
a = -1, b = -1;
r = new Rectangular(a, b);
distance = r.getDistance();
assertTrue(distance == 0);
a = -1, b = 0;
r = new Rectangular(a, b);
distance = r.getDistance();
assertTrue(distance == 0);
a = -1, b = 4;
r = new Rectangular(a, b);
distance = r.getDistance();
assertTrue(distance == 0);
a = 8, b = 4;
r = new Rectangular(a, b);
distance = r.getDistance();
assertTrue(distance == 24);
}
@Test
public void testGetArea() {
double a, b, area;
Rectangular r;
a = -1, b = -1;
r = new Rectangular(a, b);
area = r.getArea();
assertTrue(area == 0);
a = -1, b = 0;
r = new Rectangular(a, b);
area = r.getArea();
assertTrue(area == 0);
a = -1, b = 4;
r = new Rectangular(a, b);
area = r.getArea();
assertTrue(area == 0);
a = 8, b = 4;
r = new Rectangular(a, b);
area = r.getArea();
assertTrue(area == 32);
}
}