I need a java code for this program ... Write a small program that will calculat
ID: 3707646 • 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
import java.util.*;
class Shape // base class
{
public double area;
public Shape() //default constructor
{
area = 0.0;
}
}
class Square extends Shape
{
private double side;
public Square()
{
side = 0.0;
}
public Square(double side)
{
this.side = side;
}
public void print()
{
area = side*side;
System.out.printf("SideLength : %.2f Area : %.2f",side,area);
}
}
class Rectangle extends Shape //derived class
{
private double sideA,sideB;
public Rectangle()
{
sideA = 0.0;
sideB = 0.0;
}
public Rectangle(double sideA, double sideB)//argument constructor
{
this.sideA = sideA;
this.sideB = sideB;
}
public double getSideA()
{
return sideA;
}
public double getSideB()
{
return sideB;
}
public void print()
{
area = sideA*sideB;
System.out.printf("Length : %.2f Width : %.2f Area : %.2f",sideA,sideB,area);
}
}
class Circle extends Shape
{
private double radius;
public Circle()
{
radius = 1;
}
public Circle(double radius)
{
this.radius = radius;
}
public double getRadius()
{
return radius;
}
public void print()
{
area = 3.14*radius*radius;
System.out.printf("Radius: %.2f Area :%.2f ",radius,area);
}
}
class TestShapes
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
int choice;
double radius,side,sideA,sideB;
String option = "y";
do
{
System.out.println("Enter the choice (1.Square, 2.Rectangle or 3.Circle): ");
choice = input.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter the side length of the square:");
side = input.nextDouble();
Square s = new Square(side);
s.print();
break;
case 2:
System.out.println("Enter the length of the rectangle: ");
sideA = input.nextDouble();
System.out.println("Enter the width of the rectangle: ");
sideB = input.nextDouble();
Rectangle r = new Rectangle(sideA,sideB);
r.print();
break;
case 3:
System.out.println("Enter the radius of the circle: ");
radius = input.nextDouble();
Circle c = new Circle(radius);
c.print();
break;
default: System.out.println("Invalid option");
break;
}
System.out.println(" Do you want to calculate another Shape? : <y/n> ");
option = input.next();
if(option.charAt(0) != 'y')
break;
}while(option.charAt(0) == 'y');
}
}
Output:
Enter the choice (1.Square, 2.Rectangle or 3.Circle):1
Enter the side length of the square:5.6
SideLength : 5.60
Area : 31.36
Do you want to calculate another Shape? : <y/n>y
Enter the choice (1.Square, 2.Rectangle or 3.Circle):2
Enter the length of the rectangle:4.5
Enter the width of the rectangle:6.2
Length : 4.50 Width : 6.20
Area : 27.90
Do you want to calculate another Shape? : <y/n>y
Enter the choice (1.Square, 2.Rectangle or 3.Circle):3
Enter the radius of the circle:4.4
Radius: 4.40
Area :60.79
Do you want to calculate another Shape? : <y/n> ?n
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.