University of Arizona



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 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download