-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRectangleTest.java
More file actions
56 lines (50 loc) · 2.22 KB
/
Copy pathRectangleTest.java
File metadata and controls
56 lines (50 loc) · 2.22 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
/*Exercise 9.1
* (The Rectangle class) Following the example of the Circle class in Section 9.2,
design a class named Rectangle to represent a rectangle. The class contains:
-Two double data fields named width and height that specify the width and
height of the rectangle. The default values are 1 for both width and height .
- A no-arg constructor that creates a default rectangle.
- A constructor that creates a rectangle with the specified width and height .
- A method named getArea() that returns the area of this rectangle.
- A method named getPerimeter() that returns the perimeter.
Draw the UML diagram for the class and then implement the class. Write a test
program that creates two Rectangle objects—one with width 4 and height 40
and the other with width 3.5 and height 35.9 . Display the width, height, area,
and perimeter of each rectangle in this order
*/
// import java.util.Scanner;
public class RectangleTest {
public static void main(String[] args) {
// Rectangle objects—one with width 4 and height 40
Rectangle rectangle1 = new Rectangle(4, 40);
System.out.println("Rectangle 1 with width " + rectangle1.width + " and height " + rectangle1.height +
" has an area of " + rectangle1.getArea() + " and a perimeter of " + rectangle1.getPerimeter());
// Rectangle objects—two with width 3.5 and height 35.
Rectangle rectangle2 = new Rectangle(3.5, 35.9);
System.out.println("Rectangle 2 with width " + rectangle2.width + " and height " + rectangle2.height +
" has an area of " + rectangle2.getArea() + " and a perimeter of " + rectangle2.getPerimeter());
}
}
// The Rectangle clas
class Rectangle {
double width;
double height;
// A no-arg constructor that creates a default rectangle.
Rectangle() {
width = 1;
height = 1;
}
// A constructor that creates a rectangle with the specified width and height .
Rectangle (double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
// A method named getArea() that returns the area of this rectangle.
double getArea() {
return width * height;
}
// A method named getPerimeter() that returns the perimeter.
double getPerimeter() {
return width + +height + width + height;
}
}