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: 3707660 • 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

PROGRAM

import java.util.Scanner;

// Create class Shape

class Shape

{

protected double area; // one double attribute area

}

// Create base class Circle

class Circle extends Shape

{

public double areaCircle(double radius) // Declare and implement areaCircle() method

{

area= Math.PI*radius*radius; // calculate area of circle and assign result to "area"

return area; // return area of Circle

}

}

// Create another base class Rectangle

class Rectangle extends Shape

{

public double areaRectangle(double sideA,double sideB) // Declare and implement areaRectangle() method

{

area=sideA*sideB; // calculate area of rectangle and assign result to "area"

return area; // return area of Rectangle

}

}

// Create another base class Square

class Square extends Shape

{

public double areaSquare(double side) // Declare and implement areaSquare() method

{

area=side*side; // calculate area of Square and assign result to "area"

return area; // return area of Square

}

}

class ShapeExample

{

// Create static method menu()

public static int menu()

{

int ch; // Declare integer variable ch for choice

Scanner scr=new Scanner(System.in); // Create Scanner Object scr

// Display Menu items

System.out.println("1. Circle");

System.out.println("2. Rectangle");

System.out.println("3. Square");

System.out.println("4. Exit");

System.out.print("Which shape you Want: ");

ch=scr.nextInt(); // read choice

return ch; // return choice value

}

public static void main(String args[])

{

Scanner scr=new Scanner(System.in); // Create Scanner Object scr

Circle c=new Circle(); // Create Circle Object c

Rectangle r=new Rectangle(); // Create Rectangle Object r

Square s=new Square(); // Create Square Object s

double area; // Declare double variable area

while(true) // create infinity while loop

{

int ch=menu(); // calling menu() method

switch(ch) // create switch statement

{

case 1:

System.out.print("Enter Radius: ");

double rad=scr.nextDouble(); // read radius "rad"

area=c.areaCircle(rad); // calling areaCircle() method from Circle Object "c"

System.out.println("Area of Circle: "+area); // display area of circle

break;

case 2:

System.out.print("Enter Side-1: ");

double s1=scr.nextDouble(); // read side-1

System.out.print("Enter Side-2: ");

double s2=scr.nextDouble(); // read side-2

area=r.areaRectangle(s1,s2); // calling areaRectangle() method from Rectangle Object "r"

System.out.println("Area of Rectangle: "+area); // display area of Rectangle

break;

case 3:

System.out.print("Enter Side: ");

double side=scr.nextDouble(); // read side

area=s.areaSquare(side); // calling areaSquare() method from Square Object "s"

System.out.println("Area of Square: "+area); break;

case 4: System.out.println("Thank You");System.exit(0); // exit from program

break;

default: System.out.println("You Enter Wrong Choice....");

}

}

}

}

OUTPUT

F:>javac ShapeExample.java

F:>java ShapeExample
1. Circle
2. Rectangle
3. Square
4. Exit
Which shape you Want: 1
Enter Radius: 3.6
Area of Circle: 40.71504079052372
1. Circle
2. Rectangle
3. Square
4. Exit
Which shape you Want: 2
Enter Side-1: 1.2
Enter Side-2: 3.5
Area of Rectangle: 4.2
1. Circle
2. Rectangle
3. Square
4. Exit
Which shape you Want: 3
Enter Side: 5.6
Area of Square: 31.359999999999996
1. Circle
2. Rectangle
3. Square
4. Exit
Which shape you Want: 4
Thank You

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