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

Exam 2 – Programming Exercise Write a small program that will calculate the area

ID: 3707661 • Letter: E

Question

Exam 2 – Programming Exercise

Write a small program that will calculate the area of shapes.

Write a Class Shape. That class should have the double attribute area.

Then write three sub-classes from Shape: Circle, Rectangle, and Square.

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

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, or 3, for Circle, Rectangle, and Square. Based on the input, ask for the attribute required to calculate the shape.

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

Java

Explanation / Answer

//Menu driven program using switch case:

import java.util.Scanner;

class Shape

{

public static void main(String args[])

{

Scanner s=new Scanner(System.in);

  

System.out.println("MENU:");

System.out.println("1.Area of a Circle");

System.out.println("2.Area of a Square");

System.out.println("3.Area of a Rectangle");

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

  

System.out.println("Enter your option:");

int op=s.nextInt();

switch(op)

{

case 1: System.out.println("Enter radius:");

double r=s.nextFloat();

double ac=3.14f*r*r;

System.out.println("Area:"+ac);

break;

case 2: System.out.println("Enter side:");

double x=s.nextInt();

double as=x*x;

System.out.println("Area:"+as);

break;

case 3: System.out.println("Enter length and breadth:");

double l=s.nextInt();

double b=s.nextInt();

double ar=l*b;

System.out.println("Area:"+ar);

break;

  

default:System.out.exit(0);

}

}

}

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