26.7k views
3 votes
Exercise 2-2 Modify the Test Score application In this exercise, you'll modify the Test Score application. This should give you a chance to write some code of your own. 1. Open the project named ch02_ex2_TestScore that's in the ex_starts directory shown in the previous exercise. 2. Test this application with valid data to see how it works. Then, test the application with invalid data. You should be able to crash the program at least once. 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 TestScore App.java and modify the while statement so the program only ends when you enter 999. Then, test the program to see how this works. Section / Essential Java skills 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. 5. Run the application again, enter 999 as the first score, and note what is displayed for the average score. Then, modify the code so it sets the initial value of average score to 0.0 and so it only calculates the average score if the value of the scoreCount variable is greater than zero. Test this change.

1 Answer

3 votes

The code to use for the changes for the Test Score application is :

Change the while statement in the main method to only end when the user enters 999:

Java

while (score != 999) {

if (score > 100 || score < 0) {

System.out.println("Invalid entry; not counted");

} else {

scores[scoreCount++] = score;

}

System.out.print("Enter next test score (enter 999 to quit): ");

score = scanner.nextInt();

}

2. to add an error message if the user enters a score greater than 100 but not 999 use the code below

Java

if (score > 100 && score != 999) {

System.out.println("Invalid entry; not counted");

} else {

scores[scoreCount++] = score;

}

User OneChillDude
by
9.5k points

No related questions found