Programming Project: Arrayfun s1

Total Page:16

File Type:pdf, Size:1020Kb

Programming Project: Arrayfun s1

Programming Project: ArrayFun

Collaboration: Solo with help from classroom/online resources, section leaders, or Rick. Anyone can also share thoughts about any specification and possible algorithms.

This project asks you to solve ten problems requiring arrays. You are asked to write ten methods in one class named ArrayFun and one or more test methods for each of those ten methods in a unit test named ArrayFunTest. Highly recommended: Code and test one method at a time.

______1) public int numberOfPairs(String[] array) Return the number of times a pair occurs in array. A pair is any two String values that are equal (case sensitive) in consecutive array elements. The array may be empty or have only one element. In both of these cases, return 0. (Note: these examples of behavior are written like the CodingBat problems) numberOfPairs( {"a", "b", "c" } ) returns 0 numberOfPairs( {"a", "a", "a"} ) returns 2 numberOfPairs( {"a", "a", "b", "b" } ) returns 2 numberOfPairs( { } ) returns 0 numberOfPairs( {"a"} ) returns 0

The following assertions must pass. These @Test methods must be in ArrayFunTest.java. So feel free to start with this unit test (you will need to create the new class ArrayFun to hold the ten methods).

/** * This class has many @Test methods for testing the ten required * methods that must be in a file named ArrayFun.java. * * @author YOUR NAME */ import static org.junit.Assert.*; import java.util.Scanner; import org.junit.Test; public class ArrayFunTest {

// If you include this variable myFun, you can use myFun in all @Tests // to save repeating this construction in every @Test method private ArrayFun myFun = new ArrayFun();

@Test // If an error exists, hover over @Test, "Add Junit 4 library … " public void testFailedNumberOfPairsWhenArrayIsEmpty() { String[] strs = { "one" }; assertEquals(0, myFun.numberOfPairs(strs)); String[] strs1 = { "one", "two" }; assertEquals(0, myFun.numberOfPairs(strs1)); String[] strs2 = { "one", "one", "one" }; assertEquals(2, myFun.numberOfPairs(strs2)); }

// Add many, many more @Test methods with assertions here

// Implement ONE method at a time in ArrayFun.java } ______2) public int numberOfVowels(char[] array) Given a filled array of char array elements, return the number of vowels which could be the letters 'a' , 'e', 'i', 'o', or 'u' in either upper case or lower case. If the array is empty as in char[] chars = new char[0];, return 0. numberOfVowels( {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U', 'x', 'z'} ) returns 10 numberOfVowels( {'y', 'Y' } ) returns 0 numberOfVowels( {'a', 'X', 'a' } ) returns 2 numberOfVowels( { } ) returns 0

______3) public boolean sumGreaterThan(double[] array, double sum) Given a filled array of double array elements, return true if the sum of all array elements is greater than sum. Return false if the array is empty. Sum may be negative. sumGreaterThan( { 1.1, 2.2, 3.3 }, 4.0) returns true sumGreaterThan( { 1.1, 2.2, 3.3 }, 6.6) returns false sumGreaterThan( { -1.1, -2.2}, -5.0) returns true sumGreaterThan( { }, -1.0) returns false

______4) // Precondition: scanner has valid doubles and there is at least one // Note: this is a new method from Spring 11 public double[] stats(Scanner scanner)

Given a Scanner of double values, return an array of capacity three that has the maximum value in the Scanner as the value in result[0], the minimum value as the second value in result[1], and the average as the third value in result[2]. The following assertions must pass:

@Test public void testStats() { Scanner scanner = new Scanner("90.0 80.0 70.0 68.0");

double[] result = myFun.stats(scanner);

assertEquals(3, result.length); // The capacity is always 3 assertEquals(90.0, result[0], 0.1); // The maximum is at index 0 assertEquals(68.0, result[1], 0.1); // The minimum is at index 1 assertEquals(77.0, result[2], 0.1); // The average is at index 2 }

You may assume the Scanner contains only valid double literals and there is at least one double in the argument. Do not test for empty arrays (Rick doesn't). ______5) // Precondition: scanner has only valid ints in the range of 0..10 // Note: this is a new method from Spring 11 public int[] frequency(Scanner scanner)

Given a Scanner constructed with a String containing a stream of integers in the range of 0..10 (like quiz scores), return an array of 11 integers where the first value (at index 0) is the number of 0s in the Scanner, the second value (at index 1) is the number of ones on the Scanner, and the 11th value (at index 10) is the number of tens in the Scanner. The following assertions must pass.

@Test public void testFrequencyWithOneZeroTwoOnesTwoTwosTwoFiveAndNoThreesOrFours() { Scanner scanner = new Scanner("5 0 1 2 1 5 2");

int[] result = myFun.frequency(scanner); assertEquals(11, result.length);

assertEquals(1, result[0]); // There was 1 zero assertEquals(2, result[1]); assertEquals(2, result[2]); assertEquals(0, result[3]); assertEquals(0, result[4]); assertEquals(2, result[5]); // There were 2 5s for(int score = 6; score <= 10; score++) { assertEquals(0, result[score]); // There were zero 6s, 7s, 8s, 9s, 10s } }

Assume the Scanner only has valid integers and they are in the range of 0 through 10 only.

______6) public int howMany(String[] array, String valueToFind) Complete method howMany to return the number of elements in an array of Strings that equals valueToFind. The array may be empty. howMany( {"A", "a", "A", "a" }, "A" ) returns 2 howMany( {"And", "there", "goes", "another"}, "another" ) returns 1 howMany( {"And", "there", "goes", "another"}, "Not Here" ) returns 0

______7) public void sortOfSort(int[] array) Write method sortOfSort that modifies the parameter array to place the largest integer at index n-1 and the smallest integer at array[0]. The others elements must still be in the array, but not in any particular order. You must modify the given array argument by changing array in method sortOfSort. Original Array Modified Array (all elements except the first and last may differ in order) { 4, 3, 2, 0, 1, 2 } { 0, 3, 2, 1, 2, 4 } { 4, 3, 2, 1 } { 1, 3, 2, 4 } { 1, 3, 2, 4 } { 1, 3, 2, 4 } The following assertions must pass:

@Test public void testSortOfSort() { int[] x = { 4, 3, 2, 1 }; myFun.sortOfSort(x); assertEquals(1, x[0]); assertEquals(4, x[3]); assertTrue(x[1] == 2 || x[1] == 3); assertTrue(x[2] == 2 || x[2] == 3); }

Tip: Get the min at index 0 before you put the max in the last index

______8) public void evensLeft(int[] array)

