-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cuboid.java
72 lines (62 loc) · 1.48 KB
/
Cuboid.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
class Rectangle
{
double length;
double width;
Rectangle ()
{
}
Rectangle (double length, double width)
{
if (length < 0)
this.length = 0;
else
this.length = length;
if (width < 0)
this.width = 0;
else
this.width = width;
}
public double getLength ()
{
return length;
}
public double getWidth ()
{
return width;
}
public double getArea ()
{
return length * width;
}
}
class Cuboid extends Rectangle
{
double height;
Rectangle rectangle;
public Cuboid (Rectangle rectangle, double height)
{
if (height < 0)
this.height = 0;
else
this.height = height;
this.rectangle = rectangle;
}
public double getHeight ()
{
return height;
}
public double getVolume ()
{
return rectangle.getArea () * getHeight ();
}
public static void main (String[]args)
{
Rectangle rectangle = new Rectangle (2, 3);
System.out.println ("rectangle length =" + rectangle.getLength ());
System.out.println ("rectangle width =" + rectangle.getWidth ());
System.out.println ("area= " + rectangle.getArea ());
Cuboid cuboid = new Cuboid (rectangle, 5);
System.out.println ("height= " + cuboid.getHeight ());
System.out.println ("cuboid volume= " + cuboid.getVolume ());
}
}