File tree Expand file tree Collapse file tree 10 files changed +230
-0
lines changed
Exercises/Chapter10/src/Exercise13 Expand file tree Collapse file tree 10 files changed +230
-0
lines changed Original file line number Diff line number Diff line change 1+ package Exercise13 ;
2+
3+ public class Circle extends TwoDimShape {
4+ private double radius ;
5+
6+ public Circle (double radius ) {
7+ this .radius = radius ;
8+ }
9+
10+ @ Override
11+ public double getArea () {
12+ return Math .PI * Math .pow (radius , 2 );
13+ }
14+
15+ public double getRadius () {
16+ return radius ;
17+ }
18+
19+ public void setRadius (double radius ) {
20+ this .radius = radius ;
21+ }
22+
23+ @ Override
24+ public String toString () {
25+ return "Circle [radius=" + radius + "]" ;
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ package Exercise13 ;
2+
3+ public class Cube extends ThreeDimShape {
4+ private double side ;
5+
6+ public Cube (double side ) {
7+ this .side = side ;
8+ }
9+
10+ public double getSide () {
11+ return side ;
12+ }
13+
14+ public void setSide (double side ) {
15+ this .side = side ;
16+ }
17+
18+ @ Override
19+ public double getArea () {
20+ return 6 * Math .pow (side , 2 );
21+ }
22+
23+ @ Override
24+ public double getVolume () {
25+ return Math .pow (side , 3 );
26+ }
27+
28+ @ Override
29+ public String toString () {
30+ return "Cube [side=" + side + "]" ;
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ package Exercise13 ;
2+
3+ public abstract class Shape {
4+ public abstract double getArea ();
5+ }
Original file line number Diff line number Diff line change 1+ package Exercise13 ;
2+
3+ public class Sphere extends ThreeDimShape {
4+ private double radius ;
5+
6+ public Sphere (double radius ) {
7+ this .radius = radius ;
8+ }
9+
10+ public double getRadius () {
11+ return radius ;
12+ }
13+
14+ public void setRadius (double radius ) {
15+ this .radius = radius ;
16+ }
17+
18+ @ Override
19+ public double getArea () {
20+ return 4 * Math .PI * Math .pow (radius , 2 );
21+ }
22+
23+ @ Override
24+ public double getVolume () {
25+ return (4 / 3 ) * Math .PI * Math .pow (radius , 3 );
26+ }
27+
28+ @ Override
29+ public String toString () {
30+ return "Sphere [radius=" + radius + "]" ;
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ package Exercise13 ;
2+
3+ public class Square extends TwoDimShape {
4+ private double side ;
5+
6+ public Square (double side ) {
7+ this .side = side ;
8+ }
9+
10+ public double getSide () {
11+ return side ;
12+ }
13+
14+ public void setSide (double side ) {
15+ this .side = side ;
16+ }
17+
18+ @ Override
19+ public double getArea () {
20+ return Math .pow (side , 2 );
21+ }
22+
23+ @ Override
24+ public String toString () {
25+ return "Square [side=" + side + "]" ;
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ package Exercise13 ;
2+
3+ import java .util .ArrayList ;
4+
5+ public class Test {
6+ public static void main (String [] args ) {
7+
8+ ArrayList <Shape > shapes = new ArrayList <Shape >();
9+
10+ shapes .add (new Circle (5 ));
11+ shapes .add (new Square (5 ));
12+ shapes .add (new Triangle (8 , 3 ));
13+ shapes .add (new Sphere (4 ));
14+ shapes .add (new Cube (4 ));
15+ shapes .add (new Tetrahedron (10 ));
16+
17+ for (Shape shape : shapes ) {
18+ System .out .println (shape + ":" );
19+ if (shape instanceof ThreeDimShape ) {
20+ System .out .printf ("\t Area: %.2f%n" , ((ThreeDimShape ) shape ).getArea ());
21+ System .out .printf ("\t Volume: %.2f%n" , ((ThreeDimShape ) shape ).getVolume ());
22+ }
23+ else if (shape instanceof TwoDimShape ) {
24+ System .out .printf ("\t Area: %.2f%n" , ((TwoDimShape ) shape ).getArea ());
25+ }
26+ }
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ package Exercise13 ;
2+
3+ public class Tetrahedron extends ThreeDimShape {
4+ private double side ;
5+
6+ public Tetrahedron (double side ) {
7+ this .side = side ;
8+ }
9+
10+ public double getSide () {
11+ return side ;
12+ }
13+
14+ public void setSide (double side ) {
15+ this .side = side ;
16+ }
17+
18+ @ Override
19+ public double getArea () {
20+ return Math .sqrt (3 ) * Math .pow (side , 2 );
21+ }
22+
23+ @ Override
24+ public double getVolume () {
25+ return Math .pow (side , 3 ) / (6 * Math .sqrt (2 ));
26+ }
27+
28+ @ Override
29+ public String toString () {
30+ return "Tetrahedron [side=" + side + "]" ;
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ package Exercise13 ;
2+
3+ public abstract class ThreeDimShape extends TwoDimShape {
4+ public abstract double getVolume ();
5+ }
Original file line number Diff line number Diff line change 1+ package Exercise13 ;
2+
3+ public class Triangle extends TwoDimShape {
4+ private double base ;
5+ private double height ;
6+
7+ public Triangle (double base , double height ) {
8+ this .base = base ;
9+ this .height = height ;
10+ }
11+
12+ public double getBase () {
13+ return base ;
14+ }
15+
16+ public void setBase (double base ) {
17+ this .base = base ;
18+ }
19+
20+ public double getHeight () {
21+ return height ;
22+ }
23+
24+ public void setHeight (double height ) {
25+ this .height = height ;
26+ }
27+
28+ @ Override
29+ public double getArea () {
30+ return (base * height ) / 2 ;
31+ }
32+
33+ @ Override
34+ public String toString () {
35+ return "Triangle [base=" + base + ", height=" + height + "]" ;
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ package Exercise13 ;
2+
3+ public abstract class TwoDimShape extends Shape {
4+
5+ }
You can’t perform that action at this time.
0 commit comments