MathToolbox
.main
method to perform your own tests.MathToolbox
)
with several static methods which can be called to compute the result of some function;
these methods will be described below.
Rules
main
method will not be tested; you may use it any way you want.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 n
th 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:
javac -cp .;junit-cs211.jar *.java
java -cp .;junit-cs211.jar P1tester
javac -cp .:junit-cs211.jar *.java
java -cp .:junit-cs211.jar P1tester
P1tester.java
, and click the Test button.
Submission instructions are as follows.
xxx_yyyyyyyy_P1/
MathToolbox.java
in the directory you've just created.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