-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wall.java
107 lines (79 loc) · 2.78 KB
/
Wall.java
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
// Write a class with the name Wall. The class needs two fields (instance variables) with name width and height of type double.
// The class needs to have two constructors. The first constructor does not have any parameters (no-arg constructor). The second constructor has parameters width and height of type double and it needs to initialize the fields. In case the width is less than 0 it needs to set the width field value to 0, in case the height parameter is less than 0 it needs to set the height field value to 0.
// Write the following methods (instance methods):
// Method named getWidth without any parameters, it needs to return the value of width field.
// Method named getHeight without any parameters, it needs to return the value of height field.
// Method named setWidth with one parameter of type double, it needs to set the value of the width field. If the parameter is less than 0 it needs to set the width field value to 0.
// Method named setHeight with one parameter of type double, it needs to set the value of the height field. If the parameter is less than 0 it needs to set the height field value to 0.
// Method named getArea without any parameters, it needs to return the area of the wall.
// TEST EXAMPLE
// → TEST CODE:
// 1 Wall wall = new Wall(5,4);
// 2 System.out.println("area= " + wall.getArea());
// 3wall.setHeight(-1.5);
// 4 System.out.println("width= " + wall.getWidth());
// 5 System.out.println("height= " + wall.getHeight());
// 6 System.out.println("area= " + wall.getArea());
// OUTPUT:
// area= 20.0
// width= 5.0
// height= 0.0
// area= 0.0
public class Wall
{
private double width;
private double height;
public Wall ()
{
}
public Wall (double width, double height)
{
if (width < 0)
{
width = 0;
}
if (height < 0)
{
height = 0;
}
this.width = width;
this.height = height;
}
public double getWidth ()
{
return width;
}
public double getHeight ()
{
return height;
}
public void setWidth (double width)
{
if (width < 0)
{
width = 0;
}
this.width = width;
}
public void setHeight (double height)
{
if (height < 0)
{
height = 0;
}
this.height = height;
}
public double getArea ()
{
return getWidth () * getHeight ();
}
public static void main (String[]args)
{
Wall wall = new Wall (5, 4);
System.out.println ("area= " + wall.getArea ());
wall.setHeight (-1.5);
System.out.println ("width=" + wall.getWidth ());
System.out.println ("height= " + wall.getHeight ());
System.out.println ("area= " + wall.getArea ());
}
}