-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritanceShapes.java
More file actions
112 lines (93 loc) · 2.83 KB
/
Copy pathInheritanceShapes.java
File metadata and controls
112 lines (93 loc) · 2.83 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
public class InheritanceShapes {
private double width; // these are
private double height; // now private
// A default constructor.
InheritanceShapes() {
width = height = 0.0;
}
// Parameterized constructor.
InheritanceShapes(double w, double h) {
width = w;
height = h;
}
// Construct object with equal width and height.
InheritanceShapes(double x) {
width = height = x;
}
// Accessor methods for width and height.
double getWidth() { return width; }
double getHeight() { return height; }
void setWidth(double w) { width = w; }
void setHeight(double h) { height = h; }
void showDim() {
System.out.println("Width and height are " + width + " and " + height + ".");
}
}
// A subclass of TwoDShape for triangles.
class Triangle extends InheritanceShapes {
private String style;
// A default constructor.
Triangle() {
super();
style = "none";
}
Triangle(String s, double w, double h) {
super(w, h); // call superclass constructor
style = s;
}
// One argument constructor.
Triangle(double x) {
super(x); // call superclass constructor
style = "filled";
}
double area() {
return getWidth() * getHeight() / 2;
}
void setStyle(String s) { style = s; }
void showStyle() {
System.out.println("Triangle is " + style + ".");
}
}
// Extend Triangle.
class ColorTriangle extends Triangle {
private String color;
ColorTriangle(String c, String s, double w, double h) {
super(s, w, h);
color = c;
}
String getColor() { return color; }
void showColor() { System.out.println("Color is " + color + ".");
}
}
class Shapes {
public static void main(String[] args) {
Triangle t1 = new Triangle();
Triangle t2 = new Triangle();
var ct1 = new ColorTriangle("Blue", "outlined", 8.0, 12.0);
ColorTriangle ct2 = new ColorTriangle("Red", "filled", 2.0, 2.0);
t1.setWidth(4.0);
t1.setHeight(4.0);
t1.setStyle("filled");
t2.setWidth(8.0);
t2.setHeight(12.0);
t2.setStyle("outlined");
System.out.println("\nInfo for t1: ");
t1.showStyle();
t1.showDim();
System.out.println("Area is " + t1.area() + ".");
System.out.println("\nInfo for t2: ");
t2.showStyle();
t2.showDim();
System.out.println("Area is " + t2.area() + ".");
System.out.println("\nInfo for ct1: ");
ct1.showStyle();
ct1.showDim();
ct1.showColor();
System.out.println("Area is " + ct1.area() + ".");
System.out.println("\nInfo for ct2: ");
ct2.showStyle();
ct2.showDim();
ct2.showColor();
System.out.println("Area is " + ct2.area() + ".");
}
}