Question
JAVA package algs11; import java.util.Arrays; import stdlib.*; /** * This is a skeleton file for your homework. Edit the sections marked TODO. You * may
JAVA
package algs11;
import java.util.Arrays; import stdlib.*;
/** * 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. */ public class CSC300Program2 {
/** * numUnique returns the number of unique values in an array of doubles. * Unlike the previous questions, the array may be empty and it may contain * duplicate values. Also unlike the previous questions, you can assume the * array is sorted. * * Your solution must go through the array exactly once. 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 }) **/ public static int numUnique (double[] list) { return StdRandom.uniform (100); //TODO: fix this }
/** * 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 }) **/ public static double[] removeDuplicates (double[] list) { return list; // TODO: fix this } /** * Objective: become familiar with the drawing routines in the StdDraw library. page 43 * * #1 : create a cohesive image. */ public static void drawASimplePicture() { // To Do: remove the code below and replace your code which meets the above spec StdDraw.square(.5, .5, .5); StdDraw.setPenColor(StdDraw.BLUE); StdDraw.line(.5, .5, .9, .5); StdDraw.line(.9, .5, .5, .8); StdDraw.line(.5, .8, .5, .5); StdDraw.circle(.7, .65, .25); } /** * 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. */ public static void main (String[] args) { // for numUnique: array must be sorted testNumUnique (0, new double[] {}); testNumUnique(1,new double[] {11}); testNumUnique(1,new double[] {11,11,11,11}); testNumUnique(4,new double[] {11,21,31,41}); testNumUnique(4,new double[] {11,11,11,21,31,31,31,31,41}); testNumUnique(4,new double[] {11,21,21,21,31,41,41,41,41}); testNumUnique(4,new double[] {11,11,21,21,21,31,31,41,41,41,41}); testNumUnique(8,new double[] {11,11,11,11,21,31,41,41,41,41,41,51,51,61,71,81,81}); testNumUnique(8,new double[] {11,21,31,41,41,41,41,41,51,51,61,71,81}); testNumUnique(7,new double[] {11,11,11,11,21,31,41,41,41,41,41,51,51,61,71}); testNumUnique(7,new double[] {11,21,31,41,41,41,41,41,51,51,61,71}); testNumUnique(8,new double[] {-81,-81,-81,-81,-71,-61,-51,-51,-51,-51,-41,-41,-31,-21,-11,-11,-11});
// for removeDuplicates: array must be sorted testRemoveDuplicates (new double[] {}, new double[] {}); testRemoveDuplicates (new double[] {11}, new double[] {11} ); testRemoveDuplicates (new double[] {11}, new double[] {11,11,11,11} ); testRemoveDuplicates (new double[] {11,21,31,41}, new double[] {11,21,31,41} ); testRemoveDuplicates (new double[] {11,21,31,41}, new double[] {11,11,11,21,31,31,31,31,41} ); testRemoveDuplicates(new double[] {11,21,31,41} , new double[] {11,21,21,21,31,41,41,41,41} ); testRemoveDuplicates(new double[] {11,21,31,41} , new double[] {11,11,21,21,21,31,31,41,41,41,41} ); testRemoveDuplicates(new double[] {11,21,31,41,51,61,71,81} , new double[] {11,11,11,11,21,31,41,41,41,41,41,51,51,61,71,81,81} ); testRemoveDuplicates(new double[] {11,21,31,41,51,61,71,81} , new double[] {11,21,31,41,41,41,41,41,51,51,61,71,81} ); testRemoveDuplicates(new double[] {11,21,31,41,51,61,71} , new double[] {11,11,11,11,21,31,41,41,41,41,41,51,51,61,71} ); testRemoveDuplicates(new double[] {11,21,31,41,51,61,71} , new double[] {11,21,31,41,41,41,41,41,51,51,61,71} ); testRemoveDuplicates(new double[] {-81,-71,-61,-51,-41,-31,-21,-11} , new double[] {-81,-81,-81,-81,-71,-61,-51,-51,-51,-51,-41,-41,-31,-21,-11,-11,-11} ); StdOut.println ("Finished tests"); drawASimplePicture(); }
private static void testNumUnique (int expected, double[] list) { int actual = numUnique (list);
if (expected != actual) { StdOut.format ("Failed numUnique([%s]): Expecting (%d) Actual (%d) ", Arrays.toString(list), expected, actual); } } private static void testRemoveDuplicates (double[] expected, double[] list) { double[] actual = removeDuplicates (list);
// != operator does not do what we want on arrays, use equals function from Arrays class if (! Arrays.equals (expected, actual)) { StdOut.format ("Failed removeDuplicates([%s]): Expecting (%s) Actual (%s) ", Arrays.toString(list), Arrays.toString (expected), Arrays.toString (actual)); } }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started