How do you acomplish this assignment? I cant get the output right. Assignment In
ID: 3871346 • Letter: H
Question
How do you acomplish this assignment? I cant get the output right.
Assignment Interface CSIS-1410 Learning Objectives: Create an interface Implement an interface Demonstrate polymorphic behavior of interfaces Use the instanceof operator Develop algorithms (print methods ) 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» Sha 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 e all of them are a Shape all except Circle are a PrintableExplanation / Answer
Printable.java
public interface Printable {
public abstract void print();
}
Shape.java
public interface Shape {
public abstract double perimeter();
public abstract double area();
}
Rectangle.java
public class Rectangle implements Shape,Printable{
private int length;
private int breadth;
public void setLength(int length) {
this.length = length;
}
public int getLength() {
return length;
}
public void setBreadth(int breadth) {
this.breadth = breadth;
}
public int getBreadth() {
return breadth;
}
public Rectangle(int length,int breadth) {
this.length=length;
this.breadth=breadth;
}
@Override
public void print() {
for (int i = 0; i < breadth; i++) {
System.out.print("o ");
}
System.out.println();
for (int i = 0; i < length - 2; i++) {
System.out.print("o ");
for (int j = 0; j < breadth - 2; j++) {
System.out.print(" ");
}
System.out.println("o ");
}
for (int i = 0; i < breadth; i++) {
System.out.print("o ");
}
System.out.println();
}
@Override
public double perimeter() {
return 2*(length+breadth);
}
@Override
public double area() {
return length*breadth;
}
}
Square.java
public class Square implements Shape,Printable{
public Square(int size) {
super();
this.size = size;
}
private int size;
public void setSize(int size) {
this.size = size;
}
public int getSize() {
return size;
}
@Override
public void print() {
for (int i = 0; i < size; i++) {
System.out.print("o ");
}
System.out.println();
for (int i = 0; i < size - 2; i++) {
System.out.print("o ");
for (int j = 0; j < size - 2; j++) {
System.out.print(" ");
}
System.out.println("o ");
}
for (int i = 0; i < size; i++) {
System.out.print("o ");
}
System.out.println();
}
@Override
public double perimeter() {
return 4*size;
}
@Override
public double area() {
return Math.pow(size,2);
}
}
IsoscelesRightTriangle.java
public class IsoscelesRightTriangle implements Shape,Printable{
public IsoscelesRightTriangle(int size) {
super();
this.size = size;
}
private int size;
public void setSize(int size) {
this.size = size;
}
public int getSize() {
return size;
}
@Override
public void print() {
for(int i=0;i<size;i++){
for(int j=0;j<=i;j++){
System.out.print("o ");
}
System.out.println();
}
}
@Override
public double perimeter() {
return size*(2+Math.sqrt(2));
}
@Override
public double area() {
return (Math.pow(size, 2))/2;
}
}
Circle.java
public class Circle implements Shape {
public Circle(int radius) {
super();
this.radius = radius;
}
private int radius;
public void setRadius(int radius) {
this.radius = radius;
}
public int getRadius() {
return radius;
}
@Override
public double perimeter() {
return 2*Math.PI*radius;
}
@Override
public double area() {
return Math.PI*radius*radius;
}
}
InterfaceApp.java
import java.math.BigDecimal;
import java.math.RoundingMode;
public class InterfaceApp {
public static void main(String[] args) {
Shape[] shapeArr=new Shape[8];
shapeArr[0]=new Rectangle(6,3);
shapeArr[1]=new Rectangle(5,4);
shapeArr[2]=new Square(4);
shapeArr[3]=new Square(3);
shapeArr[4]=new IsoscelesRightTriangle(5);
shapeArr[5]=new IsoscelesRightTriangle(3);
shapeArr[6]=new Circle(7);
shapeArr[7]=new Circle(2);
print(shapeArr);
}
private static void print(Shape[] shapeArr) {
System.out.println("Shape Array:");
System.out.println("-----------");
for (int i = 0; i < shapeArr.length; i++) {
Shape r=shapeArr[i];
if(r instanceof Rectangle){
System.out.println("Rectangle ("+ ((Rectangle)r).getLength()+"x"+((Rectangle)r).getBreadth()+")");
}else if(r instanceof Square){
System.out.println("Square ("+ ((Square)r).getSize()+")");
}else if(r instanceof IsoscelesRightTriangle){
System.out.println("IsoscelesRightTriangle ("+ ((IsoscelesRightTriangle)r).getSize()+")");
}else if(r instanceof Circle){
System.out.println("Circle ("+ ((Circle)r).getRadius()+")");
}
System.out.println("Perimeter: "+new BigDecimal(r.perimeter()).setScale(1, RoundingMode.HALF_UP).doubleValue());
System.out.println("Area: "+new BigDecimal(r.area()).setScale(1, RoundingMode.HALF_UP).doubleValue());
if(r instanceof Printable){
((Printable) r).print();
}
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.