Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ANSWER WITH JAVA. ONLY ANSWER THIS IF YOU CAN ACTUALLY ANSWER RIGHT NOW. DO NOT

ID: 3687067 • Letter: A

Question

ANSWER WITH JAVA. ONLY ANSWER THIS IF YOU CAN ACTUALLY ANSWER RIGHT NOW. DO NOT SAY YOU WILL ANSWER IN x AMOUNTS OF HOURS. I NEED THEM NOW. 1. Write a program which reads as input a single character followed (on the next line) by one or two real numbers (depending on the character input). (There will be no more than one character or number on each input line.) Solve this problem once using if/else and solve it for the second time using switch statements. You must ask the user how many times he/she wants to repeat the run of the program. The character represents a shape — allowable inputs are C or c for circle - followed by one real number for the radius T or t for triangle – followed by two real numbers for base and height R or r for rectangle – followed by two real numbers for length and width Input the dimensions for the given shape and carry out the required arithmetic to calculate the area of the shape. If the single character is not one of the allowable ones, print a reasonable error message. Prompt appropriately for the inputs. Enter character: r Enter length: 7.5 Enter width: 10.0 The area of a rectangle with length = 7.5 and width = 10.0 is 75.0

Explanation / Answer

import java.util.Scanner;

/**
* @author Srinivas Palli
*
*/
public class AreaOfShape {

   public static void main(String[] args) {
       Scanner scanner = null;
       try {
           scanner = new Scanner(System.in);
           System.out.print("How many times want run:");
           int n = scanner.nextInt();
           for (int i = 1; i <= n; i++) {

               System.out.print("Enter character: ");
               char choice = scanner.next().charAt(0);
               switch (choice) {
               case 'c':
               case 'C': {
                   System.out.print("Enter radius :");
                   double radius = scanner.nextDouble();
                   double area = 3.141 * radius * radius;
                   System.out
                           .printf("The area of a Circle with radius = %.2f is %.2f ",
                                   radius, area);

                   break;
               }
               case 't':
               case 'T': {
                   System.out.print("Enter base :");
                   double base = scanner.nextDouble();
                   System.out.print("Enter height :");
                   double height = scanner.nextDouble();
                   double area = (base * height) / 2.0;
                   System.out
                           .printf("The area of a Triangle with base = %.2f and height = %.2f is %.2f ",
                                   base, height, area);

                   break;
               }
               case 'R':
               case 'r': {
                   System.out.print("Enter length :");
                   double length = scanner.nextDouble();
                   System.out.print("Enter width :");
                   double width = scanner.nextDouble();
                   double area = length * width;
                   System.out
                           .printf("The area of a Triangle with base = %.2f and height = %.2f is %.2f ",
                                   length, width, area);

                   break;
               }

               default:
                   System.out.println("Invalid Choice");
                   break;
               }
           }
       } catch (Exception e) {
           // TODO: handle exception
       }

   }
}

OUTPUT:

How many times want run:2
Enter character: r
Enter length :7.5
Enter width :10
The area of a Triangle with base = 7.50 and height = 10.00 is 75.00
Enter character: C
Enter radius :3
The area of a Circle with radius = 3.00 is 28.27

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote