COMP2243 - Programming and Problem Solving

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

Methods RightTriangle.java RightTriangleWithLoop.java Geometry.java PayrollWithMethod.java PayrollWithMethodLoop.java Palindrome.java GlobalAndLocal.java Overloading.java Overloading2.java MathMethods.java and MathProgram.java CaesarCipher.java



RightTriangleWithLoop Program


Description

This program is the enhanced version of the previous program.

It uses a method to calculate and return the hypotenuse of the right triangle.

In the main() method, it uses a loop to accept the user input and call the method until the user chooses not to.

It also validates the user input.

Associated Concepts:

Method

Global and local variables

Calling method in a loop

Algorithm:

Declare and create keyboard as a global variable so it is available to all the methods in the program.

main method:

Declare local variables to represent side 1, side 2, and side 3 of right triangle

Use a loop to input side 1 and side 2 from the keyboard until the user enters -1 as side 1

Call hypotenuse method and pass side1 and side2 as arguments

Print the result

hypotenuse method definition:

Calculate and return side 3 based on the parameters passed by the caller

Source Code



Sample Run




Video