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

Design and implement a reusable program that takes any standard Shape and return

ID: 3863128 • Letter: D

Question

Design and implement a reusable program that takes any standard Shape and returns a Rectangle that completely in closes the Shape. I expect you to write a class that defines a method to handle a shape. Let's name the class as MyClass and the method as reusable. For simplicity, consider only Rectangle2^D. Double and Line2D.Double whenever you need to show specific Shapes in your answer. You don't have to concern about drawing shapes for this question. Your job is to answer the subquestions (a) through (d). API is on the last page. (a) Identify required class(s) and interface(s) to solve the problem and draw the corresponding class diagram to depict their relationship. Include specific Shapes in your class diagram. (b) Implement the reusable program MyClass. (c) Write a tester that calls the reusable method with two different specific Shape objects. public class Tester {public static void main (String [] args) {//your work goes here.}} (d) Briefly explain how polymorphism is used in the execution of the reusable method.

Explanation / Answer

a) Rectangle and Circle both are Shape.

So we selected Rectangle and Circle as Classes and Shape as interface for both, becouse shape has common funtionality fot both.

Classes Rectangle is a Shape and Circle is a Shape so both has "IS A" relationship with Shape

b)Implematation =>

//================== Shape.java ===========================//

public interface Shape {
   public double getArea();
   public double perimeter();
}

//========================== Rectangle.java ===========================//

public class Rectangle implements Shape {
   private double height;
   private double length;
  
   public Rectangle() {
      
   }

   public Rectangle(double h,double l) {
       height = h;
       length = l;
   }
  
   @Override
   public double getArea() {
       return height*length;
   }

   @Override
   public double perimeter() {
       return 2.0d*(height+length);
   }

   public double getHeight() {
       return height;
   }

   public void setHeight(double height) {
       this.height = height;
   }

   public double getLength() {
       return length;
   }

   public void setLength(double length) {
       this.length = length;
   }
}

//============================= Circle.java ========================//

public class Circle implements Shape {
   private double radius;
   final private double PI = 22.0d/7.0d;
   public Circle() {
      
   }
   public Circle(double r) {
       radius = r;
   }
   @Override
   public double getArea() {
       return PI*radius*radius;
   }
  
   @Override
   public double perimeter() {
       return 2.0*PI*radius;
   }
   public double getRadius() {
       return radius;
   }
   public void setRadius(double radius) {
       this.radius = radius;
   }
   public double getPI() {
       return PI;
   }

}

c) Test Program

//============================= ReusableTest.java==================================//

public class ReusableTest {

   public static void main(String[] args) {
       Shape[] shapes = new Shape[2];
       shapes[0] = new Rectangle(40.0d, 30.0d);
       shapes[1] = new Circle(20.0d);
       for(int i = 0;i<2;i++){
           System.out.println("Shape "+i+" Area :"+shapes[i].getArea());
           System.out.println("Shape "+i+" Perimeter :"+shapes[i].perimeter());
       }
   }

}

d)

We have careated Shape refrence variable and storing the object of circle and rectangel

by using shape only we are calling method area and perimeter but actual method is called from respective objcet only, so that is the example of run time polimorphysm

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