Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Task 3. Using JUnit Download both the ArrayLab.java file and the ArrayLabTester.java file. Load them in DrJava and compile them. The ArrayLab.java file has a

Task 3. Using JUnit

Download both the ArrayLab.java file and the ArrayLabTester.java file. Load them in DrJava and compile them.

The ArrayLab.java file has a bunch of methods that do practically nothing. Your task in this lab is to get as many of these methods working as you can.

The ArrayLabTester.java file has methods to test the methods of ArrayLab.java using the JUnit testing package. These methods have been created for you in this lab. You can run these tests by clicking the Test button at the top of DrJava. Try that now.

You will notice at the bottom of DrJava, the Test Output tab is shown, the Test Progress bar is red (it will be green when all tests pass), and in the text area, the name of each test is red. This means that the specific tests failed. The names will turn green when the tests pass. Underneath the list of names will be descriptions of what failed in each test. If you click on the error, it will take you to the test line that failed. It will also list what the expected value was for the test and what value your method produced. Below the failure descriptions will be a listing, if any, of exceptions that were thrown by the tests.

If you look at the first error listed in the test cases:

File: .../ArrayLabTester.java [line:16] Failure: junit.framework.AssertionFailedError: Incorrectly claims 11 is not in a[0..1] 

the message states that the method gives the wrong return value. If you click on the message, it will take you to the specific test that was done, namely ArrayLab.contains(a, 0, 1, 11) where a is the array {11, 13}.

The JUnit error message will tell you which test of JUnit failed, it will not tell you where in your program the error is. If you want a "standard" Java error message, copy the specific JUnit test into the interactions pane and run it there.

Task 4. Writing and Testing Your Code

First, place you name and your lab partner's name at the top of the ArrayLab.java file.

Step 1: Write the contains method and test it using the Test button until the test for contains is green.

You are not to modify the ArrayLabTester.java test cases for the contains method.

The comment above each method explains what the method should do. The Precondition is a statement that describes what you can assume will be true about the parameters to the method. The Postcondition statement lists something that, if your code is correct, will be true when the method returns. The "Postcondition" can also be thought of as the goal for the method.

Switch roles, the driver should navigate and the navigator should drive.

Step 2: Write the arrayToString method and test it using the Test button. Note that the last test of this JUnit test case will always fail. When you get to that point, you need to create an appropriate JUnit test case. Your test case should check that the method works correctly on an array with more than two elements.

Each JUnit test consists of methods called assertEquals (or variations like assertTrue or assertArrayEquals). The assertEquals method takes three inputs. The first is a String that is a message that should be printed if the test fails. The second is an object that represents the correct result of the test, and the third input is the actual result of the test.

You need to create an appropriate array for testing the arrayToString method on more than two elements. Then replace the fail method (that always fails) with an assertEquals method call. For the first input, give an appropriate message, for the second message give the String that should be produced by calling arrayToString in your test, and the third input should be the String returned by the call to arrayToString.

Demonstrate your method and show your test case to the lab assistant.

Switch roles, the driver should navigate and the navigator should drive.

Step 3: Write the remove method and test it using JUnit.

Switch roles, the driver should navigate and the navigator should drive.

Step 4: Write the insert method and test it using JUnit.

ArrayLab

