Package algs11

Class MyFirstHomeworkFor300PartTwo

java.lang.Object
algs11.MyFirstHomeworkFor300PartTwo

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 Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    allSame(double[] list)
    allSame returns true if all of the elements in list have the same value.
    static void
    main(String[] args)
    A test program, using private helper functions.
    static void
    main2(String[] args)
     
    static int
    numUnique(double[] list)
    numUnique returns the number of unique values in a sorted array of doubles.
    static double[]
    removeDuplicates(double[] list)
    removeDuplicates returns a new array containing the unique values in the sorted argument array, in the same order that they were found in the original array.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • 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 a sorted array of doubles. The array may be empty and it may contain duplicate values. Assume that the list 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 sorted argument array, in the same order that they were found in the original 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). Assume that the list array is sorted, as you did for numUnique. Your solution should contain at most one loop. You may not use recursion. Your solution must not call any other functions, except numUnique. 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 })
       
    • main2

      public static void main2(String[] args)
    • 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.