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

In this assignment you will create 2 interfaces: Shape and Printable and you wil

ID: 3847144 • Letter: I

Question

In this assignment you will create 2 interfaces: Shape and Printable and you will modify the classes Rectangle, Square,IsoscelesRightTriangle, and Circle so that they implement one or both of the interfaces.In addition, you will create a class called InterfaceApp (different from InheritanceApp). This class includes the main method and demonstrates the polymorphic behavior of interfaces.Declare the interfaces according to the UML diagrams:
ad interface Shape:

perimeter .. returns the perimeter of the shape

area .. returns the area of the shape

ad interface Printable:

print … prints the outline of the shape with stars (*)

within a line the stars are always separated by a single space (blank) The rectangle is printed with the length on the x-axis (more wide than high) In case of the triangle the right angle is on the bottom left ( see output ) The output produced by the print method needs to reflect the actual field values

Modify the four shape classes Rectangle, Square, IsoscelesRightTriangle, and Circle so that
all of them implement Shape

all except Circle implement Printable

ad InterfaceApp:
This class includes the main method.
This assignment does NOT accept user input

Create instances of Rectangle, Square, IsoscelesRightTriangle, and Circle and assigns them to variables of type Rectangle, Square, IsoscelesRightTriangle, and Circle.The rectangle has length 6 and width 3 The square has a side length of 5 The triangle has a leg size of 6 The circle has a radius of 5

All arguments passed to the constructors are hard-coded in the main method.

    
For each of the 4 instances print the object (toString)For Circle calculate the diameter and circumference and print the results

Print the string “Shape Array: “ and underline it with dashes

Create an array of the interface type Shape

Initialize it with the four shape instances we created above

Loop through all the elements of the array. For each of the array elements do the following:

print the shape (toString)

print the perimeter

print the area

if the shape is printable, print the shapeHint: use instance of to find out whether a shape is printable

print a new line to structure the outputThe output should look like the output provided on the right

«Interface Shape perimeter double area double

Explanation / Answer

interfaceApp.java

import java.lang.*;
interface Shape{
double perimeter();
double area();
}
interface Printable{
void print();
}
class Rectangle implements Shape, Printable
{
   int len,bre,peri,are;
   Rectangle(int l,int b){
       len = l;
       bre = b;
   }
   public double perimeter()
   {
       return len * bre;
   }
   public double area(){
       return len * bre;
   }
   public void print()
   {
       for(int i=0;i<bre;i++){
           if(i==0 || i==bre-1){
           for(int j=0;j<len;j++){
               System.out.print("* ");
           }
           }
           else{
               System.out.print("*");
               for(int k=0;k<len-1;k++)System.out.print(" ");
               System.out.print("*");
           }
           System.out.print(" ");
           }
       }
   public String toString(){
       return "Rectangle(6X3)";
   }
}
class Square implements Shape, Printable
{
   int len,peri,are;
   public Square(int l){
       len = l;
   }
   public double perimeter()
   {
       return len * len;
   }
   public double area(){
       return len * len;
   }
   public void print()
   {
       for(int i=0;i<len;i++){
           if(i==0 || i==len-1){
           for(int j=0;j<len;j++){
               System.out.print("* ");
           }
           }
           else{
               System.out.print("*");
               for(int k=0;k<len-1;k++)System.out.print(" ");
               System.out.print("*");
           }
           System.out.print(" ");
           }
   }
   public String toString(){
       return "Square(5)";
   }
}

class IsoscelesRightTriangle implements Shape, Printable
{
   int h;
   double peri,are;
   public IsoscelesRightTriangle(int he){
       h= he;
   }
   public double perimeter(){
       return h + h + Math.sqrt((6*6)+(6*6));
   }
   public double area()
   {
       return (double)(h*h)/2;
   }
   public void print(){
       for(int i=0;i<h;i++){
           for(int j=0;j<i;j++){
               System.out.print("* ");
           }
           System.out.print(" ");
       }
   }
   public String toString(){
       return "Triangle(6)";
   }
}
class Circle implements Shape
{
   int r,dia;
   double peri,are;
   public Circle(int rad){
       r = rad;
   }
   public double perimeter(){
       return 2 * 3.14 * r;
   }
   public double area(){
       return 3.14 * r * r;
   }
   public double diameter(){
       return r * 2;
   }
   public String toString(){
       return "Circle(5)";
   }
}
public class InterfaceApp {
   public static void main(String[] args) {
       Shape []sh = new Shape[3];
       Printable []p = new Printable[3];
       Rectangle rect = new Rectangle(6, 3);
       Square squ = new Square(5);
       IsoscelesRightTriangle is = new IsoscelesRightTriangle(6);
       Circle cir = new Circle(5);
       System.out.println("Diameter:"+cir.diameter()+" Perimeter:"+cir.perimeter()+" Area:"+cir.area());
       System.out.println(cir);
       System.out.print("Shape Array: -------- ");
       sh[0] = squ;
       sh[1] = rect;
       sh[2] = is;
       p[0] = squ;
       p[1] = rect;
       p[2] = is;
       for(int i=0;i<sh.length;i++){
           System.out.println(sh[i]+" Perimeter:"+sh[i].perimeter()+" Area:"+sh[i].area());
           if(p[i] instanceof Printable){
               p[i].print();
           }
       }
      
   }
}