Working with
Download the lab4 folder to your Desktop.
Here you will create a new class Triangle
that provides functionality for a triangle. Recall from Geometry that a triangle is defined by its three sides, let's call them a, b, and c. Its perimeter is simply the sum of its three sides. Its area can be computed using Heron's formula:
s = (a+b+c)/2
area = sqrt(s(s-a)(s-b)(s-c))
Determine where the Triangle class should be placed in the Shapes hierarchy.
Start by writing the header of the Triangle
class that extends the appropriate parent class. Add the main method so you are ready to test as you add methods.
Determine what instance variables should Triangle contain. Then, create the constructor that gives values to these instance variables. Do not forget to call any super constructor, if applicable. At this point, create a few triangle objects to test your constructor.
There may be some methods that you have to overwrite, otherwise the compiler will not let you off the hook. Determine which ones they are, and implement them. Of course, test your implementation.
If you have time, at the end, make the printout better, like as shown below. Also, write javadoc-compliant documentation and enjoy the professional look that your documentation has!
Some informative output:
dot2: circle of area 105.683 (radius: 5.8)
dot3: circle of area 0 (radius: 0.0)
dot2 area (expected: 105.68) Got: 105.68317686676063
dot3 perimeter (expected: 0.0) Got: 0.0
Define one more class, in the Shape hierarchy
, to represent an Isosceles
triangle, that is a triangle that has any two of its sides equal. (When dealing with floating-point numbers, equality is approximate, not exact. If a difference is less than minDiff
we consider the difference to be zero.)
Think carefully about how to do so. Where should this class inherit from? What instance variables
should you include? What inherited instance variables are visible from within this class? Discuss and answer these questions with your partner before writing any code.
As always, test your new class.
The ShapesCollection class will use an array to maintain a collection of Shape objects (Rectangles, Circles, Triangles). In the downloaded material, you are provided with a starting template for the ShapesCollection class. Look at the code already included in this class. Then set up the necessary instance variables, and constructor.
add an object
of type Shape to the collection.main() method
to start your testing right away.
Below is some starting sample testing output. You will need to add more testing!
This collection contains 2 shapes:
rectangle of area 12 (length: 2.0 width: 6.0)
triangle of area 6 (sides: 3.0, 4.0, 5.0)
public int findLargest()
Returns the index of the Shape with the largest area in the collection of shapes.
Test comparing a couple of Shapes that you have added in the collection.
Note that you will need to use a method to compare two shapes by comparing their areas. Do you need to write a compareTo()
method, or is the method already available to you because it is defined in the appropriate class in the hierarchy?