-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquare.java
More file actions
89 lines (71 loc) · 2.61 KB
/
Copy pathSquare.java
File metadata and controls
89 lines (71 loc) · 2.61 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
class Rectangle {
// define attributes
private int length;
private int width;
// constructor method to instantiate the attributes
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
// getter methods
public int getLength() {
// Returns the value of private attr length
return this.length;
}
public int getWidth() {
// Returns the value of private attr width
return this.width;
}
// setter methods
public void setLength(int val) {
// set the value of private attr length
this.length = val;
}
public void setWidth(int width_value) {
// set the value of private attr width
this.width = width_value;
}
// define method calculate area of the rectangle
public int calculate_area() {
return this.length * this.width;
}
// main entry point to java program
public static void main(String[] args) {
// Creating an instance of Rectangle
Rectangle myRectangle = new Rectangle(5, 3);
// Calculating and printing the area
int area = myRectangle.calculate_area();
System.out.println("The area of the rectangle is: " + area);
}
}
public class Square extends Rectangle {
// constructor method to instantiate the attributes
public Square(int size) {
// call the superclass (Rectangle) constructor and initialize length & width
super(size, size);
}
// Optional: You can override the calculate_area method for Square
@Override
public int calculate_area() {
System.out.println("Calculating area of the square...");
return this.getLength() * this.getLength(); // You can still use the method from the superclass or provide a
// different
// implementation
}
public static void main(String[] args) {
// Creating an instance of Square
Square square_a = new Square(5);
// Before modifying
System.out.println("Length of the square: " + square_a.getLength()); // 5
System.out.println("Width of the square: " + square_a.getWidth()); // 5
square_a.setLength(10);
;
square_a.setWidth(10);
// After modifying
System.out.println("Length of the square: " + square_a.getLength()); // 10
System.out.println("Width of the square: " + square_a.getWidth()); // 10
// // Calculating and printing the area
int area = square_a.calculate_area();
System.out.println("The area of the square is: " + area);
}
}