Question

Write a program that requests an input (P for perimeter and A for area) then asks...

Write a program that requests an input (P for perimeter and A for area) then asks the user for the dimensions (length & width) of a rectangular pool. The program computes and displays either the perimeter or the area of the pool.

Homework Answers

Answer #1

Since you did not mention the language, Here is the completed code for this problem in Java. If it is any other language, just mention in comments. Also, don’t forget to include the language next time you post a question. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Main.java

import java.util.Scanner;

public class Main {

      public static void main(String[] args) {

            // scanner to read user input

            Scanner scanner = new Scanner(System.in);

            // asking the user to enter P or A

            System.out

                        .println("What do you want to calculate? Enter P for perimeter or A for area: ");

            // reading choice, converting to upper case and extracting first

            // character

            char choice = scanner.next().toUpperCase().charAt(0);

            // if choice is 'P' or 'A', proceeding to read dimensions

            if (choice == 'P' || choice == 'A') {

                  System.out.println("Enter length and width of the rectangle: ");

                  // reading dimensions

                  double length = scanner.nextDouble();

                  double width = scanner.nextDouble();

                  // if choice is 'P', finding and displaying perimeter

                  if (choice == 'P') {

                        // formula = 2(length+width)

                        double peri = 2 * (length + width);

                        System.out.println("Perimeter is " + peri);

                  }

                  // otherwise finding and displaying area

                  else {

                        //formula = length x width

                        double area = length * width;

                        System.out.println("Area is " + area);

                  }

            } else {

                  //any other input is invalid

                  System.out.println("Invalid choice!");

            }

      }

}

/*OUTPUT 1*/

What do you want to calculate? Enter P for perimeter or A for area:

P

Enter length and width of the rectangle:

10 20

Perimeter is 60.0

/*OUTPUT 2*/

What do you want to calculate? Enter P for perimeter or A for area:

a

Enter length and width of the rectangle:

100 50

Area is 5000.0

Know the answer?
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
Similar Questions
Please write a program to calculate the area of a circle. Area =3.14*Radius*Radius. Input Radius. Please...
Please write a program to calculate the area of a circle. Area =3.14*Radius*Radius. Input Radius. Please write a program to calculate the area of a rectangle. Area =Width*Length. Input Width and Length. Please write a program to covert Fahrenheit degree to Celsius degree. The formula is C=(F-32)*5/9. Input Fahrenheit degree. Please write a program to covert Celsius degree to Fahrenheit degree. The formula is F=C*9/5+32. Input Celsius degree.
Write a Python program to calculate the area of a circle. The user needs to input...
Write a Python program to calculate the area of a circle. The user needs to input the value of the radius of the circle. area = 3.14 x radius x radius You should use a function named circlearea to perform the calculation 3. Write a Python program to calculate the area of a square. The user needs to input the value of the length of the square. area = len*len You should use a function named squarearea to perform the...
Problem a (LA2a.java) Write a program to compute the area, perimeter, and interior angle of a...
Problem a (LA2a.java) Write a program to compute the area, perimeter, and interior angle of a regular polygon. First, have the user supply the number of sides of the polygon (a whole number, which must be 3 or greater) and the side length (any positive number). Then, compute the area via the following equation: where n is the number of sides and s is the side length. Note that the equations are equivalent, with the first using degrees and the...
of all rectangles with a perimeter of 13​, which one has the maximum​ area? (Give the​...
of all rectangles with a perimeter of 13​, which one has the maximum​ area? (Give the​ dimensions.) The rectangle that has the maximum area has length ___ and width ___
A rectangle has an area of 100 cm 2 and a perimeter of 50 cm. What...
A rectangle has an area of 100 cm 2 and a perimeter of 50 cm. What are its dimensions? It's length is ___. It's width is ____.
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the area of a rectangle * based on the width and length entered by the user. */ public class RectangleArea2 {             public static void main(String[] args) { int length; //longer side of rectangle             int width; //shorter side of rectangle int area; //calculated area of rectangle Scanner input = new Scanner(System.in);                               System.out.print("Enter the length: ");            length = input.nextInt(); System.out.print("Enter...
Write a C program that asks the user to enter two integers x and n. Then...
Write a C program that asks the user to enter two integers x and n. Then the program computes xn (=x * x * x …… (n times)) using for loop. using printf scanf
Write a program in C++ coding that asks the user to input an integer between 25...
Write a program in C++ coding that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2...
Write a program that asks the user to input an integer between 25 and 50, inclusive....
Write a program that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2 relational expressions. You...
write a program that asks the User to enter a letter, then asks the user to...
write a program that asks the User to enter a letter, then asks the user to enter a sentence. the program will then print out how many time that letter occurred in the sentence. in C Language