Question
Using a Sentinel Value to Control a while Loop Summary In this lab, you write a while loop that uses a sentinel value to control
Using a Sentinel Value to Control a while Loop
Summary
In this lab, you write a while loop that uses a sentinel value to control a loop in a Java program that has been provided. You also write the statements that make up the body of the loop.
The source code file already contains the necessary variable declarations and output statements. Each theater patron enters a value from 0 to 4 indicating the number of stars the patron awards to the Guides featured movie of the week. The program executes continuously until the theater manager enters a negative number to quit. At the end of the program, you should display the average star rating for the movie.
Instructions
Ensure the file named MovieGuide.java is open.
Write the while loop using a sentinel value to control the loop, and write the statements that make up the body of the loop to calculate the average star rating.
Execute the program by clicking Run.
Input the following: 0, 3, 4, 4, 1, 1, 2, -1
Check that the average output is correct.
// MovieGuide.java - This program allows each theater patron to enter a value from 0 to 4
// indicating the number of stars that the patron awards to the Guide's featured movie of the
// week. The program executes continuously until the theater manager enters a negative number to
// quit. At the end of the program, the average star rating for the movie is displayed.
import java.util.Scanner;
public class MovieGuide
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
// Declare and initialize variables.
double numStars; // star rating.
String numStarsString; // string version of star rating
double averageStars; // average star rating.
double totalStars = 0; // total of star ratings.
int numPatrons = 0; // keep track of number of patrons
// This is the work done in the housekeeping() method
// Get input.
// This is the work done in the detailLoop() method
// Convert to double.
// Write while loop here
// This is the work done in the endOfJob() method
// Calculate average star rating
System.out.println("Average Star Value: " + averageStars);
System.exit(0);
} // End of main() method.
} // End of MovieGuide class.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started