0

This is for some home work i have in my java development class, we are supposed to create a shape calculator, consisting of a square, triangle, and circle while also displaying the radius and circumference of said shape once the user inputs what shape they want to see.

So far im having alot of trouble,the prompt for the homework asks for this "ask the user which shape they would like to work with".

Im not sure how to implement the feature in which the user would type the shape and java would recognize what was typed in and specifically target the shape that was typed in by the user and display its properties, i would assume it would utilize an else statement?

I've already figured how to display the string that prompts the user what shape they would like to use, but i can't find out how to specifically target said shape and have it display its radius and circumference.

package geometric;
import java.util.Scanner;

public class shapes {

    public static void main(String[] args) {
         Scanner scanner = new Scanner (System.in);
         String shapeSelect = "Which Shape: Square Circle or Triangle?";
         System.out.println(shapeSelect);
         String Circle;
         double circle;

         circle = scanner.nextDouble();
         System.out.println("enter the radius: ");
    }

}

2 Answers 2

2

You want to get the type of shape before they can enter the radius, right?

String input = scanner.nextLine();

should go right after you print your shapeSelect message.

After storing whichever shape was picked you can use if else or switch statements to do what needs to be done for each shape, after that you can just pass the variables that are stored to the methods you use to calculate the shape's properties as parameters.

Your code should look something like this:

  public static void main(String[] args) {
         Scanner scanner = new Scanner (System.in);
         String shapeSelect = "Which Shape: Square Circle or Triangle?";

         System.out.println(shapeSelect); // what shape?
         String selectedShape = scanner.nextLine(); //waits for your input

         System.out.println("enter the radius: "); // what radius?
         double circleRadius = scanner.nextDouble(); //waits for your input

         if(selectedShape.contentEquals("circle")){
           //call circle methods
         } else if(selectedShape.contentEquals("triangle")){
           //call triangle methods
         }
    }

It is also best practice to start your variable names with a lower case letter.

For the conditionals you may also use equalsIgnoreCase if you want to make it case insensitive.

Sign up to request clarification or add additional context in comments.

Comments

0

You can let the user type in the name of a geometric shape and then evaluate the string that is returned by the scanner.

String userInput = scanner.nextLine();
if(userInput.equals("circle")){   // be aware that equals is case sensitive. Use .equalsIgnoreCase otherwise
    // print circle properties and ask for the radius
}else if(userInput.equals("square"){
    // ask the user for square parameters 
}
... and so on

You also may want to provide the user a complete list of possible shapes, so that he doesn't type "hexagon" although you only provide "pentagon"

1 Comment

Please improve the formatting and add an explanation because code only answers are not preferred

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.