Fix all sections marked TO DO. Must show
output.
package JavaExcercise;
import java.util.Arrays;
import stdlib.*;
/**
* Do not change the declaration of any method.
* Each of the functions below is meant to be SELF CONTAINED. This means that
* you should use no other functions or classes unless otherwise indicated.
* 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 is
* removeDuplicates, which is allowed to call numDuplicates and go through the
* array once after.
*/
public class Program {
/**
* numDuplicates returns the number of duplicate values in an array of characters.
* Precondition: the array may be empty, but if it is not empty the array is sorted from low to high.
* { your solution can assume this is true }
*
* Your solution may go through the array exactly once. Your solution must
* not call any other functions. Here are some examples (using "=="
* informally):
*
* <pre>
* 0 == numDuplicates(new char[] { })
* 0 == numDuplicates(new char[] { 'a' })
* 3 == numDuplicates(new char[] { 'a', 'a', 'a', 'a' })
* 9 == numDuplicates(new char[] { 'a', 'a', 'a', 'a', 'b', 'd', 'g', 'g', 'g', 'g', 'g', 'h', 'h', 'm', 'p', 't', 't' })
* 5 == numDuplicates(new char[] { 'a', 'b', 'd', 'g', 'g', 'g', 'g', 'g', 'h', 'h', 'm', 'p', 't' })
* </pre>
*/
public static int numDuplicates (char[] list) {
return -1; //TODO
}
/**
* 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: numDuplicates can
* help you determine how big the array should be).
* You may assume that the list is sorted, as you did for numDuplicates.
*
* Your solution may call numDuplicates, but should not call any other
* functions. After the call to numDuplicates, you may go through the array
* exactly one time. Here are some examples; the left hand side of "<--"
* means that is the returned result from the function call on the right hand side
*
* <pre>
* new char[] { } <-- removeDuplicates(new char[] { })
* new char[] { 'a' } <-- removeDuplicates(new char[] { 'a' })
* new char[] { 'a' } <-- removeDuplicates(new char[] { 'a', 'a', 'a', 'a' })
* new char[] { 'a', 'b', 'd', 'g', 'm', 'p', 't', 'w' }
* <-- removeDuplicates(new char[] { 'a', 'a', 'a', 'a', 'b', 'd', 'g', 'g', 'g', 'g', 'g', 'm', 'm', 'p', 't', 'w', 'w' })
* </pre>
*/
public static char[] removeDuplicates (char[] list) {
return list; // TODO
}
/**
* A test program, using private helper functions. See below.
* You are encouraged to review the test cases and testing code below, but you should not
* change any of the code below.
*/
public static void main (String[] args) {
// for numUnique: array must be sorted
testNumDuplicates (0, new char[] {});
testNumDuplicates(0,new char[] {'a'});
testNumDuplicates(3,new char[] {'a','a','a','a'});
testNumDuplicates(0,new char[] {'a','b','d','g'});
testNumDuplicates(5,new char[] {'a','a','a','b','d','d','d','d','g'});
testNumDuplicates(5,new char[] {'a','b','b','b','d','g','g','g','g'});
testNumDuplicates(7,new char[] {'a','a','b','b','b','d','d','g','g','g','g'});
testNumDuplicates(6,new char[] {'a','a','b','d','g','g','g','g','h','h','m','p','t','t'});
testNumDuplicates(5,new char[] {'a','b','d','g','g','g','g','g','h','h','m','p','t'});
testNumDuplicates(8,new char[] {'a','a','a','a','b','d','g','g','g','g','g','h','h','m','p'});
testNumDuplicates(5,new char[] {'a','b','d','g','g','g','g','g','h','h','m','p'});
testNumDuplicates(7,new char[] {'t','t','t','p','m','h','h','h','g','g','d','b','a','a','a'});
// for removeDuplicates: array must be sorted
testRemoveDuplicates (new char[] {}, new char[] {});
testRemoveDuplicates (new char[] {'a'}, new char[] {'a'} );
testRemoveDuplicates (new char[] {'a'}, new char[] {'a','a','a','a'} );
testRemoveDuplicates (new char[] {'a','b','d','g'}, new char[] {'a','b','d','g'} );
testRemoveDuplicates (new char[] {'a','b','d','g'}, new char[] {'a','a','a','b','d','d','d','d','g'} );
testRemoveDuplicates(new char[] {'a','b','d','g'} , new char[] {'a','b','b','b','d','g','g','g','g'} );
testRemoveDuplicates(new char[] {'a','b','d','g'} , new char[] {'a','a','b','b','b','d','d','g','g','g','g'} );
testRemoveDuplicates(new char[] {'a','b','d','g','h','m','p','t'} , new char[] {'a','a','a','a','b','d','g','g','g','g','g','h','h','m','p','t','t'} );
testRemoveDuplicates(new char[] {'a','b','d','g','h','m','p','t'} , new char[] {'a','b','d','g','g','g','g','g','h','h','m','p','t'} );
testRemoveDuplicates(new char[] {'a','b','d','g','h','m','p'} , new char[] {'a','a','a','a','b','d','g','g','g','g','g','h','h','m','p'} );
testRemoveDuplicates(new char[] {'a','b','d','g','h','m','p'} , new char[] {'a','b','d','g','g','g','g','g','h','h','m','p'} );
StdOut.println ("Finished tests");
}
private static void testNumDuplicates (int expected, char[] list) {
int actual = numDuplicates (list);
if (expected != actual) {
StdOut.format ("Failed numDuplicates %s: Expecting (%d) Actual (%d)\n", Arrays.toString(list), expected, actual);
}
}
private static void testRemoveDuplicates (char[] expected, char[] list) {
char[] 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\n", Arrays.toString(list), Arrays.toString (expected), Arrays.toString (actual));
}
}
}
package JavaExcercise;
import java.util.Arrays;
import stdlib.*;
/**
* Do not change the declaration of any method.
* <p>
* Each of the functions below is meant to be SELF CONTAINED. This means that
* <p>
* you should use no other functions or classes unless otherwise indicated.
* <p>
* You should not use any HashSets or ArrayLists, or anything else.
* <p>
* In addition, each of your functions should go
* <p>
* through the argument array at most once. The only exception to this is
* <p>
* removeDuplicates, which is allowed to call numDuplicates and go through the
* <p>
* array once after.
*/
public class Program {
/**
* numDuplicates returns the number of duplicate values in an array of characters.
* <p>
* Precondition: the array may be empty, but if it is not empty the array is sorted from low to high.
* <p>
* { your solution can assume this is true }
* <p>
* <p>
* <p>
* Your solution may go through the array exactly once. Your solution must
* <p>
* not call any other functions. Here are some examples (using "=="
* <p>
* informally):
*
*
*
* <pre>
*
* 0 == numDuplicates(new char[] { })
*
* 0 == numDuplicates(new char[] { 'a' })
*
* 3 == numDuplicates(new char[] { 'a', 'a', 'a', 'a' })
*
* 9 == numDuplicates(new char[] { 'a', 'a', 'a', 'a', 'b', 'd', 'g', 'g', 'g', 'g', 'g', 'h', 'h', 'm', 'p', 't', 't' })
*
* 5 == numDuplicates(new char[] { 'a', 'b', 'd', 'g', 'g', 'g', 'g', 'g', 'h', 'h', 'm', 'p', 't' })
*
* </pre>
*/
public static int numDuplicates(char[] list) {
int count = 0;
for (int i = 1; i < list.length; i++) {
if (list[i - 1] == list[i])
++count;
}
return count;
}
/**
* removeDuplicates returns a new array containing the unique values in the
* <p>
* array. There should not be any extra space in the array --- there should
* <p>
* be exactly one space for each unique element (Hint: numDuplicates can
* <p>
* help you determine how big the array should be).
* <p>
* You may assume that the list is sorted, as you did for numDuplicates.
* <p>
* <p>
* <p>
* Your solution may call numDuplicates, but should not call any other
* <p>
* functions. After the call to numDuplicates, you may go through the array
* <p>
* exactly one time. Here are some examples; the left hand side of "<--"
* <p>
* means that is the returned result from the function call on the right hand side
*
*
*
* <pre>
*
* new char[] { } <-- removeDuplicates(new char[] { })
*
* new char[] { 'a' } <-- removeDuplicates(new char[] { 'a' })
*
* new char[] { 'a' } <-- removeDuplicates(new char[] { 'a', 'a', 'a', 'a' })
*
* new char[] { 'a', 'b', 'd', 'g', 'm', 'p', 't', 'w' }
*
* <-- removeDuplicates(new char[] { 'a', 'a', 'a', 'a', 'b', 'd', 'g', 'g', 'g', 'g', 'g', 'm', 'm', 'p', 't', 'w', 'w' })
*
* </pre>
*/
public static char[] removeDuplicates(char[] list) {
char[] result = new char[list.length - numDuplicates(list)];
int index = 0;
for (int i = 0; i < list.length; i++) {
if (i == 0 || list[i - 1] != list[i]) {
result[index++] = list[i];
}
}
return result;
}
/**
* A test program, using private helper functions. See below.
* <p>
* You are encouraged to review the test cases and testing code below, but you should not
* <p>
* change any of the code below.
*/
public static void main(String[] args) {
// for numUnique: array must be sorted
testNumDuplicates(0, new char[]{});
testNumDuplicates(0, new char[]{'a'});
testNumDuplicates(3, new char[]{'a', 'a', 'a', 'a'});
testNumDuplicates(0, new char[]{'a', 'b', 'd', 'g'});
testNumDuplicates(5, new char[]{'a', 'a', 'a', 'b', 'd', 'd', 'd', 'd', 'g'});
testNumDuplicates(5, new char[]{'a', 'b', 'b', 'b', 'd', 'g', 'g', 'g', 'g'});
testNumDuplicates(7, new char[]{'a', 'a', 'b', 'b', 'b', 'd', 'd', 'g', 'g', 'g', 'g'});
testNumDuplicates(6, new char[]{'a', 'a', 'b', 'd', 'g', 'g', 'g', 'g', 'h', 'h', 'm', 'p', 't', 't'});
testNumDuplicates(5, new char[]{'a', 'b', 'd', 'g', 'g', 'g', 'g', 'g', 'h', 'h', 'm', 'p', 't'});
testNumDuplicates(8, new char[]{'a', 'a', 'a', 'a', 'b', 'd', 'g', 'g', 'g', 'g', 'g', 'h', 'h', 'm', 'p'});
testNumDuplicates(5, new char[]{'a', 'b', 'd', 'g', 'g', 'g', 'g', 'g', 'h', 'h', 'm', 'p'});
testNumDuplicates(7, new char[]{'t', 't', 't', 'p', 'm', 'h', 'h', 'h', 'g', 'g', 'd', 'b', 'a', 'a', 'a'});
// for removeDuplicates: array must be sorted
testRemoveDuplicates(new char[]{}, new char[]{});
testRemoveDuplicates(new char[]{'a'}, new char[]{'a'});
testRemoveDuplicates(new char[]{'a'}, new char[]{'a', 'a', 'a', 'a'});
testRemoveDuplicates(new char[]{'a', 'b', 'd', 'g'}, new char[]{'a', 'b', 'd', 'g'});
testRemoveDuplicates(new char[]{'a', 'b', 'd', 'g'}, new char[]{'a', 'a', 'a', 'b', 'd', 'd', 'd', 'd', 'g'});
testRemoveDuplicates(new char[]{'a', 'b', 'd', 'g'}, new char[]{'a', 'b', 'b', 'b', 'd', 'g', 'g', 'g', 'g'});
testRemoveDuplicates(new char[]{'a', 'b', 'd', 'g'}, new char[]{'a', 'a', 'b', 'b', 'b', 'd', 'd', 'g', 'g', 'g', 'g'});
testRemoveDuplicates(new char[]{'a', 'b', 'd', 'g', 'h', 'm', 'p', 't'}, new char[]{'a', 'a', 'a', 'a', 'b', 'd', 'g', 'g', 'g', 'g', 'g', 'h', 'h', 'm', 'p', 't', 't'});
testRemoveDuplicates(new char[]{'a', 'b', 'd', 'g', 'h', 'm', 'p', 't'}, new char[]{'a', 'b', 'd', 'g', 'g', 'g', 'g', 'g', 'h', 'h', 'm', 'p', 't'});
testRemoveDuplicates(new char[]{'a', 'b', 'd', 'g', 'h', 'm', 'p'}, new char[]{'a', 'a', 'a', 'a', 'b', 'd', 'g', 'g', 'g', 'g', 'g', 'h', 'h', 'm', 'p'});
testRemoveDuplicates(new char[]{'a', 'b', 'd', 'g', 'h', 'm', 'p'}, new char[]{'a', 'b', 'd', 'g', 'g', 'g', 'g', 'g', 'h', 'h', 'm', 'p'});
StdOut.println("Finished tests");
}
private static void testNumDuplicates(int expected, char[] list) {
int actual = numDuplicates(list);
if (expected != actual) {
StdOut.format("Failed numDuplicates %s: Expecting (%d) Actual (%d)\n", Arrays.toString(list), expected, actual);
}
}
private static void testRemoveDuplicates(char[] expected, char[] list) {
char[] 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\n", Arrays.toString(list), Arrays.toString(expected), Arrays.toString(actual));
}
}
}


