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

Description: This assignment demonstrates the use of interfaces and how they can

ID: 3586498 • Letter: D

Question

Description: This assignment demonstrates the use of interfaces and how they can be used in combination with inheritance Use the four shapes that you implemented in Assignment Inheritance as a starting point I recommend creating a copy before you implement any changes. 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: Interface Shape: «Interface» Shape perimeter(): double area(): double perimeter .. returns the perimeter of the shape area.. returns the area of the shape Interface Printable: print... prints the outline of the shape with small circles (see output) «Interface» Printable 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 print() Modify the four shape classes Rectangle, Square, IsoscelesRightTriangle, and Circle so that . all of them are a Shape . all except Circle are a Printable

Explanation / Answer

Shape.java

public interface Shape {

double perimeter();

double area();

}

______________

Printable.java

public interface Printable {
void print();
}

______________

Rectangle.java

public class Rectangle implements Shape, Printable {

//Declaring instance variables
private double height;
private double width;


//Parameterized constructor
public Rectangle(double height, double width) {
this.height = height;
this.width = width;
}


//Calculating the Perimeter of Rectangle
@Override
public double perimeter() {

return 2 * (height + width);
}


//Calculating the area of Rectangle
@Override
public double area() {

return height * width;
}

@Override
public void print() {
char ch = 'o';


for (int i = 1; i <= width; i++) {
for (int j = 1; j <= height; j++) {
if ((i == 1 || i == width) || (j == 1 || j == height))
System.out.print(ch + " ");
else
System.out.print(" ");

}
System.out.println();
}

}

//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Rectangle (" + height + "X" + width + ")";
}

}

________________

Square.java

public class Square implements Shape, Printable {

//Declaring instance variables
private double side;


//Parameterized constructor
public Square(double side) {
this.side = side;
}


//Calculating the Perimeter of Square
@Override
public double perimeter() {

return 4 * side;
}


//Calculating the area of Square
@Override
public double area() {

return side * side;
}

@Override
public void print() {
for (int i = 1; i <= side; i++) {
for (int j = 1; j <= side; j++) {
if ((i == 1 || i == side) || (j == 1 || j == side))
System.out.print(" o ");
else
System.out.print(" ");

}
System.out.println();
}

}

//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Square (" + side + ")";
}

}

_________________

IsoscelesRightTriangle.java

public class IsoscelesRightTriangle implements Shape, Printable {

//Declaring instance variables
double side;


//Parameterized constructor
public IsoscelesRightTriangle(double side) {
this.side = side;
}

//Calculating the Perimeter of IsoscelesRightTriangle
@Override
public double perimeter() {

return (Math.PI * side + 2 * side);
}

//Calculating the area of IsoscelesRightTriangle
@Override
public double area() {

return Math.pow(side, 2) / 2.0;
}


@Override
public void print() {

for (int i = 1; i <= side; i++) {
for (int j = 1; j <= i; j++) {
if (i == 1 || i == 2 || i == side || j == 1 | j == i)
System.out.print("o ");
else
System.out.print(" ");
}
System.out.println();
}

}

//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "IsoscelesRightTriangle (" + side + ")";
}

}

________________

Circle.java

public class Circle implements Shape {

//Declaring instance variables
double radius;


//Parameterized constructor
public Circle(double radius) {
this.radius = radius;
}

//Calculating the Perimeter of Circle
@Override
public double perimeter() {

return 2 * Math.PI * radius;
}


//Calculating the area of Circle
@Override
public double area() {

return Math.PI * radius * radius;
}

@Override
public String toString() {
return "Cirlce (" + radius + ")";
}
}

__________________

InterfaceApp.java

import java.text.DecimalFormat;

public class InterfaceApp {

public static void main(String[] args) {

// DecimalFormat class is used to format the output

DecimalFormat df = new DecimalFormat("#.#");

// Creating an Rectangle object

Rectangle r1 = new Rectangle(6, 3);

// Creating an Rectangle object

Rectangle r2 = new Rectangle(5, 4);

// Creating an Square object

Square s1 = new Square(5);

// Creating an Square object

Square s2 = new Square(5);

// Creating an IsoscelesRightTriangle object

IsoscelesRightTriangle irt1 = new IsoscelesRightTriangle(5);

// Creating an IsoscelesRightTriangle object

IsoscelesRightTriangle irt2 = new IsoscelesRightTriangle(3);

// Creating an Circle object

Circle c1 = new Circle(7);

// Creating an Circle object

Circle c2 = new Circle(2);

// create an Shape array

Shape shArr[] = { r1, r2, s1, s2, irt1, irt2, c1, c2 };

System.out.println("Shape Array :");

System.out.println("---------- :");

/*

* Iterating over the array and display perimeter and area and if that

* shape is Printable type call the print() method on it

*/

for (int i = 0; i < shArr.length; i++) {

System.out.println(shArr[i].toString());

System.out.println("Perimeter :" + df.format(shArr[i].perimeter()));

System.out.println("Area :" + df.format(shArr[i].area()));

if (shArr[i] instanceof Printable) {

((Printable) (shArr[i])).print();

}

System.out.println();

}

}

}

_________________

Output:

Shape Array :
---------- :
Rectangle (6.0X3.0)
Perimeter :18
Area :18
o o o o o o
o o
o o o o o o

Rectangle (5.0X4.0)
Perimeter :18
Area :20
o o o o o
o o
o o
o o o o o

Square (5.0)
Perimeter :20
Area :25
o o o o o
o o
o o
o o
o o o o o

Square (5.0)
Perimeter :20
Area :25
o o o o o
o o
o o
o o
o o o o o

IsoscelesRightTriangle (5.0)
Perimeter :25.7
Area :12.5
o
o o
o o
o o
o o o o o

IsoscelesRightTriangle (3.0)
Perimeter :15.4
Area :4.5
o
o o
o o o

Cirlce (7.0)
Perimeter :44
Area :153.9

Cirlce (2.0)
Perimeter :12.6
Area :12.6


_____________Could you rate me well.Plz .Thank You