Homework 6
1. Static
SimpleMath
Class (5 points)Create a
SimpleMath
class that has two static methods to add or subtract two numbers, as well as one static class constant, which will store the value of the transcendental number p.define the following class constant:
double PI
- set to as many digits as you want.define/implement the following methods:
double add(double, double)
- method will take in two doubles and return their sum as a double.
double subtract(double, double)
- method will take in two doubles and return their difference as a double.Be sure to create a test class/program to test the complete functionality of the
SimpleMath
class. If you would like to do more, you can also add multiply and divide methods (for bothint
anddouble
on the divide), but it is not required.
2.
Die
Class. (10 points)Create a
Die
class. This class will describe a single die (singular of dice) that has any number of sides (4, 6, 8, 10, 12, 20, 100, or more ... even dice that are physically impossible). You will need to:store two pieces of information:
int numberOfSides
- number of sides on the die
int currentNumberUp
- current number that is facing up on the diedefine/implement the following constructors:
Die()
- the default constructor that will initialize the number of sides to 6 (a standard die) and will randomly set the current value of the die
Die(int)
- a second constructor that will take a parameter to set the number of sides on the die and will also randomly set the current value of the die
Die(int, int)
- a third constructor that will take two parameters: the first will set the number of faces on the die, and the second will set the current value of the die (which side is facing up)define/implement the following methods:
int rollDie()
- method will roll the die and return the value of the upside faceYou will need to use
Math.random()
to return a random number in order to simulate the randomness of the die roll. TherollDie
method should return the value as anint
. Note, theMath.random()
call returns adouble
greater than or equal to 0.0 and less than 1.0. So to generate a random number between 1 and 6, we will need to multiply this number by 6 (which gives us a number between 0.0 and 5.9999...) and then add 1 to it (which shifts the range by one i.e. 1.0 to 6.9999...). After this calculation, we will need to cast the random number from adouble
to anint
, which will truncate the number to give us an integer between 1 and 6. In general the pattern is:   
(int)((Math.random() * upperLimit) + lowerLimit)
where
lowerLimit
is less than or equal toupperLimit
and bothlowerLimit
andupperLimit
are positive (greater than or equal to zero).
int readDie()
- method reads the current value of the die's upside face (without re-rolling) and returns the value as anint
.Be sure to create a test program to test the complete functionality of the
Die
class. You will also want to save this class, as we might use it in future assignments.