-
Notifications
You must be signed in to change notification settings - Fork 25
/
Shape.java
106 lines (90 loc) · 1.98 KB
/
Shape.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
package OOPS.Examples;
public abstract class Shape
{
protected String shapeName;
public Shape (String shapeName)
{
this.shapeName = shapeName;
}
abstract double area();
public String toString()
{
return shapeName;
}
}
class Sphere extends Shape
{
private double radius;
public Sphere(double radius)
{
super("Sphere");
this.radius = radius;
}
public double area()
{
return 4*Math.PI*radius*radius;
}
}
class Rectangle extends Shape
{
private double length;
private double width;
public Rectangle (double length, double width)
{
super("Rectangle");
this.length = length;
this.width = width;
}
public double area()
{
return length * width;
}
}
class Cylinder extends Shape
{
private double radius;
private double height;
public Cylinder (double radius, double height)
{
super("Cylinder");
this.radius = radius;
this.height = height;
}
public double area()
{
return Math.PI * radius * radius * height;
}
}
class Paint
{
private double coverage;
public Paint(double coverage)
{
this.coverage = coverage;
}
public double amount(Shape s)
{
double finalAmount = s.area()/coverage;
System.out.println("Quantity needed for " + s + " is " + finalAmount);
return finalAmount;
}
}
class PaintThings
{
public static void main (String[] args)
{
final double coverage = 350;
Paint paint = new Paint(coverage);
Rectangle deck = new Rectangle (20,30);
Sphere bigBall = new Sphere (15);
Cylinder tank = new Cylinder (10,30);
double deckAmt, ballAmt, tankAmt;
deckAmt = paint.amount(deck);
ballAmt = paint.amount(bigBall);
tankAmt = paint.amount(tank);
System.out.println ("\nQuantity of paint needed : \n--------------");
System.out.println ("Deck " + deckAmt);
System.out.println ("Big Ball " + ballAmt);
System.out.println ("Tank " + tankAmt);
}
}