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



RightTriangle Program


Description

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

It also demonstrates the concepts of global and local variables.

Associated Concepts:

Method

Global and local variables

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

Input side 1 and side 2 from the keyboard

Call hypotenuse method and pass side1 and side2 as arguments, and use side3 to hold the returned value

Print the result

hypotenuse method definition:

Declare local variable s3 to represent side 3 of the right triangle

Calculate side 3 based on the parameters passed by the caller

Return the result

Source Code



Sample Run




Video