$30
Project 1
Mathematical Functions and Classes
Instructions
Welcome to the first CSCI 1933 project! We recommend that you do not use an IDE when writing
this project – the terminal and a simple text editor are sufficient. Students who learn to write and
debug Java programs without the help of an IDE will gain fundamental experience as programmers
and will likely do well on midterms and the final.
All students are expected to understand the rules listed below. While some rules may seem unforgiving, all guidelines are made to help the TAs grade efficiently and fairly. As a result, we will
generally not make exceptions. Rules listed in the syllabus also apply but may not be listed here.
• Due: The project is due on Friday, February 21st by 11:55 PM.
• Identification: Place your x500 in a comment in all files you submit. For example,
//Written by shino012.
• Submission: Submit a zip or tar archive on Canvas containing all your java files. You are
allowed to change or modify your submission, so submit early and often, and verify that all
your .java files are in the submission.
Failure to submit the correct files will result in a score of zero for all missing parts. Late
submissions will be penalized in accordance with the syllabus.
• Individual Project: You may not work with a partner for this project. All work is to be
done by yourself.
• Code: You must use the exact class and method signatures we ask for. This is because we
use a program to evaluate your code. Code that doesn’t compile will receive a significant
penalty. Code should be able to run on the CSE Labs computers.
• Questions: Questions related to the project can be discussed on Canvas in abstract. This
relates to programming in Java, understanding the writeup, and topics covered in lecture and
labs. Do not post any code or solutions on the forum. Do not e-mail the TAs your
questions when they can be asked on Canvas.
• Grading: Grading will be done by the TAs, so please address grading problems to them
privately.
1
CSCI 1933 PROJECT 1 CODE STYLE
Code Style
Part of your grade will be decided based on the “code style” demonstrated by your programming.
In general, all projects will involve a style component. This should not be intimidating, but it is
fundamentally important. The following items represent “good” coding style:
• Use effective comments to document what important variables, functions, and sections of
the code are for. In general, the TA should be able to understand your logic through the
comments left in the code.
Try to leave comments as you program, rather than adding them all in at the end. Comments
should not feel like arbitrary busy work - they should be written assuming the reader is fluent
in Java, yet has no idea how your program works or why you chose certain solutions.
• Use effective and standard indentation.
• Use descriptive names for variables. Use standard Java style for your names: ClassName,
functionName, variableName for structures in your code, and ClassName.java for the file
names.
Try to avoid the following stylistic problems:
• Missing or highly redundant/useless comments. int a = 5; //Set a to be 5 is not helpful.
• Disorganized and messy files. Poor indentation of braces ({ and }).
• Incoherent variable names. Names such as m and numberOfIndicesToCount are not useful. The former is too short to be descriptive, while the latter is much too descriptive and
redundant.
• Slow functions. While some algorithms are more efficient than others, functions that are
aggressively inefficient could be penalized even if they are otherwise correct. In general,
functions ought to terminate in under 5 seconds for any reasonable input.
The programming exercises detailed in the following pages will both be evaluated for code style.
This will not be strict – for example, one bad indent or one subjective variable name are hardly a
problem. However, if your code seems careless or confusing, or if no significant effort was made to
document the code, then points will be deducted.
In further projects we will continue to expect a reasonable attempt at documentation and style as
detailed in this section. If you are confused about the style guide, please talk with a TA.
2
CSCI 1933 PROJECT 1 1. RANDOM NUMBERS
1 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 first 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 different 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...
3
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 sufficient 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
2 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 coefficients.
• 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 find 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 current quadratic function, e.g. 9x
2 − 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 coefficients are floats, comparing them directly probably won’t work
due to floating point imprecision. Instead of directly testing if two numbers are equal,
check if their difference is within some tolerance, e.g. 0.0001.
• A main method: Like the previous class, demonstrate that all the methods work via welldocumented, well-formatted tests. You may want to include getters and setters for the coefficients 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
3 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