CS 211, 004/006
Project 1
due Thursday, September 20th at noon


The objective of this project is to implement a toolbox of mathematical routines, exercising skills in writing programs which utilize conditions, loops, and arrays

Overview:

  1. Create the Java class MathToolbox.
  2. Implement the ten mathematical routines described below.
  3. Download and use the tester module to ensure that your program is correct.
  4. (Optional) Use the main method to perform your own tests.
  5. Prepare the assignment for submission and submit it
Imagine that you're building a toolkit of mathematical functions (perhaps because you foresee needing a special-purpose calculator to check your work on some theoretical assignment). To do this, you create a class (MathToolbox) with several static methods which can be called to compute the result of some function; these methods will be described below.

Rules

  1. This project is an individual effort; the Honor Code applies
  2. You may not import any extra functionality besides the default (i.e. System, Math)
  3. The main method will not be tested; you may use it any way you want.
Instructions: Download the following:

This file contains template code for the project. Your task will be to fill in each of the methods as described. Whenever you implement a method, be sure to delete the line which throws an exception. The exceptions are placeholders which keeps the unit tester running correctly even when nothing is present.

Method 1:
public static boolean isPrime(int i)

This method should return true if and only if the input i is a positive prime number. A prime is a number which is only divisible by 1 and itself (however the numbers 0 and 1 are not considered primes).

Method 2:
public static int numPrimes(int lower, int upper)

This method will return the total number of primes in the range between lower and upper, including lower and upper themselves.

Hint: it's possible to use isPrime(int i) as part of the computation.

Method 3:
public static int factorial(int n)

Find the factorial of n, which is given by n! = 1*2*...*(n-1)*n. The factorial of zero is a special case which is equal to 1.

Method 4:
public static double sumPower(int n, double p)

Sums the series 1p+2p+...+(n-1)p+np.

Method 5:
public static int boundedSum(int[] list, int low, int high)

Sums up the elements in list, but only including the elements x in list for which low ≤ x ≤ high.

Method 6:
public static double filteredSum(double[] list, double[] filterList)

Assume that list and filterList are lists of the same length. Then if the elements in list are l0, l1, ..., ln-2, ln-1 and the elements in filterList are f0, f1, ..., fn-2, fn-1, then this method will return l0f0+l1f1+...+ln-2fn-2+ln-1fn-1.

Method 7:
public static double geometricMean(double[] list)

Computes the geometric mean of the numbers in list. If the n numbers in list are l0, l1, ..., ln-2, ln-1, then the geometric mean is defined as the nth root of the product l0l1...ln-2ln-1. You may assume that the list is not empty and the numbers are all positive.

Hint: you may use the built-in Math.pow().

Method 8:
public static int largestConsecutiveDiff(int[] list)

Considering the set of differences between consecutive elements in list, this method returns the largest. Consecutive difference is defined as the latter element minus the earlier element, i.e. l1-l0. If there are not enough elements in the list to compute any differences, then the method should return zero by default.

Method 9:
public static int largestElementWithDups(int[] list)

A duplicate element is an element which appears more than once in the list. This method returns the largest element in list which has any duplicates whatsoever. If none of the elements have duplicates (every element in the list is unique) then this method should return Integer.MIN_VALUE.

Method 10:
public static void findElementFreqs(int[] list, int[] freqs)

Assume that list and freqs are lists of the same size. For each element in list, this method should count the number of times that elements occurs in the list, and store the value in the same spot in freqs.

For example, if list[3] == 4, and the number 4 appears a total of 7 times in list, then the value 7 should be stored in freqs[3]. The same goes for all elements in freqs

Testing:

Download the following:

These include sample test cases. For grading, we may modify the set of test cases.

When you think your programs are ready (you can run them even without the tester if you write a main method), do the following from the command prompt to run the tests.
On Windows:

javac -cp .;junit-cs211.jar *.java
java -cp .;junit-cs211.jar P1tester
On Mac or Linux:
javac -cp .:junit-cs211.jar *.java
java -cp .:junit-cs211.jar P1tester
In Dr Java:

Grading:

The implementation of each method described above is worth 10% of the total. Of that 10%, half will be based on automatic testing (the percent of test cases which passed). 1% will be granted for style (i.e. commenting code, not writing unreadable code). The remainder will be awarded based on manual inspection. Thus, partial credit is possible, but hard-coding to pass a certain set of test cases will receive an automatic zero on the manual inspection points.

Programs which do not compile without modification will receive a zero in most cases.

Submission:

Submission instructions are as follows.

  1. Let xxx be your lab section number (one of 213-220), and let yyyyyyyy be your GMU userid. Create the directory xxx_yyyyyyyy_P1/
  2. Place the file MathToolbox.java in the directory you've just created.
  3. Create the file ID.txt in the format shown below, containing your name, userid, G#, lecture section and lab section, and add it to the directory. Your directory should contain two files total.

    Full Name: Donald Knuth
    userID: dknuth
    G#: 00123456
    Lecture section: 004
    Lab section: 213

  4. compress the folder and its contents into a .zip file, and upload the file to Blackboard.