-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode2.java
More file actions
63 lines (50 loc) · 1.62 KB
/
Code2.java
File metadata and controls
63 lines (50 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.text.DecimalFormat;
interface Resizable {
void resize(double factor);
}
class Circle implements Resizable {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public void resize(double factor) {
radius *= Math.sqrt(factor);
}
public double getRadius() {
return radius;
}
}
class Rectangle implements Resizable {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public void resize(double factor) {
double ratio = Math.sqrt(factor);
width *= ratio;
height *= ratio;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
}
public class Code2 {
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("#.##");
Circle circle = new Circle(5);
System.out.println("Circle Radius: " + df.format(circle.getRadius()));
circle.resize(2);
System.out.println("Resized Circle Radius: " + df.format(circle.getRadius()));
Rectangle rectangle = new Rectangle(4, 6);
System.out.println("Rectangle Width: " + df.format(rectangle.getWidth()) + ", Height: " + df.format(rectangle.getHeight()));
rectangle.resize(1.5);
System.out.println("Resized Rectangle Width: " + df.format(rectangle.getWidth()) + ", Height: " + df.format(rectangle.getHeight()));
}
}