Java Reference
In-Depth Information
9. Save and compile the Triangle class.
10. Write a program that instantiates at least one Polygon object and
one Triangle object. Invoke the various methods to ensure that
everything is working.
When you instantiate a Triangle object, the output should display “Inside
Polygon constructor” before displaying “Inside Triangle constructor.”
Lab 6.2: Overriding Methods
This lab is a continuation of Lab 6.1 and demonstrates overriding methods.
1. Write a class called RightTriangle that extends your Triangle class
from Lab 6.1.
2. Add a field of type double called hypotenuse to represent the
longest side of a right triangle.
3. Add a constructor that takes in two int's to represent the base and
height. Pass these two values up to the Triangle constructor and
then use these two values in the constructor to compute the
hypotenuse field. The formula is:
hypotenuse = sqrt(base*base + height*height)
4. Use the Math.sqrt() function to compute the square root. Math.sqrt()
takes in a double and returns a double. Also, print out a message
stating “Inside RightTriangle constructor.”
5. Add the toString() method to RightTriangle. Use super to invoke
toString() in the parent and concatenate the result with the
hypotenuse.
6. Save and compile the RightTriangle class.
7.
Write a program that instantiates a RightTriangle object and invokes
the toString(), getArea(), and getNumberOfSides() methods. Run
your program, and verify that everything is working correctly.
When you instantiate a RightTriangle object, the output of the con-
structors should be in the following order: “Inside Polygon constructor,”
“Inside Triangle constructor,” and “Inside RightTriangle constructor.”
Search WWH ::




Custom Search