Question

Write a Java program that prompts the user to enter a number representing a geometric shape....

Write a Java program that prompts the user to enter a number

representing a geometric shape. Options are 1 for square, 2 for

rectangle, and 3 for right triangle. According to the user input, the

program will collect either 1, 2 or 3 values from the user

representing the lengths of the shape sides. The user is

responsible for ensuring the 3 sides of the triangle represent a

right triangle. The program will print the perimeter of the shape.

Use method overloading for the perimeter method.

Hint

Square:

perimeter = 4 x side

Rectangle:

perimeter = 2 x (side1 + side2)

Right Triangle: perimeter = side1 + side2 + hypotenuse

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

//class efinition
public class Perimeter {
  
   //definition of findPerimeter method by method overload
   //findPerimeter definition for square
   public static int findPerimeter(int side){
       int perimeter;
       perimeter = 4 * side;
       return perimeter;
   }

   //findPerimeter definition for rectangle
   public static int findPerimeter(int length,int breadth){
       int perimeter;
       perimeter =2 *( length + breadth);
       return perimeter;
   }
  
   //findPerimeter definition for right triangle
   public static int findPerimeter(int base,int height,int hypotenuse){
       int perimeter;
       perimeter = base + height + hypotenuse;
       return perimeter;
   }
  
   //main function definition
   public static void main(String[] args) {
      
       //create scanner class to take user input
       Scanner sc = new Scanner(System.in);
      
       //define variables for shape and perimeter
       int shape , perimeter = 0;
      
       //prompt to user and take the user choice
       System.out.println("Please choose a shape : \n1. Square \n2. Rectangle \n3. Right Triangle");
       shape = sc.nextInt();
      
       //switch case to execute different blocks for different choices made by user
       switch(shape){
       case 1:
           int s;
           System.out.println("Enter square side :");
           s = sc.nextInt();
           perimeter = findPerimeter(s);
           break;
       case 2:
           int l,b;
           System.out.println("Enter 2 sides of rectangle :");
           l = sc.nextInt();
           b= sc.nextInt();
           perimeter = findPerimeter(l, b);
           break;
       case 3:
           int s1,s2,h;
           System.out.println("Enter 3 sides of rectangle :");
           s1 = sc.nextInt();
           s2= sc.nextInt();
           h= sc.nextInt();
           perimeter = findPerimeter(s1,s2,h);
           break;
       default:
           System.out.println("Invalid Input !");
       }
      
       //Print perimeter if calculated
       if(perimeter>0)
           System.out.println("The perimeter is : " + perimeter);
   }

}

Add a comment
Know the answer?
Add Answer to:
Write a Java program that prompts the user to enter a number representing a geometric shape....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write the java program: A right triangle can have sides whose lengths are all integers. The...

    Write the java program: A right triangle can have sides whose lengths are all integers. The set of three integer values for the length of the sides of a triangle is called a Pythagorean triple. The length of the three sides must satisfy the relationship that the sum of the squares of the sides is equal to the square of the hypotenuse. Write a Java application that prompts the user for an integer that represents the largest side value and...

  • Write the java program: A right triangle can have sides whose lengths are all integers. The...

    Write the java program: A right triangle can have sides whose lengths are all integers. The set of three integer values for the length of the sides of a triangle is called a Pythagorean triple. The length of the three sides must satisfy the relationship that the sum of the squares of the sides is equal to the square of the hypotenuse. Write a Java application that prompts the user for an integer that represents the largest side value and...

  • 2. Write a program that prompts a user to enter the lengths of sides and angles...

    2. Write a program that prompts a user to enter the lengths of sides and angles for the two triangles described below. The program should calculate the length of the side of the triangle indicated below. Print the two answers to 3 decimal places onto the console screen Triangle 1 Read in the value of the angle, alpha, and the length, a, of the side indicated in the picture below. Calculate the length of the hypotenuse, c, of this right...

  • Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle,...

    Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle, Circle, and Parallelogram), then calculate area of the shape,must have input validation and uses more of java library like math class, wrapper class, string methods, use formatted output with the printf method. Finally, create a pseudo-code statement and flowchart

  • Write a program that prompts the user to enter three points (x1, y1), (x2, y2), (x3,...

    Write a program that prompts the user to enter three points (x1, y1), (x2, y2), (x3, y3) of a triangle and displays its area. The formula for computing the distance of two points (x1, y1) and (x2, y2) is d = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); or d = Math.pow((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1), 0.5); The formula for computing the...

  • Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3:...

    Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3: 5 Enter the base: 5 Enter the height: 8 --------------------- The triangle is Isosceles since it has two equal length sides. Isosceles triangle also has two equal angles The area is: 20 The permimeter is: 13 Continue? (y/n): y Enter length 1: 10 Enter length 2: 10 Enter length 3: 10 Enter the base: 10 Enter the height: 7 --------------------- The triangle is Equilateral...

  • java programe Write a program that prompts the user to enter in an integer number representing...

    java programe Write a program that prompts the user to enter in an integer number representing the number of elements in an integer array. Create the array and prompt the user to enter in values for each element using a for loop. When the array is full, display the following: The values in the array on a single line. The array with all of the elements reversed. The values from the array that have even numbered values. The values from...

  • solve it with Java please Write a Java program that prompts the user to input length...

    solve it with Java please Write a Java program that prompts the user to input length and width of a shape. If length is equal to width, call a method to calculate the area of the shape and send only one side as parameter to this method. This meth return the area of the shape. If length is not equal to width, call a method to calculate the area of the shape and send both width and length as parameters...

  • //////// ONLY JAVA **** //////// ONLY JAVA **** 5. In a right triangle, the square of...

    //////// ONLY JAVA **** //////// ONLY JAVA **** 5. In a right triangle, the square of the length of one side is equal to the sum of the squares of the lengths of the other two sides. Write a program that prompts the user to enter the lengths of three sides of a triangle and then outputs a message indicating whether the triangle is a right triangle. (Have the user enter the lengths 3, 4, and 5).

  • How to write a Java program that after prompting the user to enter the lengths of...

    How to write a Java program that after prompting the user to enter the lengths of sides (3) of a triangle, it prints: "Enter the lengths of the sides of triangle 1: 6 6 6" "A triangle with sides of 6,6,6 is equilateral."

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT