Package algs11

Class MyFirstHomeworkFor402

java.lang.Object
algs11.MyFirstHomeworkFor402

public class MyFirstHomeworkFor402 extends Object
This is a skeleton file for your homework. Edit the sections marked TODO. You may also edit the function "main" to test your code.

You must not change the declaration of any method. This will be true of every skeleton file I give you.

For example, you will get zero points if you change the line

     public static double minValue (double[] list) {
 
to something like
     public static void minValue (double[] list) {
 
or
     public static double minValue (double[] list, int i) {
 

Each of the functions below is meant to be SELF CONTAINED. This means that you should use no other functions or classes. You should not use any HashSets or ArrayLists, or anything else! In addition, each of your functions should go through the argument array at most once. The only exception to this removeDuplicates, which is allowed to call numUnique and then go through the array once after that.

  • Constructor Details

  • Method Details

    • minValue

      public static double minValue(double[] list)
      minValue returns the minimum value in an array of doubles. You can assume the array is nonempty and has no duplicates. Your solution must go through the array exactly once. Your solution must not call any other functions. Here are some examples (using "==" informally):
         -7  == minValue (new double[] { -7 })
          1  == minValue (new double[] { 1, 7, 8, 11 })
         -7  == minValue (new double[] { 1, -4, -7, 7, 8, 11 })
         -13 == minValue (new double[] { -13, -4, -7, 7, 8, 11 })
         -13 == minValue (new double[] { 1, -4, -7, 7, 8, 11, -13 })
       
    • minPosition

      public static int minPosition(double[] list)
      minPosition returns the position of the minimum value in an array of doubles. The first position in an array is 0 and the last is the array.length-1.

      You can assume the array is nonempty and has no duplicates. Your solution must go through the array exactly once. Your solution must not call any other functions. Here are some examples (using "==" informally):

         0 == minPosition(new double[] { -7 })
         2 == minPosition(new double[] { 1, -4, -7, 7, 8, 11 })
         0 == minPosition(new double[] { -13, -4, -7, 7, 8, 11 })
         6 == minPosition(new double[] { 1, -4, -7, 7, 8, 11, -9 })
       
    • distanceBetweenMinAndMax

      public static int distanceBetweenMinAndMax(double[] list)
      distanceBetweenMinAndMax returns difference between the minPosition and the maxPosition in an array of doubles.

      You can assume the array is nonempty and has no duplicates. Your solution must go through the array exactly once. Your solution must not call any other functions. Here are some examples (using "==" informally):

         0 == distanceBetweenMinAndMax(new double[] { -7 })                      // -7,-7 are the min and max
         3 == distanceBetweenMinAndMax(new double[] { 1, -4, -7, 7, 8, 11 }),    // -7,11
         5 == distanceBetweenMinAndMax(new double[] { -13, -4, -7, 7, 8, 11 })   // -13,11
         1 == distanceBetweenMinAndMax(new double[] { 1, -4, -7, 7, 8, 11, -9 }) // -9,11
       
    • allSame

      public static boolean allSame(double[] list)
      allSame returns true if all of the elements in list have the same value. allSame returns false if any two elements in list have different values. The array may be empty and it may contain duplicate values.

      Your solution should contain at most one loop. You may not use recursion. Your solution must not call any other functions. Here are some examples (using "==" informally):

           true  == allSame(new double[] { })
           true  == allSame(new double[] { 11 })
           true  == allSame(new double[] { 11, 11, 11, 11 })
           false == allSame(new double[] { 11, 11, 11, 22 })
           false == allSame(new double[] { 11, 11, 22, 11 })
           true  == allSame(new double[] { 22, 22, 22, 22 })
       
    • numUnique

      public static int numUnique(double[] list)
      numUnique returns the number of unique values in an array of doubles. The array may be empty and it may contain duplicate values. Unlike the previous questions, you can assume the array is sorted.

      Your solution should contain at most one loop. You may not use recursion. Your solution must not call any other functions. Here are some examples (using "==" informally):

           0 == numUnique(new double[] { })
           1 == numUnique(new double[] { 11 })
           1 == numUnique(new double[] { 11, 11, 11, 11 })
           8 == numUnique(new double[] { 11, 11, 11, 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88, 88 })
           8 == numUnique(new double[] { 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88 })
       
    • removeDuplicates

      public static double[] removeDuplicates(double[] list)
      removeDuplicates returns a new array containing the unique values in the array. There should not be any extra space in the array --- there should be exactly one space for each unique element (Hint: numUnique tells you how big the array should be). You may assume that the list is sorted, as you did for numUnique.

      Your solution may call numUnique, but should not call any other functions. After the call to numUnique, you must go through the array exactly one more time. Here are some examples (using "==" informally):

         new double[] { }
           == removeDuplicates(new double[] { })
         new double[] { 11 }
           == removeDuplicates(new double[] { 11 })
           == removeDuplicates(new double[] { 11, 11, 11, 11 })
         new double[] { 11, 22, 33, 44, 55, 66, 77, 88 }
           == removeDuplicates(new double[] { 11, 11, 11, 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88, 88 })
           == removeDuplicates(new double[] { 11, 22, 33, 44, 44, 44, 44, 44, 55, 55, 66, 77, 88 })
       
    • main

      public static void main(String[] args)
      A test program, using private helper functions. See below. To make typing tests a little easier, I've written a function to convert strings to arrays. See below.
    • testMinValue

      private static void testMinValue(double expected, String list)
    • testMinPosition

      private static void testMinPosition(int expected, String list)
    • testDistanceBetweenMinAndMax

      private static void testDistanceBetweenMinAndMax(int expected, String list)
    • testAllSame

      private static void testAllSame(boolean expected, String list)
    • testNumUnique

      private static void testNumUnique(int expected, String list)
    • testRemoveDuplicates

      private static void testRemoveDuplicates(String expected, String list)
    • doublesFromString

      private static double[] doublesFromString(String s)