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

1. Write a program which reads as input a single character followed (on the next

ID: 3686977 • Letter: 1

Question

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

1. Using If-Else:

import java.util.Scanner;

public class AreaCalculator {

   public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
      
       char c;
      
       while(true){
           System.out.print("Enter a character(E to exit): ");
           c = sc.next().charAt(0); // reading a character
          
           if(c=='E' || c=='e')
               break;
           if(c=='C' || c=='c'){
              
               System.out.print("Enter radius: ");
               double radius = sc.nextDouble();
               double area = 3.14*radius*radius;
               System.out.printf("Area of circle of radius %.2f is %.2f ",radius,area);
           }
           else if(c=='T' || c=='t'){
               System.out.print("Enter base: ");
               double base = sc.nextDouble();
               System.out.print("Enter height: ");
               double height = sc.nextDouble();
               double area = 0.5*base*height;
               System.out.printf("Area of triangle of base %.2f and height is %.2f ",base,height,area);
           }
           else if(c=='R' || c=='r'){
               System.out.print("Enter width: ");
               double width = sc.nextDouble();
               System.out.print("Enter height: ");
               double height = sc.nextDouble();
               double area = width*height;
               System.out.printf("Area of rectangle of width %.2f and height is %.2f ",width,height,area);
           }
           else
               System.out.println("Invalid characcter!!");
       }
   }
}

2. Using Switch

import java.util.Scanner;

public class AreaCalculator2 {

public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
      
       char c;
       double area;
       while(true){
           System.out.print("Enter a character(E to exit): ");
           c = sc.next().charAt(0); // reading a character
          
           switch(c){
          

          
           case 'C':
           case 'c':
              
               System.out.print("Enter radius: ");
               double radius = sc.nextDouble();
               area= 3.14*radius*radius;
               System.out.printf("Area of circle of radius %.2f is %.2f ",radius,area);
               break;
          
           case 'T':
           case 't':
               System.out.print("Enter base: ");
               double base = sc.nextDouble();
               System.out.print("Enter height: ");
               double higt = sc.nextDouble();
               area = 0.5*base*higt;
               System.out.printf("Area of triangle of base %.2f and height is %.2f ",base,higt,area);
               break;
           case 'R':
           case 'r':
               System.out.print("Enter width: ");
               double width = sc.nextDouble();
               System.out.print("Enter height: ");
               double height = sc.nextDouble();
               area = width*height;
               System.out.printf("Area of rectangle of width %.2f and height is %.2f ",width,height,area);
          
           default:
               System.out.println("Invalid characcter!!");
           }
          
           if(c=='E' || c=='e')
               break;
       }
   }
}