// chapter 11, Java programming, Joyce Farrell, 8th ed // output // do simple an
ID: 3672217 • Letter: #
Question
// chapter 11, Java programming, Joyce Farrell, 8th ed // output // do simple and apply from chapter 11// chapter 11, Java programming, Joyce Farrell, 8th ed // output // do simple and apply from chapter 11 Modify Exercise 11, adding an interface called Sidedobject that contains a method called displaysidesO; this method displays the number of sides the object possesses Modify the GeometricFigure subclasses to include the use of the interface to display the number of sides of the figure. Create an application that demonstrates the use of both subclasses. Save the files as GeometricFigure2.java, Square2.java, Triangle2.java, SidedObject.,java, and UseGeometric2.java. 12.
Explanation / Answer
//Interface class
//SidedObject.java
public interface SidedObject
{
//method declaration will be implemented in the class in which
//it is implemented
public void displaySides();
}
------------------------- ------------------------- -------------------------
//GeometricFigure2.java
public abstract class GeometricFigure2
{
//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;
}
//abstract method area
public abstract double area();
}
------------------------- ------------------------- -------------------------
//The Square2 class that inherits the properties
//from the GeometricFigure2 and implements the SidedObject
//Square2.java
public class Square2
extends GeometricFigure2
implements SidedObject
{
//Constructor that calls the super class
public Square2(double height, double width, String figureType)
{
super(height, width, figureType);
}
//Method definition of displaySides
@Override
public void displaySides()
{
System.out.println("Number of sides of "+getFigureType()+":"+4);
}
//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 SidedObject
//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
@Override
public void displaySides()
{
System.out.println("Number of sides of "+getFigureType()+":"+3);
}
//Method definition of area
@Override
public double area() {
return (0.5)*getWidth()*getHeight();
}
}
------------------------- ------------------------- -------------------------
/**The java program UseGeometric2 that tests the methods
* of Square2 and Triangle2 classes. Print the area of
* square and triangle classes */
//UseGeometric2.java
public class UseGeometric2
{
public static void main(String[] args)
{
//Create an instance of Square2
Square2 square=new Square2(10, 5, "Square");
//print number of sides of square
square.displaySides();
//print area of square
System.out.println("Area of Square : "+square.area());
//Create an instance of Triangle2
Triangle2 triangle=new Triangle2(10, 10, "Triangle");
//print number of sides of triangle
triangle.displaySides();
//print area of triangle
System.out.println("Area of Triangle : "+triangle.area());
}
}
------------------------- ------------------------- -------------------------
Sample Output:
Number of sides of Square:4
Area of Square : 50.0
Number of sides of Triangle:3
Area of Triangle : 50.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.