Fix all sections marked TO DO. Must show output. package JavaExcercise; import java.util.Arrays; import stdlib.*; /**...
package homework; import java.util.Arrays; import stdlib.*; /** CSC300Homework4 version 1.0 * * * * Find the 3 Sections marked TODO: * * TODO #1: SumOddsRecursive * TODO #2: ReverseArray * TODO #3: MergeArrays * * You must not add static variables. You MAY add static functions. * * It is okay to add functions, such as * * <pre> * public static double sumOfOddsHelper (double[] list, int i) { * </pre> * * but it is NOT okay to...
import java.util.Arrays; import stdlib.*; public class CSC300Homework4 { /** * As a model for Problem 1, here are two functions to find the minimum value of an array of ints * an iterative version and a recursive version * * precondition: list is not empty /** iterative version */ public static double minValueIterative (int[] list) { int result = list[0]; int i = 1; while (i <...
package exampleassignment; import java.util.Arrays; import stdlib.*; public class assignment { /** * valRange returns the difference between the maximum and minimum values in the * array; Max-Min. Precondition: the array is nonempty. Your solution must go * through the array at most once. * * Here are some examples (using "==" informally): * * <pre> * 0 == valRange (new double[] { -7 }) * 10 == valRange (new double[]...
import java.util.Arrays; import java.util.Random; import java.util.Scanner; /** * TODO Write a summary of the role of this class in the * MasterMind program. * * @author TODO add your name here */ public class MasterMind { /** * Prompts the user for a value by displaying prompt. * Note: This method should not add a new line to the output of prompt. * * After prompting the user, the method will consume an entire * line of input while reading...
How do I take a random char element from an existing array and assign it to a new array? I have this code with a testing method but my code only returns an empty array instead of the chars expected. public static char[] generateHiddenCode(Random rand, int numPositions, char[] symbols) { char[] arrCode = new char[numPositions]; for (int i=0; i<arrCode.length; ++i) { arrCode[i] = (char) rand.nextInt(symbols.length); } return arrCode; } Test: private static void testGenerateHiddenCode() { boolean error = false;...
JAVA Help Please. Thank you! /** * deDupAndReverse returns a new array containing the unique values in the * original array reversed. 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...
*Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...
Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * Provides an implementation of a binary search tree * with no balance constraints, implemented with linked nodes. * * * */ public class Bst<T extends Comparable<T>> implements Iterable<T> { ////////////////////////////////////////////////////////////////// // I M P L E M E N T T H E M I N M E T H O D B E L O W...
Can I get help with implementing a quick sort in my program? Starter Code: import java.util.Random; import java.util.Scanner; import java.util.Arrays; import java.util.Collections; import java.util.ArrayList; public class RQS { public static int[] prepareData(int[] patients){ /* Data is prepared by inserting random values between 1 and 1000. Data items may be assumed to be unique. Please refer to lab spec for the problem definiton. */ // what is our range? int max = 1000; // create instance of Random class Random randomNum...
Do anyone knows how to write this in java? You are allowed to use the following methods from the Java API: class String length charAt class StringBuilder length charAt append toString class Character any method moveXDownLeft takes a char and a two dimensional array of char as input and returns nothing: The method should find the first occurrence of the char in the array (searching from "top" to "bottom" and "left" to "right"), and it should slide that character...