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

  Java threads

This is an introductory assignment on Java synchronization. You will use Java Threads while learning more about concurrency and achieving atomicity using Java’s inbuilt mechanisms.

  Create a circle class

create acircle class, which has one field, the radius. the class should contains two constructors: a no-argument constructor that set the radius to zero, and a constructor that takes a parameter to set the radius.

  Describe how you would design a class for it

A class is a blueprint for an object. A class may have a default constructor, a constructor with arguments, accessor methods, public fields, private fields, and mutator methods.

  Good idea to test a general class with a driver program

Question 1: Why is it a good idea to test a general class with a driver program before trying to use it? Question 2: What is the difference between black box and glass box debugging?

  Explain the meaning of polymorphism

Explain the meaning of polymorphism

  Determine when the application should stop looping display

Write an application that reads a series of pairs of numbers as follows: 1) product number 2) quantity sold - Create an application in java

  Computer network and data communication

What are the question for computer network and data communication

  Java programming-gamesystem

JTabbed Panes for GUI Components Part 2: --- Create a JFrame program with a JTabbed pane on it with two panels that mimics a control box for a game system.

  Write the program using a for-loop and a break statement

Write a program that asks the user to enter a number that's positive and a multiple of 4. The user has three attempts to find such a number. Write the program using a for-loop and a ‘break' statement in the loop

  Write a java program that prints the numbers from 1 to 125

Write a Java Program that prints the numbers from 1 to 125. But for multiles of three print "Batman" instead of the number and for the multiples of five print "Superman". For instances which are multiples of both three and five "JusticeLeague".

  Demonstrate your knowledge in a pragmatic way

Summarize everything that we have addressed in the XML Applications course, and provide a mechanism to demonstrate your knowledge in a pragmatic way.

  Your program should ask the user to input a grade

Your program should ask the user to input a grade (integer from 1-100) and then output the letter grade.

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