Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

So this is the example I am currently working on. It HAS to be OBJECT ORIENTED JAVA programming. I am having trouble with my program.

So this is the example I am currently working on. It HAS to be OBJECT ORIENTED JAVA programming. I am having trouble with my program. I only need it to ask for certain amounts of input. I did an array of 3 to put the parameters in the other classes but this is the only way I could figure out how to do it. So if the user choose square, they have to enter the value three times. I don't know how to fix it.. I will post my code.

image text in transcribed

image text in transcribed

import java.util.Scanner;

abstract class Shape { protected double perimeter;

public abstract void calculatePerimeter(double[] sides);

public double getPerimeter() { return perimeter; } }

class Rectangle extends Shape { @Override public void calculatePerimeter(double[] sides) { perimeter = 2 * (sides[0] + sides[1]); } }

class Square extends Shape { @Override public void calculatePerimeter(double[] sides) { perimeter = 4 * sides[0]; } }

class Circle extends Shape { @Override public void calculatePerimeter(double[] sides) { perimeter = Math.PI * sides[0]; } }

class Triangle extends Shape { @Override public void calculatePerimeter(double[] sides) { perimeter = sides[0] + sides[1] + sides[2]; } }

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");

double totalLength = 0;

while (true) { System.out.print(" Enter the shape type (R, S, C, T, L, 2): "); String shapeType = scanner.next();

if (shapeType.equalsIgnoreCase("Q")) { break; }

Shape shape = null;

switch (shapeType.toUpperCase()) { case "R": shape = new Rectangle(); System.out.print("Enter Height and Width of Rectangle: "); break; case "S": shape = new Square(); System.out.print("Enter Height of Square: "); break; case "C": shape = new Circle(); System.out.print("Enter Diameter of Circle: "); break; case "T": shape = new Triangle(); System.out.print("Enter Sides of Triangle: "); break; case "L": System.out.print("Enter Length of Line: "); double lineLength = scanner.nextDouble(); totalLength += lineLength; break; default: System.out.println("Invalid shape type. Try again."); continue; }

if (shape != null) { double[] sides = new double[3];

for (int i = 0; i

shape.calculatePerimeter(sides);

System.out.printf("The perimeter of the %s is %.1f ", shape.getClass().getSimpleName(), shape.getPerimeter());

totalLength += shape.getPerimeter(); } }

System.out.println(" Shapes Needed"); System.out.printf("%.1f - rectangle ", 37.0); System.out.printf("%.1f - circle ", 37.7); System.out.printf("%.1f - line ", totalLength); System.out.printf("%.1f Total Length ", 74.7); System.out.println("Thank You"); } }

- (R)ectangle given height and width - (S)quare given height - (C)ircumference of a circle, given the diameter. - (T)riangle perimeter - (L)ine given length [used for connecting shapes] The program should continue to ask the user for values until the user enters ' Q ' as the type of calculation. After data entry, print the list of shapes and the Total Length. Welcome to Nick's Neon Tubing Calculator Enter the shape type (R, S, C, T, L, Q): R Enter Height and width of Rectangle: 6.512 The perimeter of a 6.5 x 12 rectangle is 37.0 Enter the shape type (R, S, C, T, L, Q): c Enter Diameter: 12 The circumference of circle with diameter 12 is 37.7 Enter the shape type (R,S,C,T,L,Q) : Q Shapes Needed 37.0 - rectangle, 6.512 37.7 - circle, diameter 12 74.7 Total Length Thank You

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Explain the importance of nonverbal messages.

Answered: 1 week ago

Question

Describe the advantages of effective listening.

Answered: 1 week ago

Question

Prepare an employment application.

Answered: 1 week ago