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

How do I create this program? Without using if else if style of coding: For an e

ID: 3666942 • Letter: H

Question

How do I create this program?

Without using if else if style of coding:

For an entry of 1 calculate the area of a Circle use: Pi r ^2 You should ask for the radius of the circle then display the area and Pi is fixed at 3.14159.

For an entry of 2 calculate the area of a Rectangle use length * width. You should ask for the length and width the display the result.

For an entry of 4 calculate the area of a Triangle use base * height *.5 You should ask for the base and height and display the result.

For an entry of 4 stop the program.

Validate the number entered is between 1 and 4 and display the following statement: Error on selection. Please enter a number from 1 through 4

Validate the input numbers for calculation are positive numbers.

Explanation / Answer

import java.util.Scanner;

public class Shapes {
public static void main(String[] args) {

float pi=3.14;
System.out.println("Press 1 for calculate the area of a Circle");
System.out.println("Press 2 for calculate the area of a Rectangle");
System.out.println("Press 4 for calculate the area of a Triangle");
Scanner in=new Scanner(Sytem.in);
int num=in.nextInt();
switch (num) {
case 1: System.out.println("Enter radius of a circle");
float r=in.nextFloat();
float a1=pi*r*r;
System.out.println("the area of a Circle is"+a1);
break;
case 2: System.out.println("Enter length of a rectangle");
float l=in.nextFloat();
System.out.println("Enter width of a rectangle");
float w=in.nextFloat();

float a2=l*w;
System.out.println("the area of a rectangle is"+a2);
break;
case 3: System.out.println("Error on selection");
case 4: System.out.println("Enter base of a triangle");
float b=in.nextFloat();
System.out.println("Enter height of a triangle");
float h=in.nextFloat();

float a3=b*h;
System.out.println("the area of a triangle is"+a3);
break;

default: System.out.println("Please enter valid input");
break;
}
}
}

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