9
Assignment: Four Array Methods Implement a Unit Test and 4 methods with Array Parameters This assignment asks you to write four methods in class Array4Fun and test methods for both in class Array4FunTest. First, a Note about Rick's Reference Tests @Test public void testFailedShiftNTimesWhenNIsTwo() { Array4Fun af = new Array4Fun(); int[] a = { 1, 2, 3 }; af.shiftNTimes(a, 2); assertEquals(3, a[0]); assertEquals(1, a[1]); assertEquals(2, a[2]); } If any one of the above three assertions fails, WebCat will provide a hint under Problem Coverage. The hint gets rendered as the method heading parsed so each new word in the hint occurs at each upper case letter in that method heading (test is ignored and the remaining hint is lower case). Therefore, if any assertion from this method fails, the method heading to the left would be rendered as the WebCat hint to the right: public void testFailedShiftNTimesWhenNIsTwo() failed shift n times when n is two These hints are not intended to give you a solution, but rather to give you hints about what Rick was testing when your code did not meet the expected behavior in the specification of each of the ten methods that follow.

Assignment: Four Array Methodsmercer/Assignments/Array4Fun.pdf · Assignment: Four Array Methods Implement a Unit Test and 4 methods with Array Parameters This assignment asks you

  • Upload
    others

  • View
    20

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Assignment: Four Array Methodsmercer/Assignments/Array4Fun.pdf · Assignment: Four Array Methods Implement a Unit Test and 4 methods with Array Parameters This assignment asks you

Assignment: Four Array Methods Implement a Unit Test and 4 methods with Array Parameters

This assignment asks you to write four methods in class Array4Fun and test methods for both in class Array4FunTest. First, a Note about Rick's Reference Tests

@Test public void testFailedShiftNTimesWhenNIsTwo() { Array4Fun af = new Array4Fun(); int[] a = { 1, 2, 3 }; af.shiftNTimes(a, 2); assertEquals(3, a[0]); assertEquals(1, a[1]); assertEquals(2, a[2]); }

If any one of the above three assertions fails, WebCat will provide a hint under Problem Coverage. The hint gets rendered as the method heading parsed so each new word in the hint occurs at each upper case letter in that method heading (test is ignored and the remaining hint is lower case). Therefore, if any assertion from this method fails, the method heading to the left would be rendered as the WebCat hint to the right:

public void testFailedShiftNTimesWhenNIsTwo() failed shift n times when n is two

These hints are not intended to give you a solution, but rather to give you hints about what Rick was testing when your code did not meet the expected behavior in the specification of each of the ten methods that follow.

Page 2: Assignment: Four Array Methodsmercer/Assignments/Array4Fun.pdf · Assignment: Four Array Methods Implement a Unit Test and 4 methods with Array Parameters This assignment asks you

What do you think happened with the following WebCat Submissions?

Page 3: Assignment: Four Array Methodsmercer/Assignments/Array4Fun.pdf · Assignment: Four Array Methods Implement a Unit Test and 4 methods with Array Parameters This assignment asks you

This looks GREEN! Yeah, but it is 0 points! Do you see 0.0/100.0?

Page 4: Assignment: Four Array Methodsmercer/Assignments/Array4Fun.pdf · Assignment: Four Array Methods Implement a Unit Test and 4 methods with Array Parameters This assignment asks you

Scroll Down a bit to see a method is named incorrectly

Page 5: Assignment: Four Array Methodsmercer/Assignments/Array4Fun.pdf · Assignment: Four Array Methods Implement a Unit Test and 4 methods with Array Parameters This assignment asks you

Now start a project named Array4Fun____________________________________________________________________________________

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⎯return 0 in both cases.

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

@Test public void testFailedNumberOfPairsWhenArrayIsEmptyOrSizeOne() { Array4Fun af = new Array4Fun(); String[] s0 = {}; assertEquals(0, af.numberOfPairs(s0)); String[] s1 = { "solo" }; assertEquals(0, af.numberOfPairs(s1)); }

@Test public void testFailedNumberOfPairsWhenOnlyTwoElemnts() { Array4Fun af = new Array4Fun();

String[] s0 = { "one", "two" }; assertEquals(0, af.numberOfPairs(s));

String[] s1 = { "one", "one" }; assertEquals(1, af.numberOfPairs(s)); }

Page 6: Assignment: Four Array Methodsmercer/Assignments/Array4Fun.pdf · Assignment: Four Array Methods Implement a Unit Test and 4 methods with Array Parameters This assignment asks you

________________________________________________________________________________________

2) 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. Precondition: The array may be empty. If so, return false. 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.0) returns false

@Test public void testFailedSumGreaterThanWhenArrayHasOnlyOneElement() { double[] x2 = { 2.2 }; assertTrue(studentsFun.sumGreaterThan(x2, 2.1)); assertFalse(studentsFun.sumGreaterThan(x2, 2.2)); assertFalse(studentsFun.sumGreaterThan(x2, 5.501)); }

__________________________________________________________________________________

3) 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 }

Page 7: Assignment: Four Array Methodsmercer/Assignments/Array4Fun.pdf · Assignment: Four Array Methods Implement a Unit Test and 4 methods with Array Parameters This assignment asks you

_________________________________________________________________________________

4) 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.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 }

Hint: To test, make sure the left side of the array has even integers and the right side of the array has odd integers. It may be that the evens or the odds can be in any order depending on the algorithm you use.

Submitting to WebCatYou will be graded on a scale of 0.0 through 10.0 automatically by WebCat. Here are the instructions for getting Submitting your first project to the WebCat automated feedback/grading tool:

Change your WebCat password• Read your email to get for your Web-Cat account ID (almost always your UofA NetID) and the randomly

generated password • Login to WebCat at this url: https://web-cat.cs.vt.edu/Web-CAT/WebObjects/Web-CAT.woa • Change your password to a good password no one else can guess.

Page 8: Assignment: Four Array Methodsmercer/Assignments/Array4Fun.pdf · Assignment: Four Array Methods Implement a Unit Test and 4 methods with Array Parameters This assignment asks you

Set up your preferences in the lab and/or on your personal machine • From Eclipse, select Window > Preferences > Electronic Submission• Using your own Default username and your own email address, fill in the four fields using this url for the

Assignment definition URL (Recommended: Copy and paste this long URL into the URL field):

https://web-cat.cs.vt.edu/Web-CAT/WebObjects/Web-CAT.woa/wa/assignments/eclipse?institution=Arizona

Page 9: Assignment: Four Array Methodsmercer/Assignments/Array4Fun.pdf · Assignment: Four Array Methods Implement a Unit Test and 4 methods with Array Parameters This assignment asks you

Submit your project to WebCat• Right click on your project name• Select the Submit button (near the bottom of the list of options). WebCat return a list of published projects• Click on the appropriate project• Make sure UserName has your WebCat user name (which may be your UofA NetID)• Enter your WebCat password

• Click Next and Finish• Click view your graded results and wait until the report is posted back to you• Wait until WebCat posts results back to the Eclipse browser--could take 15-45 seconds• If you get 100%, Congratulations! If not, read all hints from WebCat and try again.