I seem to be having issue in my code for a program to output to a string for 4 d
ID: 3648901 • Letter: I
Question
I seem to be having issue in my code for a program to output to a string for 4 diff shapes the name of the shape, the length of the side(s), the area, the # of custom tiles, cost of tiles, cost of labor, cost of tax, and total cost.When compiled, it asks for the length as expected and also the number of custom tiles. However, after that, it sklps directly to the statement after the loop asking if you would like to enter another shape. If somebody could please point out the error that would be causing this in Square it would be greatly appreciated.
Also, any other tips to improve my code are welcome.
Here is an excerpt from my main class:
import java.util.*;
public class Floorservice {
//Beginning of Main
public static void main(String[] args) {
//Create a new scanner for input
Scanner input = new Scanner(System.in);
//Create an object of shape
Shape newShape;
//Set continue loop to yes
int cont = 1;
//Continue until the user declines to add a new shape
do
{
//Prompt the user to input the type of shape, and read the input
System.out.println("What is the shape of the floorplan?");
System.out.println("Enter 1 for square, 2 for circle, 3 for rectangle, or 4 for triangle");
int t = input.nextInt();
if ( t == 1 ) {
System.out.println("What is the length of a side of the square?");
double l = input.nextDouble();
newShape = new Square(l);
System.out.println(" How many square feet of custom tiles are required? ");
double cust = input.nextDouble();
newShape.setCustom(cust);
System.out.println();
newShape.toString();
}
else if ( t == 2 ){
System.out.println("What is the radius of the circle? ");
double rad = input.nextDouble();
newShape = new Circle(rad);
System.out.println("How many square feet of custom tiles are required?");
double cust = input.nextDouble();
newShape.setCustom(cust);
System.out.println();
newShape.toString();
}
And here is my class for square:
public class Square extends Shape {
//Constructor for Square
public Square (double side)
{
super(side);
}
//Overwritten methods:
public String getName()
{
return "Square";
}
public String toString()
{
String newString = "The floor shape: " + getName() + " " + "Side: "+ getSide() + " " + "Area: " + area() + " " + "Custom-made art tiles: " + super.getCustom() +
" " + "Total cost of tiles: $" + super.getTiles() + " " + "Labor: $" + super.getLabor() + " " + "Tax(8.5%): $" + super.getTax() + " " + "Total Estimated Cost: $" + getCost();
return ( newString );
}
//Find the area by multiplying the side of the square by itself
public double area()
{
return super.getMeasure1() * super.getMeasure1();
}
//Set Method:
public void setSide ( double side )
{
super.setMeasure1( side );
}
//Get Method:
public double getSide ()
{
return super.getMeasure1();
}
}
And here is my class shape, since Square extends Shape
abstract class Shape
{
private double measure1, measure2, custnum;
public double tiles;
public double custom;
public double labor;
public double tax;
//Constructor for class Shape with 2 inputs
public Shape (double m1, double m2)
{
//super ();
measure1 = m1;
measure2 = m2;
}
//Constructor for class Shape with 1 input
public Shape (double m1)
{
//super ();
measure1 = m1;
}
//Set Methods for the 2 measurements
public void setMeasure1 (double m)
{
measure1 = m;
}
public void setMeasure2 (double m)
{
measure2 = m;
}
//Get Methods for the 2 measurements
public double getMeasure1()
{
return measure1;
}
public double getMeasure2()
{
return measure2;
}
//Abstract method allocated to area within Shape
public abstract double area();
public void setCustom(double c)
{
custnum = c;
}
public double getCustom()
{
return custnum;
}
public double getCost()
{
tiles = 15.00;
custom = 20.00;
labor = 1.50;
return ((((area()- custnum) * tiles) + (custom * custnum) + (area() * labor))*1.085);
}
public double getTiles()
{
return ((area()- custnum) * tiles) + (custom * custnum);
}
public double getTax()
{
return (((area()- custnum) * tiles) + (custom * custnum) + (area() * labor)) * .085;
}
public double getLabor()
{
return (area() * labor);
}
//Abstract methods in Abstract Class Shape
public abstract String getName();
//end of abstract class Shape
}
Explanation / Answer
Well I don't find any error in your code... what else do you want it to ask after the length as expected and also the number of custom tiles? It automatically calculates rest of the variables...