a. Create an abstract class called GeometricFigure. Each figure includes a height, a width, a figure type, and an area. Include an abstract method to determine the area of the figure. Create two subclasses called Square and Triangle. Create an application that demonstrates creating objects of both subclasses, and store them in an array. Save the files as GeometricFigure.java, Square.java, Triangle.java, and UseGeometric.java.
b. Modify Exercise a, adding an interface called SidedObject that contains a method called displaySides(); 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.
GeometricFigure2.java
package geometricfigure;
public abstract class GeometricFigure2 {
private int height, width;
private String figure_type;
private double area;
GeometricFigure2(){
}
GeometricFigure2(int h, int w, String f){
height = h;
width = w;
figure_type = f;
}
int getHeight(){
return height;
}
int getWidth(){
return width;
}
abstract void area();
}
Square2.java
package geometricfigure;
public class Square2 extends GeometricFigure2 implements SidedObject {
Square2(int h, int w){
super(h, w, "Square");
}
void area(){
System.out.println("The area of the square is: "+ getHeight() * getWidth());
}
public void displaySides(){
System.out.println("Triangle has 4 sides ");
}
}
Triangle2.java
package geometricfigure;
public class Triangle2 extends GeometricFigure2 implements SidedObject {
Triangle2(int h, int w){
super(h, w, "Triangle");
}
void area(){
System.out.println("The area of the Triangle is: "+ 0.5*getHeight() * getWidth());
}
public void displaySides(){
System.out.println("Triangle has 3 sides ");
}
}
SidedObject.java
package geometricfigure;
public interface SidedObject {
void displaySides();
}
UseGeometric2.java
package geometricfigure;
public class UseGeometric {
public static void main(String[] args) {
Square2 s = new Square2(9,8);
Triangle2 t = new Triangle2(8,6);
s.area();
s.displaySides();
t.area();
t.displaySides();
Square2 sq[] = new Square2[2];
Triangle2 tr[] = new Triangle2[2];
}
}
Comments
Leave a comment