Select Page

COMP SCI 200 University of Wisconsin Test Cases for getValidInt Java Program

Question Description

I’m working on a Java project and need support to help me study.

I need help to finish this program. I got most of it done I just need help with the last improvements and test case implementations. The code is on 2 seperate files below and the instructions are on an image doc below and within the code itself. Thank you.

InputMethods.java

import java.util.Collections;
import java.util.Scanner;
import java.util.ArrayList;

/**
* Some methods to get user input and filter an ArrayList.
*
*/
public class InputMethods {

/**
* This method returns an integer between min and max, inclusive of both, when that
* integer is the first token on an input line. Otherwise it will keep printing the prompt
* and getting another line of input. If min is greater than max then return -2.
*
* @param input A Scanner instance, if null this returns -1.
* @param prompt The string to print to the user asking for input.
* @param min The minimum acceptable integer.
* @param max The maximum acceptable integer.
* @return A value between min and max, inclusive, or return -1 if input is null or
* return -2 if min is greater than max.
*/
public static int getValidInt(Scanner input, String prompt, int min, int max) {
boolean haveValidNumber = false;
int value = 0;
do {
System.out.print(prompt);
if (input.hasNextInt()) {
value = input.nextInt();
if ( value > min || value < max) {
haveValidNumber = true;
}
}

if ( !haveValidNumber) {
System.out.println(“Error, not a valid number.”);
}
} while (!haveValidNumber);
return value;
}

/**
* This method filters an ArrayList to make sure the data is clean and correct.
* This returns a new list that contains the elements from list in sorted order
* with all the null values and duplicate values removed.
*
* If list is null then return an empty list. If the list contains
* null values then they are all removed. This method uses
* Collections.sort(ArrayList<Integer>) to order the ArrayList. This method
* will not change the list passed in the parameter.
*
* @param list A list of Integers, may contain null and duplicate values.
* @return A list in sorted order, without duplicates or null values.
*/
public static ArrayList<Integer> removeDuplicates(ArrayList<Integer> list) {
ArrayList<Integer> newList = new ArrayList<>();
Collections.sort(list);
for ( int i = 1; i < list.size()-1; i++) {
if ( list.get(i).equals( list.get(i+1))) {
list.remove(i);
}
}
return list;
}
}

TestInputMethods.java

import java.util.ArrayList;
import java.util.Scanner;

/**
* This contains testing methods for the InputMethods class.
*/
public class TestInputMethods {

/**
* This main method runs the selected tests. Comment out a test if you don’t want it to run.
*
* @param args unused
*/
public static void main(String[]args) {
testGetValidInt();
testRemoveDuplicates();
}

/**
* Tests that the getValidInt method handles all the cases described in its method header comment.
*/
private static void testGetValidInt() {
boolean error = false;

{ //test 1, a straightforward “happy path” case “9n” with min 5 and max 10, should result in 9
int expected = 9;
Scanner input = new Scanner(“9n”);
int actual = InputMethods.getValidInt(input, “Enter a number between 5 and 10:”, 5, 10);
if (actual != expected) {
error = true;
System.out.println(“testGetValidInt 1) Error, expected: ” + expected + “, actual: ” + actual);
}
}

{ //test 2, null scanner.

}

{ //test 3, min > max

}

{ //test 4, min value check, “5n” with min 5 and max 10.
int expected4 = 5;
Scanner input4 = new Scanner(“5n”);
int actual4 = InputMethods.getValidInt(input4, “Enter a number between 5 and 10:”, 5, 10);
if (actual4 != expected4) {
error = true;
System.out.println(“testGetValidInt 4) Error, expected: ” + expected4 + “, actual: ” + actual4);
}
}

{ //test 5, max value check, “6n” with min 0 and max 6 should result in 6.
int expected5 = 6;
Scanner input5 = new Scanner(“6n”);
int actual5 = InputMethods.getValidInt(input5, “Enter a number between 0 and 6:”, 0, 6);
if (actual5 != expected5) {
error = true;
System.out.println(“testGetValidInt 5) Error, expected: ” + expected5 + “, actual: ” + actual5);
}
}

{ //test 6, skip multiple lines

}

if (error) {
System.out.println(“testGetValidInt failed”);
} else {
System.out.println(“testGetValidInt passed”);
}
}

/**
* Tests that the removeDuplicates method handles all the cases described in its method header comment.
*/
private static void testRemoveDuplicates() {
boolean error = false;

{ //test 1, a simple case
ArrayList<Integer> list = new ArrayList<>();
list.add(20);
//can also add an array of items to an array list with:
//list.addAll(java.util.Arrays.asList( new Integer[]{20}));

ArrayList<Integer> expected = new ArrayList<>();
expected.add(20);
ArrayList<Integer> actual = InputMethods.removeDuplicates(list);
if (!actual.equals(expected)) {
error = true;
System.out.println(“testRemoveDuplicates 1) Error, expected: ” + expected + ” , actual: ” + actual);
}
}

{ //test 2, null list

}

{ //test 3, null values in arraylist

}

{ //test 4, duplicate values and values out of order

}

{ //test 5, verify input list is unchanged

}

if (error) {
System.out.println(“testRemoveDuplicates failed”);
} else {
System.out.println(“testRemoveDuplicates passed”);
}
}
}

"Place your order now for a similar assignment and have exceptional work written by our team of experts, guaranteeing you "A" results."

Order Solution Now