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

1. Start NetBeans and open the project named ch02_ex2_TestScore that\'s in the e

ID: 3548901 • Letter: 1

Question

1. Start NetBeans and open the project named ch02_ex2_TestScore that's in the ex_starts directory shown in the previous exercise.

2. Test this application with the valid data to see how it works. Then, test the application with invalid data to see what will cause exceptions. Note that if you enter a test score like 125, the program ends, even though the instructions say that the program ends when you enter 999.

3. Open the file named TestScoreApp.java and modify the while statement so the program only ends when  you enter 999. Then, test the program to see how this works.

4. Modify the if statement so it displays an error message like "Invalid entry, not counted" if the user enters a score that's greater than 100 but isn't 999. Then, test this change.

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

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

Explanation / Answer

1. Start NetBeans and open the project named ch02_ex2_TestScore that's in the ex_starts directory shown in the previous exercise.

2. Test this application with the valid data to see how it works. Then, test the application with invalid data to see what will cause exceptions.
Note that if you enter a test score like 125, the program ends, even though the instructions say that the program ends when you enter 999.

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

Enter score: 45
Enter score: 67
Enter score: 125

Score count:   2
Score total:   112.0
Average score: 56.0

// YES. PROGRAM ENDS WHEN YOU ENTER MORETHAN 100.
// THIS PROGRAM GIVES AVERAGE of VALID SCORES ENTERED.

3. Open the file named TestScoreApp.java and modify the while statement so the program only ends when you enter 999.
Then, test the program to see how this works.

// MODIFIED CODE
import java.util.Scanner;

public class TestScoreApp
{
    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
        while (testScore != 999)
        {
            // get the input from the user
            System.out.print("Enter score: ");
            testScore = sc.nextInt();

            // accumulate score count and score total
            if (testScore <= 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);
    }
}

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

Enter score: 45
Enter score: 67
Enter score: 125
Enter score: 999

Score count:   2
Score total:   112.0
Average score: 56.0

// THIS PROGRAM CALCULATES AVERAGE OF VALID SCORES THAT ARE IN RANGE 0 to 100 and PROGRAM ENDS ONLY WHEN YOU ENTER 999.

4. Modify the if statement so it displays an error message like "Invalid entry, not counted" if the user enters a score that's greater than 100 but isn't 999.
Then, test this change.

// MODIFIED CODE
import java.util.Scanner;

public class TestScoreApp
{
    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
        while (testScore != 999)
        {
            // get the input from the user
            System.out.print("Enter score: ");
            testScore = sc.nextInt();

            // accumulate score count and score total
            if (testScore <= 100)
            {
                scoreCount = scoreCount + 1;
                scoreTotal = scoreTotal + testScore;
            }
            else if(testScore!=999)
            {
                // PRINT ERROR.
                System.out.println("Invalid entry, not counted");
            }
        }

        // 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);
    }
}

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

Enter score: 45
Enter score: 67
Enter score: 125
Invalid entry, not counted
Enter score: 999

Score count:   2
Score total:   112.0
Average score: 56.0

// NOW PROGRAM PRINTS ERROR MESSAGE WHEN SCORE ENTERED IS MORE THAN 100 but not 999
// and when entered score is 999. PROGRAM TERMINATES.
// THIS PROGRAM CALCULATES AVERAGE OF VALID SCORES THAT ARE IN RANGE 0 to 100 and PROGRAM ENDS ONLY WHEN YOU ENTER 999.