Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this lab, you will review the while loop that uses a sentinel value to control a loop in a Java program. A source code

In this lab, you will review the while loop that uses a sentinel value to control a loop in a Java program. A source code has already been provided with the necessary variable declarations, logic and output statements.

Each theater patron enters a value from 0 to 4 indicating the number of stars that 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.

  1. Open the source code file named MovieGuide.java using Notepad or an IDE of your choice.
  2. Review the code and save the source code file in a directory of your choice.
  3. Compile the source code file, MovieGuide.java.
  4. Execute the program. Input the following as star ratings:

0 3 4 4 1 1 2 -1

  1. Record the average star rating for the movie.
  2. Attach execution results to Moodle

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. System.out.println("Enter rating for featured movie: "); numStarsString = s.nextLine(); // This is the work done in the detailLoop() method // Convert to integer. numStars = Double.parseDouble(numStarsString); while ( numStars >= 0 ) // Test for loop entry. { totalStars += numStars; // Accumulate total of star ratings. numPatrons++; // Add 1 to number of patrons. System.out.println("Enter rating for featured movie: "); numStarsString = s.nextLine(); numStars = Integer.parseInt(numStarsString); } // End of while loop. // This is the work done in the endOfJob() method averageStars = totalStars / numPatrons; System.out.println("Average Star Value: " + averageStars); System.exit(0); } // End of main() method.

} // End of MovieGuide class.

I know all I have to do is compile the code and run the numbers but for whatever reason when I run it the output box shows up but won't allow me to input any numbers

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Oracle APEX 5 Application Development

Authors: Edward Sciore

2nd Edition

1484209893, 9781484209899

More Books

Students also viewed these Databases questions

Question

=+ e. What problem does your answer to part (d) illustrate?

Answered: 1 week ago