Java Fundamentals  «Prev  Next»

Using Instance Variables Methods - Exercise

Objective: Use instance variables and methods.

Exercise Grading

The full credit for this exercise is 5 points. To receive full credit, you will need to successfully create the source code for the class and test program. You will submit your source code by pressing the submit button.

Exercise Instructions

  1. Create a class named RightTriangle which models a right triangle. Use two instance variables named a and b to represent the lengths of the shorter sides of the right triangle, and use an instance method hypotenuse() to calculate the length of the hypotenuse of the right triangle. The length of the hypotenuse is equal to the square root of the sum of the squares of the lengths of the shorter sides.
  2. Write a test application called TriangleTest that creates an instance of the RightTriangle class, sets the values of the instance variables a and b to 3.0 and 4.0 respectively, and then invokes the hypotenuse() method to calculate the length of the hypotenuse. Make sure that you output the results of hypotenuse() so that you can tell if the class works properly. You should see a result of 5.0.

What to submit

In the text box below, cut and paste the source code for RightTriangle and TriangleTest. Click the Submit Button after pasting your source code .