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

//Chapter 11, Java Programming, Joyce Farrell, 8th ed //cant run with UseGeometr

ID: 3674494 • Letter: #

Question

//Chapter 11, Java Programming, Joyce Farrell, 8th ed

//cant run with UseGeometric2 //programming Exercise 12 // need to fix

//use interface (pubilc interface SideObject)

public class UseGeometric2

{

   public static void main(String[] args)

   {

GeometricFigure2[] array = new GeometricFigure2[4];

   array[0] = new Square2(5, 5, "Square");

   array[1] = new Triangle2(5, 5, "Triangle");

   array[2] = new Square2(10, 10, "Square");

   array[3] = new Triangle2(10, 10, "Triangle");

for(GeometricFigure2 Geofigure: array)

{

       array.displaySides();

      System.out.println(Geofigure.getFigureType() + " with height = " + Geofigure.getHeight() +

   " and with width = " + Geofigure.getWidth() + " has an area of " + Geofigure.area());

}

   }

}

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

public abstract class GeometricFigure2

{

   protected double height;

   protected double width;

   protected String figureType;

   abstract public double area();

   public GeometricFigure2(double ht, double wt, String figure)

   {

   height = ht;

   width = wt;

   figureType = figure;

   }

   public String getFigureType()

   {

return figureType;

   }

   public double getHeight()

   {

return height;

   }

   public double getWidth()

   {

return width;

   }

}

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

public interface SidedObject

{

   public void displaySides();

}

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

public class Square2 extends GeometricFigure2 implements SidedObject

{

   public Square2(double ht, double wt, String figure)

   {

super(ht, wt, figure);

   }

   public void displaySides()

   {

   System.out.println("This figure has " + getFigureType() + 4);

   }

   @Override

   public double area()

   {

   return height * width;

   }

}

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

public class Triangle2 extends GeometricFigure2 implements SidedObject

{

   public Triangle2(double ht, double wt, String figure)

   {

super(ht, wt, figure);

   }

   public void displaySides()

   {

   System.out.println("This figure has" + getFigureType() + 3);

   }

   @Override

   public double area()

   {

return 0.5 * height * width;

   }

}

Explanation / Answer

//Interface class
//SidedObject.java

public interface SidedObject
{  
   //Method declarations to be implemeneted
   public double area();
   //method declaration will be implemented in the class in which
   //it is implemented
   public void displaySides();
}

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

//The Square2 class that inherits the properties
//from the GeometricFigure2 and implements the SideObject
//Square2.java
public class Square2
extends GeometricFigure2
implements SideObject
{
   //Constructor that calls the super class
   public Square2(double height, double width, String figureType)
   {
       super(height, width, figureType);      
   }
   //Method definition of displaySides
   public void displaySides()
   {
       System.out.println("This figure "+getFigureType()
               +" has" + 4+" sides");
   }
   //Method definition of area
   @Override
   public double area()
   {      
       return getHeight()*getWidth();
   }  
}//end of Square2 class

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

//The Triangle2 class that inherits the properties
//from the GeometricFigure2 and implements the SideObject
//Triangle2.java
public class Triangle2 extends GeometricFigure2 implements SidedObject
{

   //Constructor that calls the super class
   public Triangle2(double height, double width, String figureType) {
       super(height, width, figureType);

   }
   //Method definition of displaySides
   public void displaySides()
   {
       System.out.println("This figure "+getFigureType()
               +" has" + 3+" sides");
   }
   //Method definition of area
   @Override
   public double area() {      
       return (0.5)*getWidth()*getHeight();
   }

}

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

//GeometricFigure2.java
public abstract class GeometricFigure2
                   implements SidedObject
{
  
   //private members of class
   private double height;
   private double width;
   private String figureType;
  
   //Constructor that takes height , width and figure type
   public GeometricFigure2(double height,double width, String figureType)
   {
       //set values
       this.height=height;
       this.width=width;
       this.figureType=figureType;
   }
  
   //Returns height
   public double getHeight()
   {
       return height;
   }
  
   //Returns width
   public double getWidth()
   {
       return width;
   }
  
   //Returns the figure type
   public String getFigureType()
   {
       return figureType;
   }
      
}

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

//Tester java program that creates an array of type
//GeometricFigure2 and instantiate the Square2 and Triangle2
//and call the displaySides method that will print
//the number of sides of each object
//UseGeometric2.java
public class UseGeometric2
{
   public static void main(String[] args)
   {
       GeometricFigure2[] array = new GeometricFigure2[4];

       array[0] = new Square2(5, 5, "Square");
       array[1] = new Triangle2(5, 5, "Triangle");
       array[2] = new Square2(10, 10, "Square");
       array[3] = new Triangle2(10, 10, "Triangle");
      
       for(GeometricFigure2 Geofigure: array)
       {
           Geofigure.displaySides();

           System.out.println(Geofigure.getFigureType()
                   + " with height = " + Geofigure.getHeight() +
                   " and with width = " + Geofigure.getWidth() +
                   " has an area of " + Geofigure.area());

       }

   }

}

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

Sample Output:

This figure Square has4 sides
Square with height = 5.0 and with width = 5.0 has an area of 25.0
This figure Triangle has3 sides
Triangle with height = 5.0 and with width = 5.0 has an area of 12.5
This figure Square has4 sides
Square with height = 10.0 and with width = 10.0 has an area of 100.0
This figure Triangle has3 sides
Triangle with height = 10.0 and with width = 10.0 has an area of 50.0