Question
This is supposed to be a OBJECT ORIENTED JAVA program and the code I have gotten so far wont work. I think it's because shape
This is supposed to be a OBJECT ORIENTED JAVA program and the code I have gotten so far wont work. I think it's because shape cant be used in the if statement but im not sure. can someone look at it and help me out?
import java.util.ArrayList; import java.util.Scanner;
abstract class Shape { protected double perimeter; protected int numBrackets; protected double[] sides;
public Shape(double[] sides) { this.sides = sides; }
public abstract void calculatePerimeter();
public double getPerimeter() { return perimeter; }
public int getNumBrackets() { return numBrackets; }
public double[] getSides() { return sides; } }
class Rectangle extends Shape { public Rectangle(double[] sides) { super(sides); }
@Override public void calculatePerimeter() { perimeter = 2 * (sides[0] + sides[1]); numBrackets = calculateNumBrackets(); }
private int calculateNumBrackets() { int numVertices = 4; int numEndpoints = 4; int numSides = 4; int maxMaterialWithoutBrackets = 20;
int numBrackets = numVertices + numEndpoints; double totalMaterial = perimeter; double materialWithoutBrackets = maxMaterialWithoutBrackets - numVertices - numEndpoints;
for (int i = 0; i materialWithoutBrackets) { materialOnSide -= materialWithoutBrackets; numBrackets++; } totalMaterial += sideLength; }
return numBrackets; } }
class Square extends Rectangle { public Square(double[] sides) { super(sides); } }
class Circle extends Shape { public Circle(double[] sides) { super(sides); }
@Override public void calculatePerimeter() { perimeter = Math.PI * sides[0]; numBrackets = 2; } }
class Triangle extends Shape { public Triangle(double[] sides) { super(sides); }
@Override public void calculatePerimeter() { perimeter = sides[0] + sides[1] + sides[2]; numBrackets = calculateNumBrackets(); }
private int calculateNumBrackets() { int numVertices = 3; int numEndpoints = 3; int numSides = 3; int maxMaterialWithoutBrackets = 20;
int numBrackets = numVertices + numEndpoints; double totalMaterial = perimeter; double materialWithoutBrackets = maxMaterialWithoutBrackets - numVertices - numEndpoints;
for (int i = 0; i materialWithoutBrackets) { materialOnSide -= materialWithoutBrackets; numBrackets++; } totalMaterial += sideLength; }
return numBrackets; } }
public class NeonTubingCalculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
System.out.println(" Welcome to Nick's Neon Tubing Calculator");
ArrayList
while (true) { System.out.println(" (R)ectangle, (S)quare, (C)ircle, (T)riangle, (L)ine or (Q)uit?"); String shapeType = scanner.next();
if (shapeType.equalsIgnoreCase("L")) { System.out.print("Enter Length of Line: "); double lineLength = scanner.nextDouble(); totalLength += lineLength;
int numBrackets = (int) Math.ceil(lineLength / 20); System.out.printf("This line needs %d bracket(s). ", numBrackets);
totalNumBrackets += numBrackets; totalNumLines++; } else { double[] sides = new double[3];
for (int i = 0; i
shape.calculatePerimeter(sides);
System.out.printf("The perimeter of the %s is %.1f cm ", shape.getClass().getSimpleName(), shape.getPerimeter());
int numBrackets = 0; if (shape instanceof Square) { numBrackets = calculateNumBracketsForSquare((Square) shape); } else if (shape instanceof Rectangle) { numBrackets = calculateNumBracketsForRectangle((Rectangle) shape); } else if (shape instanceof Circle) { numBrackets = calculateNumBracketsForCircle((Circle) shape); } else if (shape instanceof Triangle) { numBrackets = calculateNumBracketsForTriangle((Triangle) shape); }
System.out.printf("This shape needs %d bracket(s). ", numBrackets);
totalNumBrackets += numBrackets;
if (shape instanceof Square) { totalNumSquares++; } else if (shape instanceof Rectangle) { totalNumRectangles++; } else if (shape instanceof Circle) { totalNumCircles++; } else if (shape instanceof Triangle) { totalNumTriangles++; } }
totalNumShapes++; totalLength += shape.getPerimeter(); } } }
Version 2.0 needs to meet all of the design specifications for v1.0 PLUS: - Units - all measurements are in cm and calculations/output should be to the mm (ie 1 decimal place) - Triangles - enter the lengths of the 3 sides - BRACKETS - for each object, determine the number of brackets necessary to support it: - 1 bracket per vertex or endpoint (for a line) - so a square needs at least 4. - No more than 20cm of material without a bracket. A 20cm square needs 4 , but a 25cm square needs 8 since each side needs an additional bracket. - A circle needs at least 2 support brackets - Count the number of items of each type (e.g. 3 rectangles, 1 square). During the Design \& Coding Phases: Apply Object Oriented design principles following the guidelines discussed in class. Use inheritance to create a hierarchy of shapes. At least one abstract class is required with methods get TubeLength() and getNumBrackets() ArrayList (or similar) is required Use instanceOf() in an appropriate fashion. (counting the shape types?) Make sure to separate the user interface (including all I/O) from the shape classesStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started