Using a sentinel value to control a while loop

Assignment Help JAVA Programming
Reference no: EM13940375

Problem 5.2Using a Sentinel Value to control a while Loop

On this exercise I need to write a while loop that uses a sentinel value to control a loop in a Java program.  I also need to write the statements that make up the body of the loop.  I have already entered the necessary variable declarations and output statements. 

  • Each theater patron enters 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.
  • I need to write the while loop using a sentinel value to control the loop, and also write the statements that make up the body of the loop.

importjavax.swing.JOptionPane;

publicclassMovieGuide
{
publicstaticvoid main(String args[])
   {

// Declare and initialize variables.
doublenumStars;         // star rating.
      String numStarsString;   // string version of star rating
doubleaverageStars;     // average star rating.
doubletotalStars = 0;   // total of star ratings.
intnumPatrons = 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.

Problem 5.3 Using a for Loop

 

After I have completed this program it should print the numbers 0 through 10, along with their values multiplied by 10 and by 100.

  • I need to write a for loop that uses the loop control variable to take on the values 0 through 10.
  • In the body of the loop, multiply the value of the loop control variable multiplied by 10 and by 100.


publicclassNewMultiply
{
publicstaticvoid main(String args[])
   {

      String head1 = "Number: ";
      String head2 = "Multiplied by 10: ";
      String head3 = "Multiplied by 100:  ";         
intnumberCounter;   // Numbers 0 through 10.
intbyTen;     // Stores the number multiplied by 10.
intbyHundred;            // Stores the number multiplied by 100.
finalint NUM_LOOPS = 10; // Constant used to control loop.

// This is the work done in the housekeeping() method
System.out.println("0 through 10 multiplied by 10 and by 100" + "\n");

// This is the work done in the detailLoop() method
// Write for loop

// This is the work done in the endOfJob() method
System.exit(0);
   } // End of main() method.

} // End of NewMultiply class.

Problem 5.6 Accumulating Totals in a Loop

After I have completed this program, it should calculate two totals:  the number of left-handed people and the number of right-handed people in your class.  The loop should execute until the user enters the character 'X' instead of a 'L' for left-handed or 'R' for right-handed. 

Left or Right-Handed

L

R

R

R

L

L

R

X

  • I need to write a loop and a loop body that allows you to calculate a total of left-handed and a total of right-handed people in our class.

// LeftOrRight.java - This program calculates the total number of left-handed and right-handed
//                    students in a class.  
// Input:  L for left-handed; R for right handed; X to quit.
// Output: Prints the number of left-handed students and the number of right-handed students.

importjavax.swing.JOptionPane;

publicclassLeftOrRight
{
publicstaticvoid main(String args[])
   {

      String leftOrRight = ""; // L or R for one student.
intrightTotal = 0;   // Number of right-handed students.
intleftTotal = 0;    // Number of left-handed students.

// This is the work done in the housekeeping() method
leftOrRight = JOptionPane.showInputDialog("Enter L if you are left-handed, R if you are right-handed or X to quit.");

// This is the work done in the detailLoop() method
// Write your loop here.

// This is the work done in the endOfJob() method 
// Output number of left or right-handed students.
System.out.println("Number of left-handed students: " + leftTotal);
System.out.println("Number of right-handed students: " + rightTotal);
System.exit(0);
   } // End of main() method.

} // End of LeftOrRight class.

 

Reference no: EM13940375

Questions Cloud

What are the disadvantages of ifrs : What is a Business Plan important and have you ever been involved in developing one? What are the Disadvantages of IFRS (International Financial Reporting Standards) to GAAP (Generally Accepted Accounting Principles)
What is the price earnings ratio of the company : What is the price-earnings ratio of the company? What would the P/E ratio be if the discount rate were 10%?
Describe the cost formula for a strictly fixed cost : Why do mixed costs pose a problem when it comes to classifying costs into fixed and variable categories? Describe the cost formula for a strictly fixed cost such as depreciation of $15,000 per year.
Legal basis for teh eeoc : What is the legal basis for teh EEOC to hold that JBS-SWIFT had violated the employees civil rights?
Using a sentinel value to control a while loop : On this exercise I need to write a while loop that uses a sentinel value to control a loop in a Java program.  I also need to write the statements that make up the body of the loop.  I have already entered the necessary variable declarations and o..
Putting people first for organisational success : Pfeffer and Veiga (1999 pp. 37-48) in their 1999 article 'Putting people first for organisational success' discuss how many managers do not practise the "Seven principles of successful organisations" in the workplace.
What solutions our law enforcement using to combat this gang : Research motorcycle gangs in United States (e.g. the Hells Angels) and describe their criminal enterprise. How do they fund their organization? How is their leadership structured? Describe the gang and how it has changed its community. What soluti..
Incentives for quality work : Grades are partly intended to provide incentives for quality work. How do students strategically game grading systems in ways that might pervert this intent?
Current policy for recruit and preparing expatriates : What could the company have done to better prepare Donaldson? What does this case suggest about company's current policy for recruit and preparing expatriates? Make policy suggestions.

Reviews

Write a Review

 

JAVA Programming Questions & Answers

  A regular polygon is an n-sided polygon

A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree(i.e the polygon is both equilateral and equiangular).

  A small mobile application using android or ios

A small mobile application using Android or IOS, the application idea chosen by each student individually, or in groups of two students only. The app must be not less than 3 activities.

  Definition of constant initialization arrays

You have to extend the definition of constant initialization arrays to multi-dimensional arrays

  User enters a list of car parts

So if the user enters a list of car parts, the programm holds this list. Afterward, when the user types in the name of the part the programm outputs that name from the list.

  Executing stringed musical instrument class

Create and execute a stringed musical instrument class.

  Algorithm analysis with advanced data

algorithm analysis with advanced data structuresalchemy-mines of oreland yield unlimited amounts of silver but no gold.

  What things are you struggling to understand

Discuss your experiences with the programming assignment - what things are you struggling to understand?

  Methods are commonly used to break a problem down into

methods are commonly used to break a problem down into small manageable pieces. a large task can be broken down into

  Outline a test plan for a substantial real-life system

Describe how you would approach the design and testing process to ensure success and quality in the result and where are the risks? How are you going to address them?

  Question hierarchy of section

Add a class AnyCorrectChoiceQuestion to the question hierarchy of Section 9.1 that allows multiple correct choices. The respondent should provide any one of the cor- rect choices. The answer string should contain all of the correct choices, separa..

  Using a linked implementation of graph write a method that

using a linked implementation of graph write a method that takes two nodes as input and returns true if joining an edge

  Create a japplet

Create a JApplet that contains a JLabel and JButton. You'll create a JButton that will automatically change font typeface, style, and size when clicked by the user.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd