Back to 111 Main Page

Mobius strip

Assignment 2

Task

Print the requested details of a right triangle to the screen.

Textbook: 2.4 - 2.5
New concepts: operators (again), expressions, Math methods.

Steps

Remember that a right trangle always has a 90° angle between its base and height. Assume here that base edge is horizontal, and that the 90° right angle is one of the two bottom angles of the triangle.

Write a program that starts with two sides of a right triangle--the base and the height. These two lengths should be stored as double variables.

Using these two sides, calculate the following details:

  • Hypotenuse (√(base2 + height2))
  • Perimeter (sum of all 3 sides)
  • Area (1/2 base * height)
  • Slope of hypotenuse (height / base)

You may create additional variables as needed, however all output/results should change appropriately when only the initial value of base and height are changed. You can use Math.sqrt() to compute the square root.

Print out the length of each side and the result of each of the above calculations. Print one result per line. Be sure to label what each value is in your output. Thus, your output should be formatted something like this:

--Details of a right triangle--
Base: 3.0
Height: 4.0
Hypotenuse: 5.0
Perimeter: 12.0
Area: 6.0
Slope of hypotenuse: 1.3333333333333333

When you're done, change the values of your variables so that base = 7.5 and height = 18.

What to Submit

Upload your UsernameA02.java file to Tamarin (after 04 Sept).

Grading [4 points]

1 - Compiles
Your program compiles successfully (no errors)
1 - Variables
You use variables, not literals, for base and height in all your calculations and printing. (0.5) You display the values of those two sides before you do the rest of the calculations. (0.5)
2 - Calculations
Program displays the correct results of the 4 additional calculations (with b=7.5, h=18), properly labelled, one per line. (0.5 each)

FAQs

How do I do the square root (√) again?
Use Math.sqrt(). So, to take the square root of 9, you could something like this:
  double answer = Math.sqrt(9);
You could also replace the literal 9 with a longer expression.
How do I do exponents/powers?
There is another Math function to do this: Math.pow(base, exponent). However, it's not really necessary for this assignment. To get base2, just use base * base


~ztomasze Index : TA Details: ICS111: Assignment 2
http://www2.hawaii.edu/~ztomasze
Last Edited: 08 Sep 2008
©2008 by Z. Tomaszewski.