Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NEED THE JAVA CODE 1. Open the project named ch05_ex2_TestScore in the ex_starts

ID: 3549448 • Letter: N

Question

NEED THE JAVA CODE


1. Open the project named ch05_ex2_TestScore in the ex_starts directory. Then, run the application to see how it works. Note that it crashes if you enter an invalid integer for a score or if you enter "y" followed by another value at the prompt that asks if you want to enter another score. Note also that it allows invalid scores such as 150.

2. Open the ch05_FutureValueValidation project in the book_apps directory. Then, copy the generic getInt and getIntWithinRange methods from that application and past them into the TestScoreApp class.

3. Use the getInt and getIntWithinRange methods to validate that each score ranges from 1 through 100. Then, test this enhancement.

4. Add code that discards any extra entries at the prompt that asks if you want to enter another score. Then, test the appliation to make sure that it is bulletproof.


------



                    You need the source code to the Murach's Java Programming book found here:                 

                    http://www.murach.com/downloads/javp.htm                 




Explanation / Answer

/* OUTPUT


Please enter test scores that range from 0 to 100.
To end the program enter 999.

Enter score: -23
Error! Number must be greater than 1.
Enter score: 23
Enter score: 124
Error! Number must be less than 100.
Enter score: 97
Enter score: 12
Enter score: 13
Enter score: 14

Score count:   5
Score total:   159.0
Average score: 31.8

*/



import java.util.Scanner;

public class TestScoreApp
{
   public static int getInt(Scanner sc, String prompt)
    {
        int i = 0;
        boolean isValid = false;
        while (isValid == false)
        {
            System.out.print(prompt);
            if (sc.hasNextInt())
            {
                i = sc.nextInt();
                isValid = true;
            }
            else
            {
                System.out.println("Error! Invalid integer value. Try again.");
            }
            sc.nextLine(); // discard any other data entered on the line
        }
        return i;
    }

    public static int getIntWithinRange(Scanner sc, String prompt, int min, int max)
    {
        int i = 0;
        boolean isValid = false;
        while (isValid == false)
        {
            i = getInt(sc, prompt);
            if (i <= min)
                System.out.println(
                    "Error! Number must be greater than " + min + ".");
            else if (i >= max)
                System.out.println(
                    "Error! Number must be less than " + max + ".");
            else
                isValid = true;
        }
        return i;
    }
   
    public static void main(String[] args)
    {
        // display operational messages
        System.out.println("Please enter test scores that range from 0 to 100.");
        System.out.println("To end the program enter 999.");
        System.out.println(); // print a blank line

        // initialize variables and create a Scanner object
        double scoreTotal = 0;
        int scoreCount = 0;
        int testScore = 0;
        Scanner sc = new Scanner(System.in);

        // get a series of test scores from the user
        for(int i=0; i<5; i++)
        {
            // get the input from the user
            testScore = getIntWithinRange(sc,"Enter score: ",1,100);

                scoreCount = scoreCount + 1;
                scoreTotal = scoreTotal + testScore;
        }

        // display the score count, score total, and average score
        double averageScore = scoreTotal / scoreCount;
        String message = " " +
                         "Score count:   " + scoreCount + " "
                       + "Score total:   " + scoreTotal + " "
                       + "Average score: " + averageScore + " ";
        System.out.println(message);
    }
}