The purpose of this assignment is to give you practice writing programs with Java functions (static methods). The first exercise involves real-valued functions; the second exercise focuses on integer-valued functions; the third exercise considers functions on arrays.
\( \displaystyle H(x) \; = \; \begin{cases} \; 0 & \text{if } x < 0 \\ \, \frac{1}{2} & \text{if } x = 0 \\ \; 1 & \text{if } x > 0 \end{cases} \)
\(\displaystyle \sigma(x) = \frac{1}{1 + e^{-x}}\)
\(\displaystyle \tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}}\)
\(\displaystyle f(x) = \frac{x}{1 + |x|}\)
\( \displaystyle \text{SQNL}(x) \; = \; \begin{cases} \; -1 & \text{if } x \le -2 \\ \; x + \frac{x^2}{4} & \text{if } -2 < x < 0 \\ \; x - \frac{x^2}{4} & \text{if } 0 \le x < 2 \\ \; 1 & \text{if } x \ge 2 \end{cases} \)
All activation functions should return NaN (not a number) if the argument is NaN.
To do so, organize your program according to the following public API:
public class ActivationFunction { // Returns the Heaviside function of x. public static double heaviside(double x) // Returns the sigmoid function of x. public static double sigmoid(double x) // Returns the hyperbolic tangent of x. public static double tanh(double x) // Returns the softsign function of x. public static double softsign(double x) // Returns the square nonlinearity function of x. public static double sqnl(double x) // Takes a double command-line argument x and prints each activation // function, evaluated, in the format (and order) given below. public static void main(String[] args) }
Here are some sample executions:
~/Desktop/functions> java-introcs ActivationFunction 0.0 heaviside(0.0) = 0.5 sigmoid(0.0) = 0.5 tanh(0.0) = 0.0 softsign(0.0) = 0.0 sqnl(0.0) = 0.0 ~/Desktop/functions> java-introcs ActivationFunction 1.0 heaviside(1.0) = 1.0 sigmoid(1.0) = 0.7310585786300049 tanh(1.0) = 0.7615941559557649 softsign(1.0) = 0.5 sqnl(1.0) = 0.75 ~/Desktop/functions> java-introcs ActivationFunction -0.5 heaviside(-0.5) = 0.0 sigmoid(-0.5) = 0.3775406687981454 tanh(-0.5) = -0.4621171572600098 softsign(-0.5) = -0.3333333333333333 sqnl(-0.5) = -0.4375
To do so, organize your program according to the following public API:
public class Divisors { // Returns the greatest common divisor of a and b. public static int gcd(int a, int b) // Returns the least common multiple of a and b. public static int lcm(int a, int b) // Returns true if a and b are relatively prime; false otherwise. public static boolean areRelativelyPrime(int a, int b) // Returns the number of integers between 1 and n that are // relatively prime with n. public static int totient(int n) // Takes two integer command-line arguments a and b and prints // each function, evaluated in the format (and order) given below. public static void main(String[] args) }
Here are some sample executions:
~/Desktop/functions> java-introcs Divisors 1440 408 gcd(1440, 408) = 24 lcm(1440, 408) = 24480 areRelativelyPrime(1440, 408) = false totient(1440) = 384 totient(408) = 128 ~/Desktop/functions> java-introcs Divisors 987 610 gcd(987, 610) = 1 lcm(987, 610) = 602070 areRelativelyPrime(987, 610) = true totient(987) = 552 totient(610) = 240
Use the following algorithms to implement the corresponding functions.
\(\displaystyle lcm(a, b) = \frac{|a| \cdot |b|}{gcd(a, b)} \)To avoid preventable arithmetic overflow, perform the division before the multiplication. Recall that \(lcm(0, 0) = 0\).
areRelativelyPrime()
for each positive integer between 1 and n.
The greatest common divisor and least common multiple functions arise in a variety of applications, including reducing fractions, modular arithmetic, and cryptography. Euler’s totient function plays an important role in number theory, including Euler’s theorem and cyclotomic polynomials.
To do so, organize your program according to the following public API:
Here is some more information about the required behavior:public class AudioCollage { // Returns a new array that rescales a[] by a multiplicative factor of alpha. public static double[] amplify(double[] a, double alpha) // Returns a new array that is the reverse of a[]. public static double[] reverse(double[] a) // Returns a new array that is the concatenation of a[] and b[]. public static double[] merge(double[] a, double[] b) // Returns a new array that is the sum of a[] and b[], // padding the shorter arrays with trailing 0s if necessary. public static double[] mix(double[] a, double[] b) // Returns a new array that changes the speed by the given factor. public static double[] changeSpeed(double[] a, double alpha) // Creates an audio collage and plays it on standard audio. // See below for the requirements. public static void main(String[] args) }
null
and that
\( \alpha > 0\) in changeSpeed()
.
main()
method must create an audio collage and play it
using StdAudio.play()
.
StdAudio.read()
to read a WAV file and extract
its samples as an array of floating-point numbers between –1 and +1.
This problem was inspired by an audio effects programming assignment by Keith Vertanen.
Submission.
Submit a .zip
file containing
ActivationFunction.java
,
Divisors.java
, and
AudioCollage.java
.
You may also submit supplementary .wav files—put them in the same directory as your .java files.
(There is no need to submit the provided .wav files.)
You may not call library functions except those in java.lang
.
Use only Java features that have already been introduced in the course
(e.g., loops, arrays, and functions but not objects).