Starting from:
$35

$29

Project 1 Mathematical Functions and Classes

    • Random Numbers

In this part you will program an instantiable Random class for generating random numbers. You are required to use a sequential random number generator (RNG). This is a function of form

rnew = ((P1   rold) + P2) mod M
(1)

In this function, P1, P2, and M are large constant prime numbers (for example, 7919, 65537, and 102611). Random numbers generated by this function will be strictly less than M, and greater than or equal to zero. Choosing P1, P2, and M can be more of an art than a science - some results seem random, and others are predictable. Therefore, our class will allow the programmer to specify these values in the constructor for each Random they create.

We will also need to specify an initial value S, since there will be no rold the rst time we invoke the function. This value is called the seed.


Note: Some students will notice that this is not inherently \random". What we are really doing is generating numbers with such a complicated function that it’s impossible to predict the next one. However, if we set the seed to the same value, the exact same numbers will appear! This property will be useful for debugging.


Create a Random class with the following methods. You are allowed to create any member variables you want so long as they are all private. You may not use another library, such as Java’s Math module, to generate random numbers.

public Random(int p1, int p2, int m): Set up a random number generator with the given constants. The constants must be in this order. Make sure not to confuse the role of P1 and P2 - check the description above if you are unsure. This should initialize the seed to 0. In general, P1, P2, and M should never be changed after this constructor gets called.

public void setSeed(int seed): Set the seed of the random number generator.

public int getMaximum(): Return the value of M for this random number generator.

public int random(): Use the sequential random number algorithm to generate the rnew value and return it. If this method is called again without resetting the seed, it should generate a di erent value.

This should be the only function that applies the generation algorithm. All subsequent methods should be accomplished using only a call to this method.

Continued on the next page...


CSCI 1933 PROJECT 1    1. RANDOM NUMBERS



public int randomInteger(int lower, int upper): Return a random integer in the range lower to upper. Possible values for randomInteger(1, 5) should be 1, 2, 3, 4 and 5; each of them should be equally probable. If lower > upper, swap them and don’t cause an error.



Note: We have the inequality 0 random() < M for all values. What happens when we add a constant to each element in the inequality? What happens if we multiply each element by a constant? Will it still hold true?

public boolean randomBoolean(): Randomly return true or false. This should simulate a 50% chance.

public double randomDouble(double lower, double upper):  Get a random double in the range lower to upper. Possible values of randomDouble(1.0, 5.0) could be 1.0, 5.0, 1.265646948, 4.9999999998, etc... If lower > upper, swap them and don’t cause an error.



Note: In Java, if you try to divide a positive int by a larger positive int, the result is always 0. For example, 75 / 100 is 0. How can you avoid this problem?



Hint: You won’t be able to use the modulo operator for randomDouble, because random returns an int and you need a double. What number can you use for division to obtain a double? How can you scale the number to be within the bounds range?

A main method: The main method should demonstrate su cient testing to prove that each of the methods work. This should not just generate a bunch of random output; it should prove to the TA that you are certain the program is correct. This means multiple tests per method, and not just for cases that should succeed. Some things to try:

{ Ensure that an even distribution occurs { that is, every choice is equally probable. { Ensure that methods are robust { they do not throw exceptions on invalid input.

{ Ensure your generator works as a whole. Some functions should not break other functions depending on the order you call them in.

The output of the main method should make the fact that the program is working quite obvious. This means printing useful information and human-readable text, not just a bunch of numbers or \true" statements.




The rest of this page is intentionally left blank










4
CSCI 1933 PROJECT 1    2. QUADRATIC FUNCTIONS



    • Quadratic Functions

In this part you will create a Quadratic class for dealing with quadratic functions. A quadratic function is a function of the form ax2 +bx+c. Create a Quadratic class with the following methods:

public Quadratic(float a, float b, float c) Set up a quadratic function with the given coe cients.

public Quadratic add(Quadratic other): This method should add other to the current quadratic function and return the result as a new Quadratic.

public Quadratic subtract(Quadratic other): This method should subtract other from the current quadratic function and return the result as a new Quadratic.

public Roots findRoots(): This method should use the quadratic formula to nd the roots of the current quadratic function, returning an instance of the Roots class containing those roots. You must write the Roots class on your own - more details are given below.

public String toString(): This method should return a String representation of the cur-

rent quadratic function, e.g. 9x2    4x + 1

public boolean equals(Object other): This method should return true if the current quadratic function is equal to other and false otherwise.


Note: Since the coe cients are oats, comparing them directly probably won’t work due to oating point imprecision. Instead of directly testing if two numbers are equal, check if their di erence is within some tolerance, e.g. 0.0001.

A main method: Like the previous class, demonstrate that all the methods work via well-documented, well-formatted tests. You may want to include getters and setters for the coef-cients in your implementation of Quadratic.

Roots: An additional class for you to write to contain the roots of a quadratic formula. The structure and methods are up to you, though you should use the Complex4 class from lecture to store the roots.


The rest of this page is intentionally left blank
















5
CSCI 1933 PROJECT 1    3. GRADING



    • Grading

Your project will be graded according to the following breakdown:

Style - 20 points Testing - 10 points

Random - 35 points - Note that the use of library functions such as Math.random() will result in a 0 for this section

Quadratic - 35 points

As described in the syllabus, submitting the project one day late will result in a 10 point penalty, while submitting the project two days late will result in a 20 point penalty. No credit will be given for projects submitted more than two days late. You are allowed to submit as many times as you would like, and we will grade your last submission.


The rest of this page is intentionally left blank









































6

More products