Question

In: Computer Science

I have to use a sentinel while loop to complete the following task in a java...

I have to use a sentinel while loop to complete the following task in a java program, I want to see how this is executed so I can better understand how the sentinel while loop works. Thank you!

Convert Lab 10 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following:

  • Prompts the user to enter a grade or a -1 to quit.
  • IF the user entered a -1 THEN
    • Display a message that the User is done entering grades
  • ELSE
    • Count each grade as it is entered.
    • Compute a running total of the grades entered.
  • END IF
  • After the user enters the sentinel of -1, calculate the average of the grades entered.
  • When computing the average, make sure that there is no division by zero. Check to make sure the counter is not equal to zero.
  • Make sure the sentinel is not counted as a grade.
  • Print out a message stating the average of the grades entered.

Include the following:

  1. Use a sentinel-controlled WHILE loop that uses a -1 as the sentinel to exit the loop.
    • Before counting the grades, initialize the counter to zero before the while loop begins.
  2. While within the body of the ELSE:
    • Within the WHILE loop:
      • Prompt the User to enter a grade or a -1 to quit.
      • IF the input is not a -1 THEN
        • Count the grade.
        • Compute a running total of these grades as they are entered.
      • END IF
    • After the WHILE Loop is done executing, compute the average of all the grades entered. Remember to check to make sure you don't perform a division by zero.

Solutions

Expert Solution

JAVA CODE

import java.util.Scanner; // import Scanner class

// implement driver class GradeCheck
class GradeCheck
{
public static void main(String [] args)
{
int grade,cnt=0,sum=0; // declare integer variables grade,cnt,sum
float average; // declare float variable average
Scanner scr=new Scanner(System.in); // create Scanner class object "scr"

   while(true) // create infinite while loop
   {
       System.out.print("Enter a grade or a -1 to quit: ");
       grade=scr.nextInt(); // read grades

       if(grade==-1) // check grade=-1
       {
           // print this message
           System.out.println("User is done entering grades...");
           break; // terminate while loop
       }
       else{ // else,
           sum+=grade; // compute sum of grades
           cnt++; // increment counter
       }
   } // end of while loop

   if(cnt!=0) // check counter cnt not =0 then,
   {
       average=sum/(float)cnt; // compute average
       System.out.println("Average of the grades: "+average); // print average
    }
} // main end
} // class end

OUTPUTS:

D:\>javac GradeCheck.java

D:\>java GradeCheck
Enter a grade or a -1 to quit: 2
Enter a grade or a -1 to quit: 3
Enter a grade or a -1 to quit: 4
Enter a grade or a -1 to quit: 4
Enter a grade or a -1 to quit: -1
User is done entering grades...
Average of the grades: 3.25

D:\>java GradeCheck
Enter a grade or a -1 to quit: -1
User is done entering grades...

PROGRAM SCREENSHOTS


Related Solutions

Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel...
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of...
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade...
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of the grades entered. When computing the average, make sure that there is...
Use a sentinel while loop that repeatedly prompts the user to enter a number, once -1...
Use a sentinel while loop that repeatedly prompts the user to enter a number, once -1 is entered, stop prompting for numbers and display the maximum number entered by the user. I am struggling to have my program print the math function. Here is what I have so far: import java.util.*; public class MaxSentinel { public static void main(String[] args) {    Scanner input = new Scanner(System.in); System.out.println("Please enter a value. Press -1 to stop prompt."); int number = 0;...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a...
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a sentinel value #This while loop ends when the user enters a 1, the sentinel value #Assign the number 8 to a variable which will serve as the sentinel value #Condition: while variable is not equal to 1 #Action: display the number assigned to the variable #Use an input statement (no prompt) to ask the user for a number and assign this number to the...
Trying to complete this in Java. Application called 'Coffee Shop' that uses a while loop to...
Trying to complete this in Java. Application called 'Coffee Shop' that uses a while loop to build a customer order. The Coffee Shops sells: Coffee ($3.25), Espresso ($4.25), and Tea ($2.75). The coffee selection presents the customer with the choices of iced (no charge), cream (50 cents), and sugar (50 cents). The espresso selection presents the customer with choice of caramel (no charge) and chocolate (no charge) with one shot (no charge) or two shots ($1.25) of espresso. Once the...
(This is for java) I need to rewrite this code that uses a while loop. public...
(This is for java) I need to rewrite this code that uses a while loop. public class Practice6 {      public static void main (String [] args) {         int sum = 2, i=2;        do { sum *= 6;    i++;    } while (i < 20); System.out.println("Total is: " + sum); }
Create a python program that contains a while loop together with a Sentinel (0) to process...
Create a python program that contains a while loop together with a Sentinel (0) to process indefinite item costs that are purchased online from a vendor. Be sure that you assign a variable SENTINEL to 0 to use in the Boolean condition of your while loop. A sales tax rate of 6.25% is applied to the subtotal for the items purchased. Be sure you assign a variable, TAXRATE to 0.0625. The program is to process a number of items, numItems,...
(C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {    ...
(C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {     cout<<i<<endl; }
How many times will the following while loop iterate? int i = 1; while (i <...
How many times will the following while loop iterate? int i = 1; while (i < 5) {     i = i + 1;     System.out.println(“Hello!”); } Group of answer choices 4 0 5 It will iterate infinitely
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT