Suppose that our program uses many kinds of shapes, such as rectangle and triang
ID: 3548304 • Letter: S
Question
Suppose that our program uses many kinds of shapes, such as rectangle and triangle. We have designed an abstract class called Shape, containing abstract method getArea() and readParameterfromFile( ). Method getArea() computes the area of the shape. Method readParameterfromFile( ) reads parameters from specified file. We have designed abstract class Shape as follows:
Shape.java
abstract public class Shape {
// Private member variables
private String filename;
// Constructor
public Shape (String filename) {
this.filename = filename;
}
public String getFilename( )
{
return filename;
}
abstract public double getArea();
abstract public voidreadParameterfromFile();
}
Write two subclasses Triangle and Rectangle from the abstract class Shape. In the derived subclasses, override the abstract methods and provide implementation to all the abstract methods including getArea() and ReadParameterfromFile(); The subclassTriangle further defines two variables called base(double type) and height(double type). The subclass Rectangle further defines two variables called length (double type) and width (double type).
A main method is provided as follows:
public class ShapeDriver {
public static void main(String[] args) {
Shape s1 = new Rectangle("rectangle.txt");
//s1.getFilename();
System.out.println("Area is " + s1.getArea());
Shape s2 = new Triangle("triangle.txt");
//s2.getFilename();
System.out.println("Area is " + s2.getArea());
}
}
When run, the main method should print this:
rectangle
Area is 7.0
triangle
Area is 10.0
A sample file of
Explanation / Answer
h
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.