• Source
    1. /* package whatever; // don't place package name! */
    2.  
    3. import java.util.*;
    4. import java.lang.*;
    5. import java.io.*;
    6.  
    7. // Shape.java: abstract base class
    8. abstract class Shape { // abstract class, can't instantiate
    9. // to implement an idNumber
    10. private static int counter = 0;
    11. private int idNumber;
    12. public Shape () {
    13. idNumber = ++counter;
    14. }
    15. public int getIdNumber() { return idNumber;}
    16.  
    17. public double area() { return 0.0; }
    18. public double volume() { return 0.0; }
    19. public abstract String getName(); // abstract, so omit body
    20. }
    21.  
    22. // Point.java: the class Point
    23. class Point extends Shape {
    24. protected int x, y; // coordinates of the Point
    25.  
    26. // constructor
    27. public Point( int a, int b ) { x = a; y = b; }
    28.  
    29. // get x coordinate
    30. public int getX() { return x; }
    31.  
    32. // get y coordinate
    33. public int getY() { return y; }
    34.  
    35. // convert the point into a String representation
    36. public String toString()
    37. { return "[" + x + ", " + y + "]"; }
    38.  
    39. // return the class name
    40. public String getName() { return "Point"; }
    41. }
    42.  
    43. // circle.java: the class Circle
    44. class Circle extends Point { // inherits from Point
    45. protected double radius;
    46.  
    47. // Constructor
    48. public Circle( double r, int a, int b ) {
    49. super( a, b ); // call the superclass constructor
    50. radius = ( r >= 0 ? r : 0 );
    51. }
    52.  
    53. // Calculate area of Circle
    54. public double area() { return Math.PI * radius * radius; }
    55.  
    56. // convert the Circle to a String
    57. public String toString()
    58. { return "Center = " + super.toString() +
    59. "; Radius = " + radius; }
    60.  
    61. // return the class name
    62. public String getName() { return "Circle"; }
    63. }
    64.  
    65. // Cylinder.java: the class Cylinder
    66. class Cylinder extends Circle {
    67. protected double height; // height of Cylinder
    68.  
    69. // constructor
    70. public Cylinder( double h, double r, int a, int b ) {
    71. super( r, a, b ); // call superclass constructor
    72. height = ( h >= 0 ? h : 0 );
    73. }
    74.  
    75. // Calculate area of Cylinder (i.e., surface area)
    76. public double area() {
    77. return 2 * super.area() +
    78. 2 * Math.PI * radius * height;
    79. }
    80.  
    81. // Calculate volume of Cylinder
    82. public double volume() { return super.area() * height; }
    83.  
    84. // Convert a Cylinder to a String
    85. public String toString() {
    86. return super.toString() + "; Height = " + height;
    87. }
    88.  
    89. // Return the class name
    90. public String getName() { return "Cylinder"; }
    91. }
    92.  
    93.  
    94. // Rectangle.java: the class Rectangle
    95. class Rectangle extends Point { // inherits from Point
    96. protected double length;
    97. protected double width;
    98.  
    99. // Constructor
    100. public Rectangle( double len, double wid, int a, int b ) {
    101. super( a, b ); // call the superclass constructor
    102. length = ( len >= 0 ? len : 0 );
    103. width = ( wid >= 0 ? wid : 0 );
    104. }
    105.  
    106. // Calculate area of Rectangle
    107. public double area() { return length * width; }
    108.  
    109. // convert the Rectangle to a String
    110. public String toString() {
    111. return "Center = " + super.toString() +
    112. "; Length = " + length + ", Width = " + width; }
    113.  
    114. // return the class name
    115. public String getName() {
    116. if (length == width) return "Square";
    117. else return "Rectangle";
    118. }
    119. }
    120.  
    121.  
    122.  
    123. // RectangularSolid.java: the class RectangularSolid
    124. class RectangularSolid extends Rectangle {
    125. protected double height; // height of RectangularSolid
    126.  
    127. // constructor
    128. public RectangularSolid(double h, double len, double wid, int a, int b) {
    129. super(len, wid, a, b); // call superclass constructor
    130. height = ( h >= 0 ? h : 0 );
    131. }
    132.  
    133. // Calculate area of RectangularSolid (i.e., surface area)
    134. public double area() {
    135. return 2 * super.area() +
    136. 2 * length * height +
    137. 2 * width * height;
    138. }
    139.  
    140. // Calculate volume of RectangularSolid
    141. public double volume() { return super.area() * height; }
    142.  
    143. // Convert a RectangularSolid to a String
    144. public String toString() {
    145. return super.toString() + "; Height = " + height;
    146. }
    147.  
    148. // Return the class name
    149. public String getName() {
    150. if (length == width && length == height)
    151. return("Cube");
    152. else return "RectangularSolid";
    153. }
    154. }
    155.  
    156.  
    157. // Main.java: test point, circle, cylinder hierarchy
    158. public class Main {
    159.  
    160. private static Shape shapes[];
    161.  
    162. public static void createShapes() {
    163.  
    164. shapes = new Shape[15]; // generic array of any shape
    165.  
    166. shapes[0] = new Rectangle(3.0, 4.0, 6, 8);
    167. shapes[1] = new Point(7, 11);
    168. shapes[2] = new Circle(3.5, 22, 8);
    169. shapes[3] = new Cylinder(10, 3.3, 10, 10);
    170. shapes[4] = new RectangularSolid(2.0, 3.0, 4.0, 6, 8);
    171. shapes[5] = new Point(8, 12);
    172. shapes[6] = new Rectangle(2.0, 2.0, 7, 9);
    173. shapes[7] = new Circle(3.6, 23, 9);
    174. shapes[8] = new Cylinder(12, 3.4, 20, 20);
    175. shapes[9] = new Rectangle(6.0, 6.0, 5, 7);
    176. shapes[10] = new RectangularSolid(3.0, 3.0, 3.0, 10, 4);
    177. shapes[11] = new Point(9, 13);
    178. shapes[12] = new Circle(3.7, 24, 10);
    179. shapes[13] = new Cylinder(14, 3.5, 30, 30);
    180. shapes[14] = new RectangularSolid(4.0, 4.0, 4.0, 8, 9);
    181. }
    182.  
    183. public static void printShapes() {
    184.  
    185. // Loop through arrayOfShapes. Use polymorphism to print the name,
    186. // area, and volume of each object.
    187. System.out.println("PRINT THE SHAPES AS AN ARRAY OF SHAPE");
    188. for ( int i = 0; i < shapes.length; i++ ) {
    189. System.out.println(shapes[i].getName() + ": " +
    190. shapes[i].toString() + ", ID: " +
    191. shapes[i].getIdNumber());
    192. System.out.println("Area = " + shapes[i].area());
    193. System.out.println("Volume = " + shapes[i].volume());
    194. System.out.println();
    195. }
    196. }
    197.  
    198. // insertionSort: Sorts array of Shapes using the insertion sort
    199. public static void insertionSort () {
    200. for (int index = 1; index < shapes.length; index++) {
    201. Shape key = shapes[index];
    202. int position = index;
    203. // shift larger values to the right
    204. while (position > 0 && compareShapes(shapes[position-1], key) > 0) {
    205. shapes[position] = shapes[position-1];
    206. position--;
    207. }
    208. shapes[position] = key;
    209. }
    210. }
    211.  
    212. private static int compareShapes (Shape s1, Shape s2) {
    213. if ((s1.getName()).equals(s2.getName()))
    214. return s1.getIdNumber() - s2.getIdNumber();
    215. else return (s1.getName()).compareTo(s2.getName());
    216. }
    217.  
    218. public static void main (String[] args) {
    219. Main shapeTest = new Main();
    220. Main.createShapes();
    221. Main.insertionSort();
    222. Main.printShapes();
    223. }
    224. }