44 lines
1.1 KiB
Java
44 lines
1.1 KiB
Java
package Exp3.Task1;
|
|
|
|
import static java.lang.Math.abs;
|
|
|
|
public class Rectangle {
|
|
private double lx, ly, rx, ry;
|
|
public Rectangle(double rx, double ry) {
|
|
this.lx = 0;
|
|
this.ly = 0;
|
|
this.rx = rx;
|
|
this.ry = ry;
|
|
}
|
|
public Rectangle(double lx, double ly, double rx, double ry) {
|
|
this.lx = lx;
|
|
this.ly = ly;
|
|
this.rx = rx;
|
|
this.ry = ry;
|
|
}
|
|
public double getAttribute(String attribute) {
|
|
return switch (attribute) {
|
|
case "lx" -> lx;
|
|
case "ly" -> ly;
|
|
case "rx" -> rx;
|
|
case "ry" -> ry;
|
|
default -> 0;
|
|
};
|
|
}
|
|
public void setAttribute(String attribute, double value) {
|
|
switch (attribute) {
|
|
case "lx": lx = value; break;
|
|
case "ly": ly = value; break;
|
|
case "rx": rx = value; break;
|
|
case "ry": ry = value; break;
|
|
default: System.out.println("Invalid attribute"); break;
|
|
}
|
|
}
|
|
public double getC() {
|
|
return 2 * (abs(rx - lx) + abs(ry - ly));
|
|
}
|
|
public double getArea() {
|
|
return abs((rx - lx) * (ry - ly));
|
|
}
|
|
}
|