Problem 1. (Three Sort) Write a programThreeSort that takes three int values as command-line arguments and prints them in ascending order, separated by spaces. Use Math.min() and Math.max(); you are not allowed to use any conditionals.

$ java ThreeSort 1 2 3
123
$ java ThreeSort 1 3 2
123
$ java ThreeSort 2 1 3
123
$ java ThreeSort 2 3 1
123
$ java ThreeSort 3 1 2
123
$ java ThreeSort 3 2 1
123

Problem 2. (Remove Duplicates) ModifyInstrumentedBinarySearch from Lab 2 to remove any duplicate keys in the whitelist after the sort. The number of keys examined in this case should be smaller than when the duplicates are not removed. Do you see why? You are not allowed to use any collection data type (eg, ArrayList). Hint: Determine the number of unique elements in whitelist[]; copy those elements into a new array of that size; and make whitelist point to that array.

$ java InstrumentedBinarySearch tinyW.txt < tinyT.txt
65

Problem 3. (Relatively Prime Numbers) Write a programRelativelyPrime that takes an int value N as a command-line argument and prints an N-by-N matrix such that the element in row i and column j (1 i,j N) is a"*" (a star) if i and j are relatively prime (ie, GCD(i,j) = 1) and""(a space) otherwise. The row numbers should be printed at the end of each row.

$ java RelativelyPrime 10
**********1
*****2
* * * * * * * 3
*****4
* * * * * * * * 5
* * * 6
* * * * * * * * * 7
*****8
* * * * * * * 9
* * * * 10

Problem 4. (Matrix Library) Write aMatrix library that implements the following API:

public class Matrix
// vector dot product
public static double dot(double[] x, double[] y)

// matrix-matrix product
public static double[][] mult(double[][] a, double[][] b)

// transpose
public static double[][] transpose(double[][] a)

// matrix-vector
product public static double[] mult(double[][] a, double[] x)

// vector-matrix product
public static double[] mult(double[] x, double[][] a)
$ java Matrix < matrix.txt
x:
3
1.00000 1.00000 1.00000
y:
3
1.00000 2.00000 1.00000
a:
3 3
1.00000 4.00000 1.00000
2.00000 3.00000 5.00000
1.00000 6.00000 2.00000
b:
3 3
0.00000 9.00000 7.00000
4.00000 6.00000 1.00000
2.00000 4.00000 5.00000
dot(x, y) = 4.0
mult(a, b):
3 3
18.00000 37.00000 16.00000
22.00000 56.00000 42.00000
28.00000 53.00000 23.00000
transpose(a):
3 3
1.00000 2.00000 1.00000
4.00000 3.00000 6.00000
1.00000 5.00000 2.00000
mult(a, x):
3
6.00000 10.00000 9.00000
mult(y, b):
3
10.00000 25.00000 14.00000

Problem 5. (Rational Numbers) Implement an immutable data typeRational for rational numbers that supports addition, subtraction, multiplication, and division. Use Euclids algorithm (see page 4) to ensure that the numerator and denominator never have any common factors (eg, 4/8 is represented as 1/2), and do this within the twoargument constructor.

public class Rational

// constructs rational number n / d.
Rational(long n, long d)

// constructs rational number n / 1.
public Rational(long n)

// sum of this number and b.
public Rational plus(Rational b)

// difference of this number and b.
public Rational minus(Rational b)

// product of this number and b.
public Rational times(Rational b)

// quotient of this number and b.
public Rational quotient(Rational b)

// is this number equal to b?
public Rational plus(Rational b)

// string representation
public String toString()
$ java Rational 40
PI (using Leibniz series) = 3.09162

In case you are curious about the test client, it computes the value of using the celebrated Leibniz series, whose n terms are

PI/4 = 1 - (1 / 3) + (1 / 5) - (1 / 7) + ... + (1 / 2n - 1)

The approximation is not very good because Leibniz series converges extremely slowly and we cant try bigger values of n because it causes overow in the long data type we use to represent the numerator and denominator in Rational.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.