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

this is in java, thanks for your help In this assignment, you must code an inter

ID: 2247033 • Letter: T

Question

this is in java, thanks for your help

In this assignment, you must code an interface named Shape with three methods and create a class named Rectangle that implements Shape.

The interface Shape represents a closed geometrical shape. It has three methods.

1. draw: This has no parameters and returns void. The method draws the shape on the screen.

2. getArea: The method has no parameters and returns the area of the shape. The return type is double.

3. getPerimeter: The method has no parameters and returns the perimeter of the shape. The return type is double.

The class Rectangle represents a rectangle and implements the Shape interface. Besides meeting that requirement, the class must satisfy the following criteria.

1. It must have exactly two double fields: width and height. Do not use any other identifiers for these two fields and do not define any other fields in the class.

2. It must have exactly one constructor that accepts the width and height in that order. It must store these values in the respective fields.

3. It must override toString and equals. You must use the standard Eclipse generated code for this.

The draw method must “draw” the rectangle using System.out.println. Simply print width number of asterisks in height number of lines. For example, see the following code and resultant output.

Shape rectangle1 = new Rectangle(4, 5); rectangle1.draw();

****

****

****

****

****

Please submit an Eclipse project as a zip file that opens correctly.

Explanation / Answer

import java.io.*;
import java.util.*;

interface Shape {
   public void draw();
   public double getArea();
   public double getPerimeter();
}

class Rectangle implements Shape {
    private double width;
    private double height;
   
    public Rectangle(double a, double b){
       width = a;
       height = b;
    }
    public void draw(){
        for (int i = 0; i<height; i++){
           for (int j =0; j<width; j++)
               System.out.print("*");
           System.out.println();
        }
    }
    public double getArea(){
         return width * height;
    }
    public double getPerimeter(){
         return 2 *(width + height);
    }

    public String toString(){
         String rep = "This is rectangle with width " + Double.toString(width) + " and height " + Double.toString(height);
         return rep;
    }
    public boolean equals(Object a){
         if (a == this) {
            return true;
         }
         if (!(a instanceof Rectangle)) {
            return false;
         }
         Rectangle c = (Rectangle) a;
         return Double.compare(width, c.width) == 0
                  && Double.compare(height, c.height) == 0;
    }
}

public class DemoRectangle {
     public static void main(String[] args){

         Shape rectangle1 = new Rectangle(4,5);

         rectangle1.draw();
     }
}