/** * A collection of methods related to int[]. */ public class ArrayLab {

/** * Return whether k is in array[start..end]. * Precondition: the length of array is at least end+1 * @param array the array of int values. * @param start the index of the start of the range to be searched. * @param end the index of the end of the range to be searched. * @param k the number to search for. * @return true if k is in array[start..end] */ public static boolean contains(int[] array, int start, int end, int k) { return false; }

/** * Create a String that has the contents of list[0..n-1] on one line, * separated by spaces and surrounded by { and }. * @param list the sorted array. * @param n the number of items in list to put in the string. * @return the first n elements of the array, separated by spaces, and surrounded by { and }. */ public static String arrayToString(int[] list, int n) {

ArrayLabTester

import org.junit.*; import static org.junit.Assert.*;

/** * Test class LinkedList */ public class ArrayLabTester { /** * Test the contains method */ @Test public void testContains() { int[] a = new int[] {11, 12, 13}; assertTrue("Incorrectly claims 11 is not in a[0..2]", ArrayLab.contains(a, 0, 2, 11)); // Test many, first assertTrue("Incorrectly claims 12 is not in a[0..2]", ArrayLab.contains(a, 0, 2, 12)); // Test many, middle assertTrue("Incorrectly claims 13 is not in a[0..2]", ArrayLab.contains(a, 0, 2, 13)); // Test many, last assertTrue("Incorrectly claims 11 is not in a[0..0]", ArrayLab.contains(a, 0, 0, 11)); // Test one, element in assertFalse("Incorrectly claims 11 is in a[1..1]", ArrayLab.contains(a, 1, 1, 11)); // Test one, element not in assertFalse("Incorrectly claims 11 is in a[2..1]", ArrayLab.contains(a, 2, 1, 11)); // Test zero } /** * Test the arrayToString method. */ @Test public void testArrayToString() { int[] a = {11, 13};

assertEquals("Printing no elements", "{}", ArrayLab.arrayToString(a, 0)); // Test zero assertEquals("Printing only the first element", "{11}", ArrayLab.arrayToString(a, 1)); // Test one assertEquals("Printing both elements of an array of two", "{11 13}", ArrayLab.arrayToString(a, 2)); // Test two

fail("You need to write a test case"); // Replace this with a test case that covers more than two elements }

/** * Test the remove method */ @Test public void testRemove() { int[] a = {0, 1, 2, 3, 5, 7}; // Test remove last ArrayLab.remove(a, 6, 7); int[] b = {0, 1, 2, 3, 5}; verifyPrefixOf(b, a); ArrayLab.remove(a, 5, 0); // Test remove first b = new int[]{1, 2, 3, 5}; verifyPrefixOf(b, a);

ArrayLab.remove(a, 4, 2); // Test remove middle b = new int[]{1, 3, 5}; verifyPrefixOf(b, a); } /** * Calls JUnit test on each element to verify that the first array is a prefix of * the second array * @param expected the expected prefix of the given array * @param given the expected array should be the prefix of the given array */ private void verifyPrefixOf(int[] expected, int[] given) { for (int i = 0; i < expected.length; i++) assertEquals("On index " + i, expected[i], given[i]); }

/** * Test the insert method */ @Test public void testInsert() { int[] a = {0, 0, 0, 0, 0, 0}; ArrayLab.insert(a, 0, 1); assertArrayEquals("Inserting an element into an empty array", new int[]{1, 0, 0, 0, 0, 0}, a); // Test zero

a[1]= 3; a[2]= 5; ArrayLab.insert(a, 3, 2); assertArrayEquals("Inserting an element that goes in the middle", new int[]{1, 2, 3, 5, 0, 0}, a); // Test insert middle ArrayLab.insert(a, 4, 0); assertArrayEquals("Inserting an element that goes in the front", new int[]{0, 1, 2, 3, 5, 0}, a); // Test insert first ArrayLab.insert(a, 5, 7); assertArrayEquals("Inserting an element that goes at the end", new int[]{0, 1, 2, 3, 5, 7}, a); // Test insert last } }

return null; }

/** * Remove k from list[0..n-1]. * Precondition: list[0..n-1] is sorted, and k is an element of list[0..n-1]. * Postcondition: list[0..n-2] is sorted. * @param list the sorted array. * @param n the number of items in list. * @param k the number to remove from list. */ public static void remove(int[] list, int n, int k) { }

/** * Insert k into list where it belongs. * Precondition: list[0..n-1] is sorted, and list[n] is unused. * Postcondition: list[0..n] is sorted. * @param list the sorted array. * @param n the number of items in list. * @param k the number to add to list. */ public static void insert(int[] list, int n, int k) { } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Bioinformatics Databases And Systems

Authors: Stanley I. Letovsky

1st Edition

1475784058, 978-1475784053

More Books

Students also viewed these Databases questions

Question

2. Are you varying your pitch (to avoid being monotonous)?

Answered: 1 week ago

Question

3. Are you varying your speaking rate and volume?

Answered: 1 week ago