(A Nick Parlante JavaBat problem) Modify the parameter array so it still contains the exact same numbers as the given array, but rearranged so that all the even numbers come before all the odd numbers. Other than that, the numbers can be in any order. You must modify the array arguments by changing array in method evensLeft. The array may be empty or have only one element. In both cases, no change should be made.

Original Array Modified Array {1, 0, 1, 0, 0, 1, 1 } { 0, 0, 0, 1, 1, 1, 1 } {3, 3, 2} { 2, 3, 3 } {2, 2, 2} { 2, 2, 2 }

The following assertions must pass:

@Test public void testEvensLeft() { int[] a = { 1, 2, 3, 4, 5, 6, 7 }; myFun.evensLeft(a); // left side of the array at indexes 0..2 is even for (int i = 0; i <= 2; i++) assertTrue(a[i] % 2 == 0); // right side of the array at indexes 3..6 is odd for (int i = 3; i <= 6; i++) assertTrue(a[i] % 2 != 0); }

______9) public void shiftNTimes(int[] array, int numShifts)

Modify array so it is "left shifted" n times -- so shiftNTimes({6, 2, 5, 3}, 1) changes the array argument to {2, 5, 3, 6} and shiftNTimes({6, 2, 5, 3}, 2) changes the array argument to {5, 3, 6, 2}. You must modify the array argument by changing the parameter array inside method shiftNTimes. Remember, a change to the parameter inside the method shiftNTimes changes the argument inside the @Test method. shiftNTimes( { 1, 2, 3, 4, 5, 6, 7 }, 3 ) modifies array to { 4, 5, 6, 7, 1, 2, 3 } shiftNTimes( { 1, 2, 3, 4, 5, 6, 7 }, 0 ) does not modify the array, it remains { 1, 2, 3, 4, 5, 6, 7 } shiftNTimes( { 1, 2, 3 }, 5) modifies array to { 3, 1, 2 } shiftNTimes( { 3 }, 5) modifies array to { 3 }

The following assertions must pass.

@Test public void testFailedShiftNTimesWhenNIsTwo() { int[] a = { 1, 2, 3 }; myFun.shiftNTimes(a, 2); // Make a have this order: {3, 1, 2} assertEquals(3, a[0]); assertEquals(1, a[1]); assertEquals(2, a[2]); }

______10) public char[] replaced(char[] array, char oldChar, char newChar)

Return a new char[] that is a copy of array with all occurrences of oldChar replaced by newChar -- so replaced ({'A', 'B', 'C', 'D', 'B'}, 'B', '+') returns a new array {'A', '+', 'C', 'D', '+'}. The original array argument must remain unchanged. The returned array must have exactly the same capacity as the array reference passed to the parameter referenced as array. replaced( { 'A', 'B', 'C', 'D', 'B'}, 'C', 'L' ) returns { 'A', 'B', 'L', 'D', 'B' } replaced( { 'n', 'n', 'n', 'D', 'n'}, 'n', 'T' ) returns { 'T', 'T', 'T', 'D', 'T' } replaced( { 'n', 'n', 'n' }, 'T', 'n' ) returns { 'n', 'n', 'n' }

The following assertions must pass

@Test public void testFailedReplacedWhileCheckingOriginalArrayIsUnchanged() { char[] a = { 'a', 'b' }; char[] result = myFun.replaced(a, 'a', 'X'); assertEquals(2, result.length); assertEquals('X', result[0]); assertEquals('b', result[1]); // Ensure the original array has not changed assertEquals('a', a[0]); assertEquals('b', a[1]); }

Grading Criteria (100pts) When you have completely tested all 10 methods, turn in your project to WebCat. You will be graded as follows ____+100 Web-Cat correctness and code coverage. · Your code must compile using the specified names o All method names must exactly match the 10 methods headings above o These methods must be in a file named ArrayFun.java. No packages. · You must include ArrayFunTest.java and all of the assertions therein must pass on WebCat · You must have tested all of your methods with assertions in @Test methods · All of Rick's tests on WebCat pass · The final WebCat submission will be the one that is graded unless you inform us · You must not have any infinite loops o You will see a timeout error and receive 0 points

Recommended publications