Page 1 of 1

This project contains 2 classes in separate files, GuessTheNumber and TestGuessTheNumber. GuessTheNumber contains a meth

Posted: Tue Jul 12, 2022 8:21 am
by answerhappygod
This project contains 2 classes in separate files,GuessTheNumber and TestGuessTheNumber.GuessTheNumber contains a method, RenameMe (poor name that shouldbe changed), that prompts a user for a value and then does a numberof checks. The method returns a negative number indicating aspecific error or, if the entered number is valid, returns thenumber the user entered. The main method contains a small programthat calls the RenameMe method to demonstrate its use. The mainmethod can be assumed to be complete and correct and won't begraded.
TestGuessTheNumber contains a method with test cases,testRenameMe, to check that the RenameMe method works correctly.Within the testRenameMe method, create a set of test cases todemonstrate that the correct value (valid value or error code) isreturned in all cases
Set up an GuessTheNumber project
Style (format, name, and comment)
Write test cases
In the class TestGuessTheNumber, within the testRenameMe methodwrite test cases for the RenameMe method that demonstrate a validvalue returned as well as verifying that the correct negativenumber is returned for each circumstance.
Note: Test cases should run without user interaction.Don't use System.in in test cases. Pass a string containing what auser would type into the scanner as demonstrated in the exampletest case.
Example test case:
Other test cases include verifying that the correct negativenumber is returned for the appropriate circumstance. Compile andrun the test cases to verify that the method still workscorrectly.
GIVEN CODE:
GuessTheNumber
import java.util.Random;import java.util.Scanner;
public class GuessTheNumber {
public static int RenameMe(Scanner A, String B,int C, int D) {if (A == null) {return -1;} else if (C > D) {return -2;} else if(C < 0) {return -3;}int E = -6;System.out.println(B);if (A.hasNextInt()) {E = A.nextInt();A.nextLine(); //read and ignore the rest of the lineif (E < C) {return -4;} else if (E > D) {return -5;}} else {A.nextLine(); //read and ignore the token that isn't aninteger}return E; }
/** * This is a guess the number game that uses themethod above for user input. * @param args unused */ public static void main(String []args) { Random rand = new Random(); Scanner input = newScanner(System.in); final int LOW_BOUND = 1; final int HIGH_BOUND = 100; int hiddenNum = rand.nextInt(HIGH_BOUND- LOW_BOUND +1) + LOW_BOUND; int guessCount = 0; int guessedNum;
do { guessedNum =RenameMe(input, "Enter a number between " + LOW_BOUND + " and " + HIGH_BOUND+ ": ", LOW_BOUND, HIGH_BOUND); guessCount++; if ( guessedNum >=LOW_BOUND && guessedNum <= HIGH_BOUND) { if(guessedNum < hiddenNum) { System.out.println("Your guess is too low."); } else{ System.out.println("Your guess is too high."); } } else { System.out.println("Your guess is invalid."); } } while (guessedNum != hiddenNum);
System.out.print("You won in " +guessCount + " attempts!"); input.close(); }}
TestGuessTheNumber
//TODO need a file header
import java.util.Scanner;
/** * This contains testing methods for the InputMethodsclass. * * @author Jim Williams * @author //TODO add your name here when youcontribute. */public class TestGuessTheNumber {
/** * 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) { testRenameMe(); }
/** * Tests that the RenameMe method handles allthe cases described in its method header * comment. */ private static void testRenameMe() { boolean error = false;
//test 1, a happy path case (validinput) "25\n" with min 2 and max 50 should result in // 25 being returned. { //creates a new scope so that thevariables declared don't affect others in // this same method.
//simulates a usertyping 25 then Enter. Scanner input = newScanner("25\n");
//the value expectedto be returned by the method for this test case. int expected = 25; int actual =GuessTheNumber.RenameMe(input, "Enter a number between 2 and50:", 2, 50);
//if the actual valuereturned doesn't equal the expected then this is an error. if (actual != expected){ error =true; System.out.println("testRenameMe 1) Expected " + expected + ", avalid value to " + "be returned instead of " + actual); } }
{ //test 2, -1 is returned inthe appropriate circumstance which should be described int expected = -1;
}
{ //test 3, -2 is returned in theappropriate circumstance which should be described int expected = -2;
}
{ //test 4, -3 is returned in theappropriate circumstance which should be described int expected = -3;
}
{ //test 5, -4 is returned in theappropriate circumstance which should be described int expected = -4;
}
{ //test 6, -5 is returned in theappropriate circumstance which should be described int expected = -5;
}
{ //test 6, -6 is returned in theappropriate circumstance which should be described int expected = -6;
}
if (error) { System.out.println("\ntestRenameMe failed"); } else { System.out.println("\ntestRenameMe passed; it is expected that theprompt is output"); } }}