CSE1030Z Week 02 Homework Problems

Utility Classes

Easy Problems

  1. Implement the utility class with this API.
  2. Implement a utility class that helps you calculate Einstein's famous mass-energy equivalence equation E = mc2 where m is mass (in kilograms), c is the speed of light (in metres per second), and E is energy (in joules). Use your utility class to compute the amount of energy equivalent to one gram of matter (this is approximately equal to the energy released by the first atomic bomb detonated during the Trinity test).
  3. The utility class java.lang.Math is very useful, but its methods assume that you want to work with just one value at a time. Create a utility class called ArrayMath that has methods that operate on arrays of numbers. Your utility class should provide the following methods:

    static double[] cos(double[] a)
    Returns the trigonometric cosine of the angles in a.

    static double[] sin(double[] a)
    Returns the trigonometric sine of the angles in a.

    static double[] tan(double[] a)
    Returns the trigonometric tangent of the angles in a.

    Your methods should assume as a precondition that the array parameter a is not empty. Also, your method must not modify the elements in a.

Moderate Problems

  1. Add to your utility class ArrayMath the following methods:

    static double max(double[] a)
    Returns the largest value in a.

    static double min(double[] a)
    Returns the smallest value in a.

    static double mean(double[] a)
    Returns the mean (average) value of the elements of a.

    Your methods should assume as a precondition that the array parameter a is not empty. Also, your method must not modify the elements in a.
  2. Create a utility class named StringUtils that provides methods that operate on strings. Implement the following methods:

    static String reverse(String s)
    Returns a string that is the same as s in reverse.

    static boolean isPalindrome(String s)
    Returns true if s.equals(StringUtils.reverse(s)) == true and false otherwise.

    A palindrome is a string that is the same when it is reversed (for example, "civic", "madam", and "radar" are all palindromes).

Hard/Creative Problems

  1. Add to your utility class ArrayMath the following methods:

    static int[] mode(int[] a)
    Returns the mode of the elements of a. The mode is the value that occurs the most often in a; if there are multiple values that occur the most often, then all of the values are returned sorted from smallest to largest.

    Your methods should assume as a precondition that the array parameter a is not empty. Also, your method must not modify the elements in a.
  2. Consider the Yahtzee utility from the labs. To implement the game of Yahtzee as a computer program you need to determine the score associated with a roll of the dice. Look up the rules for Yahtzee and add a method or methods to the Yahtzee utility that compute the score of a roll.

    Note that a roll can have many scores; for example, the roll 2-2-3-3-3 could be scored as 4 (two 2s), 9 (three 3s), 13 (three-of-a-kind), 25 (full house), or 13 (chance).