CS 1400 Lab #11: Hypotenuse Calculator

Introduction

One of the neat things about methods is that if they are correctly written, you can use them in lots of different places, and in many different kinds of programs. An important property of a well written method is that it only does one thing, and that it does it well. The Method you wrote in lab #10 does one thing: given the lengths of two sides of a right triangle, it calculates the length of the hypotenuse. The method does not care where the lengths of the two sides came from, nor does it care about where the results that it calculates will go.

In the previous lab you created a method that calculates and returns the length of the hypotenuse of a right triangle. If your method is written correctly, you should be able to copy it out of the code for lab #10, and drop it without change into a similar program that uses a Graphical User Interface.

The Interface

Start Visual Studio and create a new Windows Forms Application. You may design your own interface, but it should be intuitive and easy to use. My program looks like this:

There are two TextBoxes where the user enters in the lengths of the sides of a right triangle. There is a button that the user presses to make the program do the calculation, and a TextBox for displaying the result.

Writing the Code

Switch to the code view for your Form. Cut the code for the method you wrote to calculate the hypotenuse of a right triangle out of the program you wrote in lab #10, and paste it as a method inside the Form class of your new program. Be sure it exists as a separate method. Don't paste the code into an existing method.

Go back to the Designer view and double click on the button to create a skeleton for the method to handle the button_Click event. In this method you should

  1. Get the values in the two TextBoxes.
  2. Convert the values to doubles.
  3. Call the method you wrote to calculate the hypotenuse of the triangle.
  4. Display the result in the third TextBox.

Questions

When you submit your lab to Canvas, add a comment to your submission that correctly answers the following two questions.

Question #1: When designing a method, how many things should the method be responsible for doing?
   (a) about three is the most that it should be responsible for.
   (b) it really does not matter, make it do as many as you want.
   (c) one.
   (d) two.

Question #2: True or false: When a method is written correctly, it is possible to use the same method, without change, in a Console program or a GUI program.
   (a) true
   (a) false

When you are satisfied that your program works correctly, submit it to Canvas.

File(s) to Submit:

Place your complete project folder in a zip file and name the zip file lab_11_your-initials_V1.0.zip. For example, I would name my file lab_11_RKD_v1.0.zip. Submit this assignment as Lab #11 on Canvas.

Grading Guidelines

Description Points possible

Assignment meets grading guidelines:
o Source code files contain a declaration that you did not copy any code, except that provided.
o Assignment has been properly submitted to Canvas
o Code meets style guidelines

2

Program executes correctly and meets all requirements.

3

The answers to the questions in this lab have been included as comments on your submission, and they are correct.

2
Total 7