I need a java code for this program ... Write a small program that will calculat
ID: 3707667 • 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
Shape.java
package Shapes;
public interface Shape {
public double Area();
}
Rectangular.java
package Shapes;
public class Rectangular implements Shape{
public double x;
public double y;
public Rectangular(double x, double y) {
super();
this.x = x;
this.y = y;
}
/**
* @return the x
*/
public double getX() {
return x;
}
/**
* @param x the x to set
*/
public void setX(double x) {
this.x = x;
}
/**
* @return the y
*/
public double getY() {
return y;
}
/**
* @param y the y to set
*/
public void setY(double y) {
this.y = y;
}
@Override
public double Area() {
// TODO Auto-generated method stub
return this.x *this.y;
}
}
Square.java
package Shapes;
public class Square implements Shape{
public double side ;
@Override
public double Area() {
// TODO Auto-generated method stub
return this.side*this.side;
}
public Square(double side) {
super();
this.side = side;
}
/**
* @return the side
*/
public double getSide() {
return side;
}
/**
* @param side the side to set
*/
public void setSide(double side) {
this.side = side;
}
}
Circle.java
package Shapes;
import java.util.Arrays;
public class Circle implements Shape{
public double radius;
public Circle(double r){
this.radius= r;
}
/**
* @return the radius
*/
public double getRadius() {
return radius;
}
/**
* @param radius the radius to set
*/
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double Area() {
// TODO Auto-generated method stub
return 3.14*this.radius*this.radius;
}
}
Main.java
package Shapes;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int choice =0 ;
do{
System.out.println("enter choice 1 for rectangle 2 for square 3 for circle ");
choice = scan.nextInt();
if(choice ==1 ){
double side1;
double side2;
System.out.println("enter side 1");
side1 = scan.nextDouble();
System.out.println("enter side 2");
side2 = scan.nextDouble();
Rectangular rect= new Rectangular(side1, side2);
System.out.println("area of rectangel is :"+rect.Area());
System.out.println("do you want to cintinue if yes then enter 4 else enter choice");
choice = scan.nextInt();
}
if(choice ==1 ){
double side;
System.out.println("enter side 1");
side = scan.nextDouble();
Square d = new Square(side);
System.out.println("area of square is :"+d.Area());
System.out.println("do you want to cintinue if yes then enter 4 else enter choice");
choice = scan.nextInt();
}
if(choice ==3 ){
double radius;
System.out.println("enter side 1");
radius = scan.nextDouble();
Circle c= new Circle(radius);
System.out.println("area of circle is :"+c.Area());
System.out.println("do you want to cintinue if yes then enter 4 else enter choice");
choice = scan.nextInt();
System.out.println(choice);
System.out.println("*****");
}
}while(choice != 4);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.