fork download
  1. // Part 4: OVERLOAD / OVERRIDE (circle the correct term) the Shape's perimeter() and // area() methods.
  2.  
  3. import java.util.Scanner;
  4.  
  5. class Shape {
  6. private final int NUMBER_OF_SIDES;
  7. public Shape() {
  8. NUMBER_OF_SIDES = 0;
  9. }
  10. public Shape(int sides) {
  11. NUMBER_OF_SIDES = sides;
  12. }
  13. public int getNumberOfSides() {
  14. return NUMBER_OF_SIDES;
  15. }
  16. public double area() {
  17. return 0;
  18. }
  19. public double perimeter() {
  20. return 0;
  21. }
  22. }
  23.  
  24.  
  25. class Point {
  26. private double x;
  27. private double y;
  28. public Point() {
  29. }
  30. public Point(double x, double y) {
  31. this.x = x;
  32. this.y = y;
  33. }
  34. public double getX() {
  35. return x;
  36. }
  37. public double getY() {
  38. return y;
  39. }
  40. }
  41.  
  42.  
  43.  
  44. class Circle extends Shape {
  45. private double radius;
  46. private Point center;
  47. public Circle() {
  48. }
  49. public Circle(int sides, double radius, Point center) {
  50. super(sides);
  51. this.radius = radius;
  52. this.center = center;
  53. }
  54. public String toStr() {
  55. return "Circle with " + getNumberOfSides() + " side, radius = " + radius + ", center = (" + center.getX() + "," + center.getY() + "), area = " + area() + ", circumference = " + circumference();
  56. }
  57. public double area() {
  58. return 3.14 * radius * radius;
  59. }
  60. public double circumference() {
  61. return 2 * 3.14 * radius;
  62. }
  63. }
  64.  
  65.  
  66.  
  67. class Rectangle extends Shape {
  68. private double length;
  69. private double width;
  70. public Rectangle() {
  71. }
  72. public Rectangle(int sides, double length, double width) {
  73. super(sides);
  74. this.length = length;
  75. this.width = width;
  76. }
  77. public String toStr() {
  78. return "Rectangle with " + getNumberOfSides() + " sides, length = " + length + ", width = " + width + ", area = " + area() + ", perimeter = " + perimeter();
  79. }
  80. public double area() {
  81. return length * width;
  82. }
  83. public double perimeter() {
  84. return 2 * (length + width);
  85. }
  86. }
  87.  
  88.  
  89. class Triangle extends Shape {
  90. private double sideA;
  91. private double sideB;
  92. private double sideC;
  93. private double base;
  94. private double height;
  95. public Triangle() {
  96. }
  97. public Triangle(int sides, double sideA, double sideB, double sideC, double base, double height) {
  98. super(sides);
  99. this.sideA = sideA;
  100. this.sideB = sideB;
  101. this.sideC = sideC;
  102. this.base = base;
  103. this.height = height;
  104. }
  105. public String toStr() {
  106. return "Triangle with 3 sides, side lengths = " + sideA + "," + sideB + "," + sideC + ", base = " + base + ", height = " + height + ", area = " + area() + ", perimeter = " + perimeter();
  107. }
  108. public double area() {
  109. return 0.5 * base * height;
  110. }
  111. public double perimeter() {
  112. return sideA + sideB + sideC;
  113. }
  114. }
  115.  
  116.  
  117.  
  118. public class Main {
  119. public static void main(String[] args) {
  120. Shape[] shapes = new Shape[3];
  121. Scanner s = new Scanner(System.in);
  122. for(int i = 0; i < 3; i++) {
  123. System.out.print("Number of sides (1=Circle, 3=Triangle, 4=Rectangle): ");
  124. int sides = s.nextInt();
  125. if(sides == 1) {
  126. System.out.print("Radius: ");
  127. double radius = s.nextDouble();
  128. System.out.print("X: ");
  129. double x = s.nextDouble();
  130. System.out.print("Y: ");
  131. double y = s.nextDouble();
  132. shapes[i] = new Circle(sides, radius, new Point(x, y));
  133. }
  134. else if(sides == 3) {
  135. System.out.print("Side A: ");
  136. double sideA = s.nextDouble();
  137. System.out.print("Side B: ");
  138. double sideB = s.nextDouble();
  139. System.out.print("Side C: ");
  140. double sideC = s.nextDouble();
  141. System.out.print("Base: ");
  142. double base = s.nextDouble();
  143. System.out.print("Height: ");
  144. double height = s.nextDouble();
  145. shapes[i] = new Triangle(sides, sideA, sideB, sideC, base, height);
  146. }
  147. else if(sides == 4) {
  148. System.out.print("Length: ");
  149. double length = s.nextDouble();
  150. System.out.print("Width: ");
  151. double width = s.nextDouble();
  152. shapes[i] = new Rectangle(sides, length, width);
  153. }
  154. }
  155. for(int i = 0; i < 3; i++) {
  156. if(shapes[i] instanceof Circle)
  157. System.out.println(((Circle)shapes[i]).toStr());
  158. else if(shapes[i] instanceof Triangle)
  159. System.out.println(((Triangle)shapes[i]).toStr());
  160. else if(shapes[i] instanceof Rectangle)
  161. System.out.println(((Rectangle)shapes[i]).toStr());
  162. }
  163. }
  164. }
  165.  
  166.  
  167.  
  168.  
Success #stdin #stdout 0.09s 2184192KB
stdin
1
1
1
1
1
1
1
1
1
1
1
1
stdout
Number of sides (1=Circle, 3=Triangle, 4=Rectangle): Radius: X: Y: Number of sides (1=Circle, 3=Triangle, 4=Rectangle): Radius: X: Y: Number of sides (1=Circle, 3=Triangle, 4=Rectangle): Radius: X: Y: Circle with 1 side, radius = 1.0, center = (1.0,1.0), area = 3.14, circumference = 6.28
Circle with 1 side, radius = 1.0, center = (1.0,1.0), area = 3.14, circumference = 6.28
Circle with 1 side, radius = 1.0, center = (1.0,1.0), area = 3.14, circumference = 6.28