Question

Q2) Interface Create a program that calculates the perimeter and the area of any given 2D shape. The program should dynamical
0 0
Add a comment Improve this question Transcribed image text
Answer #1

package area;

import java.text.DecimalFormat;

public class TestShape2D {

   public static void main(String[] args) {
       Shape2D s=new Rectangle(4,3);
       s.area();
       s.perimeter();
       System.out.println(s);
       s=new Square(5);
       s.area();
       s.perimeter();
       System.out.println(s);
       s=new Circle(5);
       s.area();
       s.perimeter();
       System.out.println(s);
       s=new Tringle(4,2,3);
       s.area();
       s.perimeter();
       System.out.println(s);

   }

}
interface Shape2D{
  
   public double area();
   public double perimeter();
}
abstract class Quardilatral implements Shape2D{
   double area;
   double perimeter;
   Quardilatral(double a,double p){
       this.area=a;
       this.perimeter=p;
   }
   Quardilatral(){
      
   }
   abstract public double area();
   abstract public double perimeter();
      
  
}
class Circle implements Shape2D{
   double r;
   double area;
   double perimeter;
   Circle(double r){
       this.r=r;
   }
   public String toString(){
       return "Circle: D="+r+",P="+perimeter+",A="+area;
   }
   @Override
   public double area() {
       DecimalFormat df = new DecimalFormat("####0.00");
       area=(3.14*r*r)/4;
       area=Double.parseDouble(df.format(area));
       return area;
      
   }
   @Override
   public double perimeter() {
       DecimalFormat df = new DecimalFormat("####0.00");
       perimeter=3.14*r;
       perimeter=Double.parseDouble(df.format(perimeter));
       return perimeter;
      
   }
}
class Tringle implements Shape2D{
   double area;
   double perimeter;
   double a;
   double b;double c;
   Tringle(double a,double b,double c){
       this.a=a;
       this.b=b;
       this.c=c;
   }
   public String toString(){
       return "Tringle: a="+a+",b="+b+",c="+c+",Perimeter="+perimeter+",Area="+area;
   }

   @Override
   public double area() {
       //to find base and height
       double shotest=Math.min(Math.min(a, b), Math.min(b, c)); //min of all three
       double secShortest=0; //second min
       if(a==shotest){
           secShortest=Math.min(b, c);
       }
       else if(b==shotest){
           secShortest=Math.min(a, c);
       }
       else if(c==shotest){
           secShortest=Math.min(a, b);
       }
       DecimalFormat df = new DecimalFormat("####0.00");
       area=0.5*shotest*secShortest;
       area=Double.parseDouble(df.format(area));
       return area;
  
   }

   @Override
   public double perimeter() {
       DecimalFormat df = new DecimalFormat("####0.00");
       perimeter=a+b+c;
       perimeter=Double.parseDouble(df.format(perimeter));

       return perimeter;
   }
  


}
class Rectangle extends Quardilatral{

   Rectangle(double l,double b){
       this.l=l;
       this.b=b;
   }
   double l,b;
   @Override
   public double area() {
       DecimalFormat df = new DecimalFormat("####0.00");

       area=l*b;
       area=Double.parseDouble(df.format(area));
       return area;
      
   }

   @Override
   public double perimeter() {
       DecimalFormat df = new DecimalFormat("####0.00");
       perimeter=2*(l+b);
       perimeter=Double.parseDouble(df.format(perimeter));
       return perimeter;
      
   }
   public String toString(){
       return "Rectangle: l="+l+",b="+l+",Perimeter="+perimeter+",Area="+area;
   }
  
}
class Square extends Quardilatral{

   double l;
   Square(double l){
       this.l=l;
   }
   @Override
   public double area() {
       DecimalFormat df = new DecimalFormat("####0.00");
       area=l*l;
       area=Double.parseDouble(df.format(area));
       return area;
   }

   @Override
   public double perimeter() {
       DecimalFormat df = new DecimalFormat("####0.00");
       perimeter=4*l;
       perimeter=Double.parseDouble(df.format(perimeter));

       return perimeter;
   }
   public String toString(){
       return "Square: l="+l+",Perimeter="+perimeter+",Area="+area;
   }
  
}

output

