Write compile and test a program named ShapesDemo. The program will create sever
ID: 3687538 • Letter: W
Question
Write compile and test a program named ShapesDemo. The program will create several objects that descend from an abstract class called GeometricFigure and demonstrate their methods as described below.
1. Each GeometricFigure includes: a height, a width, and an area.
2. Provide get and set accessors for each of the above fields except area; the area is computed and is read-only.
3. Include an abstract method called ComputeArea() that computes the area of the GeometricFigure.
4. Create three additional classes:
a. A Rectangle is a GeometricFigure whose area is determined by multiplying width by height.
b. A Square is a Rectangle in which the width and height are the same. Provide a constructor that accepts both height and width, forcing them to be equal if they are not1. Provide a second constructor that accepts just one dimension and uses it for both height and width. The Square class uses the Rectangle’s ComputeArea() method.
c. A Triangle is a GeometricFigure whose area is determined by multiplying the width by half the height. In the ShapesDemo class, after each object is created, pass it to a method that accepts a GeometricFigure argument in which the figure’s data is displayed. Change some dimensions of some of the figures and pass each to the display method again.
Explanation / Answer
GeometricFigure.java
public abstract class GeometricFigure {
private double height;
private double weight;
private double area;
public GeometricFigure(double height, double weight){
this.height = height;
this.weight = weight;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public abstract double ComputeArea();
}
Rectangle.java
public class Rectangle extends GeometricFigure {
public Rectangle(double height, double weight){
super(height, weight);
}
public double ComputeArea(){
return getHeight() * getWeight();
}
}
Square.java
public class Square extends Rectangle{
public Square(double height, double wieght){
super(height, height);
}
public Square(double height){
super(height, height);
}
}
Triangle.java
public class Triangle extends GeometricFigure{
public Triangle(double height, double weight){
super(height, weight);
}
public double ComputeArea(){
return (0.5) * getHeight() * getWeight();
}
}
ShapesDemo.java
public class ShapesDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle2 r = new Rectangle2(2.4, 3.3);
System.out.println("----------Rectangle Area------------");
System.out.println(r.ComputeArea());
Square s = new Square(2.4, 3.3);
System.out.println("----------Square Area------------");
System.out.println(s.ComputeArea());
Triangle t = new Triangle(2.4, 3.3);
System.out.println("----------Triangle Area------------");
System.out.println(t.ComputeArea());
}
}
Output
----------Rectangle Area------------
7.919999999999999
----------Square Area------------
5.76
----------Triangle Area------------
3.9599999999999995
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.