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

classes -------------------------------------------- public class Circle { priva

ID: 670229 • Letter: C

Question

classes
--------------------------------------------

public class Circle {

private final int radius;
/**
*
* @param radius This is the class constructor which takes in 1 param radius
*/
Circle(int radius) {
this.radius = radius;
}
/**
*
* @return method of diameter
*/
public double diameter() {
double diamater = radius * radius;
return diamater;
}
/**
*
* @return getRadius method
*/
public double getRadius() {
return radius;
}
/**
*
* @return Circumference method
*/
public double circumference() {
double circumference1 = (2 * 3.14);
double answer = circumference1 * radius;
return answer;
}
/**
*
* @return this returns the toString method, which allows values stored as objects to be printed
*/
@Override
public String toString() {
String s;
s = String.format("Circle (%d) ", radius);
return s;
}

---------------------------------------------------------------------------------------

public class IsoscelesRightTriangle {

private final double leg;
/**
*
* @param leg Constructor class which takes in an argument of leg
*/
IsoscelesRightTriangle(double leg) {
this.leg = leg;
}
/**
*
* @return returns the hypotenuse method
*/
public double hypotenuse() {
double answer = (leg * 2) + (leg * 2);
return answer;
}
/**
*
* @return the getLeg method
*/
public double getLeg() {
return leg;
}

@Override
/**
* returns the toString method
*/
public String toString() {
String s;
s = String.format("IsoscelesRightTriangle (%f)", leg);
return s;
}

----------------------------------------------------------------------------

public class Rectangle {

private final int length;
private final int width;
/**
*
* @param length Constructor which takes in 2 arguments length and width
* @param width
*/
Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
/**
*
* @return getLength method
*/
public int getLength() {
return length;
}
/**
*
* @return getWidth method
*/
public int getWidth() {
return width;
}
/**
*
* @return toString method which has an @Override
*/
@Override

public String toString() {
String s;
s = String.format("Rectangle(%dx%d)", length, width);
return s;

}

------------------------------------------------------------------------------

public class Square extends Rectangle {

Square(int side1) {
super(side1, side1);
}

public int getSide() {
return super.getLength();
}

@Override

public String toString() {
String myString;
myString = String.format("Square (%d)", getSide());
return myString;
}

--------------------------------------------------------------------------

public class InheritanceApp {

/**
* @param args the command line arguments found in the main method
*/
public static void main(String[] args) {
Rectangle myRectangle = new Rectangle(5, 4);
Square mySquare = new Square(4);
IsoscelesRightTriangle myIsoscelesRightTriangle = new IsoscelesRightTriangle(5);
Circle myCircle = new Circle(4);

System.out.println(myRectangle.toString());
System.out.printf("Length: %d%n", myRectangle.getLength());
System.out.printf("Width: %d%n", myRectangle.getWidth());
System.out.println();

System.out.println(mySquare.toString());
System.out.printf("Side: %d%n", mySquare.getSide());
System.out.println();

System.out.println(myIsoscelesRightTriangle.toString());
System.out.printf("Leg: %f%n", myIsoscelesRightTriangle.getLeg());
System.out.printf("Hypotenuse: %f%n", myIsoscelesRightTriangle.hypotenuse());
System.out.println();

System.out.println(myCircle.toString());
System.out.printf("Diameter: %f%n", myCircle.diameter());
System.out.printf("Circumference: %f%n", myCircle.circumference());
System.out.printf("Radius: %f%n", myCircle.getRadius());
System.out.println();

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

Rectangle rectangle2 = new Square(4);
System.out.println(rectangle2);
System.out.printf("Length: %d%n", rectangle2.getLength());
System.out.printf("Width: %d%n", rectangle2.getWidth());
System.out.println();
System.out.println("Rectangle Array");
System.out.println("---------------------------");

Rectangle[] rectangles = new Rectangle[3];
rectangles[0] = rectangle2;
rectangles[1] = mySquare;
rectangles[2] = myRectangle;

for (Rectangle element : rectangles) {
System.out.println(element);
System.out.printf("Length: %s%n", element.getLength());
System.out.printf("Width: %s%n", element.getWidth());
System.out.println();
}

}

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: ad interface Shape: aInterface» Sha perimeter.. returns the perimeter of the shape perimeter): double area(): dcuble area.. returns the area of the shape ad interface Printable: print.. prints the outline of the shape with stars) «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 implement Shape . all except Circle implement Printable

Explanation / Answer

package mani;
public class InheritanceApp {
/**
* @param args the command line arguments found in the main method
*/
public static void main(String[] args) {
Rectangle myRectangle = new Rectangle(6, 3);
Square mySquare = new Square(5);
IsoscelesRightTriangle myIsoscelesRightTriangle = new IsoscelesRightTriangle(6);
Circle myCircle = new Circle(5);
System.out.println(myRectangle.toString());
System.out.printf("Length: %d%n", myRectangle.getLength());
System.out.printf("Width: %d%n", myRectangle.getWidth());
System.out.println();
System.out.println(mySquare.toString());
System.out.printf("Side: %d%n", mySquare.getSide());
System.out.println();
System.out.println(myIsoscelesRightTriangle.toString());
System.out.printf("Leg: %f%n", myIsoscelesRightTriangle.getLeg());
System.out.printf("Hypotenuse: %f%n", myIsoscelesRightTriangle.hypotenuse());
myIsoscelesRightTriangle.print();
System.out.println(myCircle.toString());
System.out.printf("Diameter: %f%n", myCircle.diameter());
System.out.printf("Circumference: %f%n", myCircle.circumference());
System.out.printf("Radius: %f%n", myCircle.getRadius());
System.out.println();
System.out.println("Shape Array:");
System.out.println("-------------");
Rectangle rectangle2 = new Square(5);
System.out.println(rectangle2);
System.out.println("Perimeter: "+ rectangle2.perimeter());
System.out.println("Area: "+rectangle2.area());
rectangle2.print();
System.out.println("Rectangle Array");
System.out.println("---------------------------");
Rectangle[] rectangles = new Rectangle[3];
rectangles[0] = rectangle2;
rectangles[1] = mySquare;
rectangles[2] = myRectangle;
for (Rectangle element : rectangles) {
System.out.println(element);
System.out.printf("Length: %s%n", element.getLength());
System.out.printf("Width: %s%n", element.getWidth());
System.out.println();
}
}
}

package mani;
public class Circle implements Shape {
private final int radius;
/**
*
* @param radius This is the class constructor which takes in 1 param radius
*/
Circle(int radius) {
this.radius = radius;
}
/**
*
* @return method of diameter
*/
public double diameter() {
double diamater = 2 * radius;
return diamater;
}
/**
*
* @return getRadius method
*/
public double getRadius() {
return radius;
}
/**
*
* @return Circumference method
*/
public double circumference() {
double circumference1 = (2 * 3.14);
double answer = circumference1 * radius;
return answer;
}
/**
*
* @return this returns the toString method, which allows values stored as objects to be printed
*/
@Override
public String toString() {
String s;
s = String.format("Circle (%d) ", radius);
return s;
}
public double area(){
   return 3.14*(radius*radius);
}
public double perimeter(){
   return this.circumference();
}

}

package mani;
public class IsoscelesRightTriangle implements Shape,Printable {
private final double leg;
/**
*
* @param leg Constructor class which takes in an argument of leg
*/
IsoscelesRightTriangle(double leg) {
this.leg = leg;
}
/**
*
* @return returns the hypotenuse method
*/
public double hypotenuse() {
double answer = (leg * 2) + (leg * 2);
return answer;
}
/**
*
* @return the getLeg method
*/
public double getLeg() {
return leg;
}
@Override
/**
* returns the toString method
*/
public String toString() {
String s;
s = String.format("IsoscelesRightTriangle (%f)", leg);
return s;
}
public double area(){
   return (leg*leg/2);
}
public double perimeter(){
   return 2*leg+hypotenuse();
}
public void print(){
   for (int i = 0; i < (new Double(leg).intValue()); i++)
{
for (int j = (new Double(leg).intValue()); j > i; j--)
{
System.out.print(" ");
}
for (int k = 1; k <= i + 1; k++) {
System.out.print("*");
}
System.out.print(" ");
}
}
}

package mani;
public class Rectangle implements Shape,Printable{
private final int length;
private final int width;
/**
*
* @param length Constructor which takes in 2 arguments length and width
* @param width
*/
Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
/**
*
* @return getLength method
*/
public int getLength() {
return length;
}
/**
*
* @return getWidth method
*/
public int getWidth() {
return width;
}
/**
*
* @return toString method which has an @Override
*/
@Override
public String toString() {
String s;
s = String.format("Rectangle(%dx%d)", length, width);
return s;
}
public double area(){
       return length*width;
  
}
   @Override
   public void print() {
       for(int i=0;i<width;i++){
           System.out.print("* ");
          
       }
       System.out.print(" ");
       for(int i=0;i<length-2;i++){
           System.out.print("* ");
           for(int j=0;j<width+1;j++){
               System.out.print(" ");
           }
           System.out.print("* ");
       }
       for(int i=0;i<width;i++){
           System.out.print("* ");
          
       }
       System.out.println();
   }
   @Override
   public double perimeter() {
      
       return 2*(length+width);
   }
}

public interface Printable
{
   public void print();
}

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

package mani;

public class Square extends Rectangle implements Shape,Printable {
Square(int side1) {
super(side1, side1);
}
public int getSide() {
return super.getLength();
}
@Override
public String toString() {
String myString;
myString = String.format("Square (%d)", getSide());
return myString;
}
public double area(){
   return super.area();
}
public double perimeter(){
   return super.perimeter();
}
public void print(){
   super.print();
}
}