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

I need a java code for this program ... Write a small program that will calculat

ID: 3707647 • Letter: I

Question

I need a java code for this program ...

Write a small program that will calculate the area of shapes.Write a Class shape.that class should have double attribute area. Then write three sub classes from Shape:Circle Rectangle and square.

The sub classes should have required attributes to calculate the respective area:

Circle has a double attribute radius,Rectangle has a double attribute sideA and sideB, and Square has a double attribute side.

In the main program, ask the user which shape they want to calculate: 1, 2 3 for Circle, Rectangle, and Square. Based on the input, ask for the attribute required to calculate the shape,

Once user has entered the required information, output area of the hsape, and ask the user if they want to calculate another shape.

Explanation / Answer


package chegg;

import java.util.Scanner;
public class Shape
{
  
double area;
public static void main(String[] args)
{
char ch=''; //Local Variable should be initialised
do
{
Scanner input=new Scanner(System.in);
System.out.println("MENU:"); //Menu for chpoosing the option
System.out.println("Press 1 Area of a Circle");
System.out.println("Press 2 Area of a Rectangle");
System.out.println("Press 3 Area of a Square");
int choice = input.nextInt();
switch(choice)
{
case 1: circleArea circ=new circleArea(); // Calculating area of Circle
circ.calcArea();
break;
case 2: rectArea rec =new rectArea(); // Calculating area of Rectangle
rec.calcArea();
break;
case 3: squrArea squa=new squrArea(); // Calculating area of Sqaure
squa.calcArea();
default: System.out.println("Wrong Choice");
}
System.out.println("Do you want to Calculate for another Shape Y or N");
Scanner nxtChoice = new Scanner(System.in);
ch=nxtChoice.next().charAt(0);
}while(ch=='Y'||ch=='y');

}
  
}
class circleArea extends Shape // //Code to calcaulate area of Circle
{
  
void calcArea()
{ Scanner inp= new Scanner(System.in);   
System.out.println("Enter the radius:");
double r= inp.nextDouble();
double area=(22*r*r)/7 ;
System.out.println("Area of Circle is: " + area);   
}
}
class rectArea extends Shape //Code to calcaulate area of Rectangle
{
void calcArea()
{
Scanner inp= new Scanner(System.in);   
System.out.println("Enter the length:");
double length= inp.nextDouble();
System.out.println("Enter the length:");
double breadth= inp.nextDouble();
  
double area=length*breadth;
System.out.println("Area of Rectangle is: " + area);
}

}
class squrArea extends Shape //Code to calcaulate area of Square
{
void calcArea()
{
Scanner inp= new Scanner(System.in);   
System.out.println("Enter the side:");
double side= inp.nextDouble();
double area=side*side;
System.out.println("Area of Square is: " + side);
}
}

Sample output

MENU:
Press 1 Area of a Circle
Press 2 Area of a Rectangle
Press 3 Area of a Square
1
Enter the radius:
5
Area of Circle is: 78.57142857142857
Do you want to Calculate for another Shape Y or N
y
MENU:
Press 1 Area of a Circle
Press 2 Area of a Rectangle
Press 3 Area of a Square
2
Enter the length:
5
Enter the length:
6
Area of Rectangle is: 30.0
Do you want to Calculate for another Shape Y or N

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