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

  Algorithm design and development - the graduate designs and

algorithm design and development - the graduate designs and develops algorithms for problem solving and implements

  The class overloaded constructor receives a 2-dim int array

The class overloaded constructor receives a 2-dim int array as a parameter and assigns its values to a private 2-dim int array. Have another method that prints the whole 2-dim table.

  Determine values of the side of triangle

Enter 3 integers and determine if these 3 values make up the sides of a triangle. If any side is

  Uses a loop to populate a one-dimensional array

Write the following program that creates and uses a loop to populate a one-dimensional array that holds the even numbers between 1 and 12; creates and uses a loop to populate a second one-dimensional array that holds the odd numbers between 1 and 12;..

  Implement a shopping cart class with user interface

project will be to implement a shopping cart class with user interface (UI) that contains main() in Net Beans. The UI class will be used to perform user input/output and to invoke the appropriate methods of shopping cart class. When your program star..

  Write a complete java program to simulate ahmed''s show

Suppose you're on a game show and you're given the choice of three doors. Behind one door is a car; there is nothing behind the others. The car and is placed randomly behind the doors before the show. The rules of the game show are as follows:

  Calculate the percentage wins of a team

Write a program that will calculate the percentage wins of a team. The program will input the number of wins and losses that a team acquired during a season. The wins should be input in a parameter-less value returning function that returns the wi..

  What vulnerabilities might these threats exploit

What special consequences do you foresee from information security breaches confined to online retail banking compared to online commercial banking?

  Develop a java program

Develop a Java program which, given the width, length and depth (in metres), of a swimming pool, determines and outputs.

  Write a program in Java to check if a number is even

Write a program in Java to check if a number is even or odd in Java?

  How are they used in javadoc comments

What is a tag and how are they used in Javadoc comments?

  Questions of java programming

Write a program that opens a .txt, reads the contents of the file line by line, and prints the content of each line. To do this, you should use look up how to use the Buffered Reader class1. Remember to use the try and catch statements to handle e..

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