Rectangle: l=4.0,b=4.0,Perimeter=14.0,Area=12.0
Square: l=5.0,Perimeter=20.0,Area=25.0
Circle: D=5.0,P=15.7,A=19.62
Tringle: a=4.0,b=2.0,c=3.0,Perimeter=9.0,Area=3.0

Add a comment
Know the answer?
Add Answer to:
Q2) Interface Create a program that calculates the perimeter and the area of any given 2D...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • Create a program that calculates the area of various shapes. Console Specifications Create an abstract class...

    Create a program that calculates the area of various shapes. Console Specifications Create an abstract class named Shape. This class should contain virtual member function named get_area() that returns a double type. Create a class named Circle that inherits the Shape class and contains these constructors and member functions: Circle(double radius) double get_radius() void set_radius(double radius) double get_area() Create a class named Square that inherits the Shape class and contains these constructors and member functions: Square(double width) double get_width() void...

  • Please use C++ and Please do all file separate separately. And also I need output too....

    Please use C++ and Please do all file separate separately. And also I need output too. thanks. Please do it complete work. ShapeNodePoly.cpp Avaliable from: Wednesday, July 27, 2016, 10:20 AM Requested files: Point.h, Point.cpp, Shape.h, Shape.cpp, Polygon.h, Polygon.cpp, Ellipse.h, Ellipse.cpp, ShapeNodePoly.cpp, ShapeNodePoly_test.cpp (Download) Type of work: Individual work In this assignment, you will create a class that can behave as any of the shapes listed below. You will use object inheritance to enable all shapes to be managed using...

  • Using Python Write the following well-documented (commented) program. Create a class called Shape that has a...

    Using Python Write the following well-documented (commented) program. Create a class called Shape that has a method for printing the area and the perimeter. Create three classes (Square, Rectangle, and Circle) which inherit from it. Square will have one instance variable for the length. Rectangle will have two instance variables for the width and height. Circle will have one instance variable for the radius. The three classes will have methods for computing the area and perimeter of its corresponding shape....

  • C++ ONLY PLEASE Problem Description: Objective: Create Inheritance Hierarchy to Demonstrate Polymorphic Behavior Create a Rectangle...

    C++ ONLY PLEASE Problem Description: Objective: Create Inheritance Hierarchy to Demonstrate Polymorphic Behavior Create a Rectangle Class Derive a Square Class from the Rectangle Class Derive a Right Triangle Class from the Rectangle Class Create a Circle Class Create a Sphere Class Create a Prism Class Define any other classes necessary to implement a solution Define a Program Driver Class for Demonstration a) Create a Container to hold objects of the types described above b) Create and Add two(2) objects...

  • Please show all work and answer all parts using python, thanks! Instructions You will need to...

    Please show all work and answer all parts using python, thanks! Instructions You will need to create four files: • Shape2D.py - file containing a class definition containing properties all Shapes could possibly have. • Circle.py - file containing a class definition of a Circle that inherits from the Shape2D class. Square.py - file containing a class definition of a Square that inherits from the Shape2D class. • testFile.py - file containing pytest functions testing the Shape2D, Circle, and Square...

  • Define an interface named Shape with a single method named area that calculates the area of...

    Define an interface named Shape with a single method named area that calculates the area of the geometric shape:        public double area(); Next, define a class named Circle that implements Shape. The Circle class should have an instance variable for the radius, a constructor that sets the radius, an accessor and a mutator method for the radius, and an implementation of the area method. Define another class named Rectangle that implements Shape. The Rectangle class should have instance variables...

  • Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...

    Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...

  • You will be creating a driver class and 5 class files for this assignment. The classes...

    You will be creating a driver class and 5 class files for this assignment. The classes are Shape2D (the parent class), Circle, Triangle, and Rectangle (all children of Shape2D) and Square (child of Rectangle). Be sure to include a message to the user explaining the purpose of the program before any other printing to the screen. Within the driver class, please complete the following tasks: Instantiate a Circle object with 1 side and a radius of 4; print it Update...

  • Create a new package in assignment called shapes. All work for this part should be done...

    Create a new package in assignment called shapes. All work for this part should be done in the shapespackage. Create a new interface called Shape. Add double area() and double perimter()methods to the Shape interface. Create two classes called Circle and Rectanglewhich implement Shape. Add reasonable fields to the Circle and Rectangle classes which will allow you to calculate the area and perimeter of each shape. Add constructors to Circle and Rectangle so you can initialize your fields. Implement the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT