CSC1110 Exercise 4
Write the helper methods for a program that will randomly generate the length of two sides of a right triangle as whole numbers between 1 and 10 inclusive, then calculate the hypotenuse and the two non-right angles and print out the results.
The Math class
The java.lang.Math class is a collection of constant and helper methods for doing common mathamatical tasks. When using a method from the Math
class, you will need to tell Java to look in the Math
class to find the code to run. This is different from, say, a String
, where you would need to create an instance of the String
, then call the method on that specific String
, since eachString
may have a different value.
String s = "penguin"; String t = "hello"; int x = s.length(); int y = t.length();
In the above example, s
and t
will have different values because they are different instances of a String. There are no instances of Math
. It simple is Math. As such, we do not create an instance. We instead reference the class itself, not some variable that contains an instance of the class.
double root = Math.sqrt(4);
Math Methods
There are many methods in the Math class. The ones we will work with in this exercise are:
- Math.sqrt() - square root
- Math.pow() - power (squared, cubed, etc)
- Math.random - generates a pseudorandom value between 0 inclusive and 1.0 exclusive
- Math.sin(), Math.cos(), Math.asin(), Math.acos(), Math.toDegrees(), Math.toRadians()
Math.random()
The random()
method uses the current time to generate a pseudorandom double value between 0.0 inclusive and 1.0 exclusive or, 0.0 <= n < 1.0. Using a little arithmetic and typecasting, we can use this method to generate many ranges of values. For our purposes, we want to generate an integer between 1 and 10. To do this, we will do the following:
- Call the random() method to give us a number between 0 and almost 1.0
double random = Math.random();
- Multiply that number by the highest value we want, in this case 10
random *= 10;
- We now have a double between 0 and almost 10. If we add 1 to this value, we will have a value between 1 and almost 11.
random += 1;
- If we now cast this as an
int
, that will remove any fractional value, and leave us with an integer between 1 and 10.
int r = (int) random;
This can all be done in a single line of code:
int r = (int) (Math.random() * 10 + 1);
Trigonometry in Java
By default, any trig function in Java results in radians. If we want degrees rather than radians, we will need to convert the value after it has been calculated. Thus the pattern we will use is
- Do the calculation that we need
- Convert the answer to the format we want
In this case, do the calculation and get the answer in radians and then convert the answer to degrees.
Calculating the values
While we do fully expect you do be able to do some basic geometry and trigonometry in college, it's the first month so we'll cut you some slack. Here are the formulae for the calculations we are doing:
- Length of a hypotenuse: h2 = a2 + b2
- Angle of one side and the hypotenuse: β = arccos(b / h)
- The last angle would simply be 90 - β, as all triangles have 180o
Formatting the results - System.out.printf()
We are familiar with the print()
and println()
functions by now. They simply print to the console whatever String
you pass in, and println()
will add a newline to the end. printf()
does the same thing, but allows you to specify how values are displayed. For this exercise, we will only focus on truncating a floating point value to one or two decimal places.
To use printf()
we will start by creating a String
that contains all the text we want, including the variables that contain the values we want to print. Say I wanted to print "I have x
apples", where x
is a double variable. We would start with:
System.out.printf("I have x apples");
At this point, it would literally print out "I have x apples"
, which not what we want. We want whatever value is stored in x
. Say the value 2.587
is currently stored in x
. To print that out, we would first replace x
with a generic placeholder for a floating point number: %f
System.out.printf("I have %f apples");
So now printf
knows it's printing a floating point value, but it doesn't know where to look. We need to tell it which value to use in place of %f
. To do this, we add our variable x
as a parameter after the String
in the method call:
System.out.printf("I have %f apples", x);
This will print out "I have 2.587 apples"
. But say we want the display to be rounded to the nearest tenth. We can do that by telling printf
to only show one place after the decimal point by adding what we call a flag to our placeholder. A flag indicates additional information to be used on that placeholder. Here we would want just one place after the decimal point, so:
System.out.printf("I have %.1f apples", x);
There is a long list of flags and what they do, but this is all we will use in this exercise.
The Code
Here is the starting code. You will only need to write the bodies of the helper methods, as indicated with a //TODO comment
/*
* Course: CSC-1110 - FIXME
* Fall 2023
* Exercise 4 - Right Triangle Math
* Name: FIXME
* Last Updated: FIXME
*/
package username;
/**
* This class generates two random whole numbers between 1 and 10 and
* uses those values as the two legs of a right triangle. It then
* calculates the hypotenuse and the remaining two angles and
* displays them.
*/
public class Exercise4 {
private static final double RIGHT_ANGLE = 90.0;
public static void main(String[] args) {
// randomly choose side a
double a = generateSide();
// randomly choose side b
double b = generateSide();
// calculate hypotenuse
double h = calculateHypotenuse(a, b);
// calculate angle ah
double angleAH = calculateAngle(a, h);
// calculate angle bh
double angleBH = RIGHT_ANGLE - angleAH;
// report using printf
report(a, b, h, angleAH, angleBH);
}
private static int generateSide() {
// TODO
}
private static double calculateHypotenuse(double a, double b) {
// TODO
}
private static double calculateAngle(double x, double h) {
// TODO
}
private static void report(double a, double b, double h, double angleAH, double angleBH) {
// TODO
}
}
A sample of what the output should look like if both a and b were randomly set to 5 would be:
a=5.0, b=5.0, h=7.07 ah=45.00, bh=45.00, ab=90.0
Submission Instructions:
Submit Exercise4.java to Canvas.