Repeat A09, only this time make the right triangle its own class and create instances of it.
Concepts: Constructors, instance methods, encapsulation
Textbook: 4.3 - 4.5
You'll need to write two classes/files for this assignment. Normally, you'd create
RightTriangle and then some other class--
TriangleUI, perhaps--that creates
RightTriangles. However, since you're going to turn in your RightTriangle class to Tamarin, you'll need to give it the unfriendly name of
This class models a right triangle. It should have at least two
private instance variables for the base and height; you may have more. It should then have at least the following public constructor and methods:
- public UsernameA14(double base, double height)
- public double getBase()
- public void setBase(double base)
- public double getHeight()
- public void setHeight(double height)
- public double getHypotenuse()
- public double getPerimeter()
- public double getArea()
- public double getHypotenuseSlope()
Remember that your methods must match these signatures exactly. Note how, unlike in A09, the methods no longer take parameters. This is because every triangle already knows its own base and height, so you don't need to pass this information to the method anymore.
Then write a separate class with a
main method that serves as a user interface to testing your triangle class. You can prompt the user to enter a base and height and create a triangle from it, producing the same output as for A09. You don't have to get the information from the user, however; you could just hard-code a few tests.
Make sure you test each method of your right triangle and confirm that it works correctly. This means you will need to change at least one triangle's base and height and print out its details again in order to test
You will not submit this file; it's just to help you ensure that your triangle is correct.
Your output will depend on how you wrote your TriangleTester class. Could be the same as for A09, but doesn't have to be.
What to Submit
UsernameA14.java file to Tamarin.
Grading [4 points]
- 1 - Compiles
- Your program compiles successfully (no errors)
- 0.5 - Encapsulated instance variables
- All your instance variables are private.
- 0.5 - Constructor
- Has a public constructor that takes two doubles.
- 1.0 - Accessors and mutators for base and height
- 1.0 - Accessors for hypotenuse, area, perimeter, and slope of hypotenuse
- All methods must exactly match the method signatures given above.
- Demo code?
- The first lab this week, we created this version of Bird.java and Aviary.java. It demonstrates simple constructors and an instance method named
During the second lab, we went over this version of BirdRevised.java and AviaryRevised.java. These files demonstrate encapsulation--the Bird's instance variables are private so that a Bird's state can only be affected through it public methods.
UsernameA14.java should only model a triangle and not do any direct communcation with the end user. So don't do any printing or user input in that class. (If you already submitted something that does, don't worry about it--it won't cost you points.) Error-checking of passed values is optional. For instance, you could prevent negative values for the base and height by taking their absolute value instead; but this is not required--you can allow another programmer to make triangles with negative lengths if they really want to.