Question

In: Computer Science

Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade...

Java Program.

Sentinel While Loop Lab

Do the following:

  • Prompts the user to enter a grade or a -1 to quit.
  • IF the user entered a -1 THEN
    • Display a message that the User is done entering grades
  • ELSE
    • Count each grade as it is entered.
    • Compute a running total of the grades entered.
  • END IF
  • After the user enters the sentinel of -1, calculate the average of the grades entered.
  • When computing the average, make sure that there is no division by zero. Check to make sure the counter is not equal to zero.
  • Make sure the sentinel is not counted as a grade.
  • Print out a message stating the average of the grades entered.

Include the following:

  1. Use a sentinel-controlled WHILE loop that uses a -1 as the sentinel to exit the loop.
    • Before counting the grades, initialize the counter to zero before the while loop begins.
  2. While within the body of the ELSE:
    • Within the WHILE loop:
      • Prompt the User to enter a grade or a -1 to quit.
      • IF the input is not a -1 THEN
        • Count the grade.
        • Compute a running total of these grades as they are entered.
      • END IF
    • After the WHILE Loop is done executing, compute the average of all the grades entered. Remember to check to make sure you don't perform a division by zero.

NOTE 1:

  • DO NOT use the "return 0" code, end, break, or exit to force an exit from within your IF/ELSE structure.  Declare all variables within the data declaration section.
    • Let the program progress to the end of the main function.
    • The end of the main function is the only place that the “return 0” should be placed.
    • A SWITCH statement is the only place that the break command should be used.
  • DO NOT use the "continue" statement.

NOTE 2:

1. Declare all variables within the data declaration section of each class and method.  (-.1)
2   Do not get input on the same line as a variable declaration.  
(-.1)

3. Do not place an equation for computation on the same line as declaring a variable.  (-.1)
4. Do not place an equation for computation on the same line as an output statement.  (-.1)

Thanks to whoever helps me!!! :)

Solutions

Expert Solution

code:

import java.util.Scanner;
class grade
{
   public static void main(String[] args)
   {
       int grade,count=0;
       double sum=0,average;
       /*Declaring variables*/
       Scanner scnr=new Scanner(System.in);/*Creating Scanner object*/
       System.out.print("enter a grade or a -1 to quit: ");
       grade=scnr.nextInt();
       /*Reading grde from the user*/
       sum+=grade;
       while(grade!=-1)
       {
           /*This loop execute untill grade is equals to -1*/
           System.out.print("enter a grade or a -1 to quit: ");
           grade=scnr.nextInt();/*Reading grade form the user*/
           sum+=grade;
           /*Adding grade to the sum*/
           count+=1;/*Increading the count*/
       }
       sum+=1;
       /*HEre we add 1 to sum because
       in the loop we add -1 to sum when user enters -1*/
       if(count!=0)
       {
           average=(sum/count);
           /*If count not equals to we prin the average*/
           System.out.println("Average Grade:"+average);  
       }
       else
       {
           /*Else we print average as 0*/
           System.out.println("Average Grade:"+0.0);
       }
   }
}

Output:

Indentation:


Related Solutions

Use a sentinel while loop that repeatedly prompts the user to enter a number, once -1...
Use a sentinel while loop that repeatedly prompts the user to enter a number, once -1 is entered, stop prompting for numbers and display the maximum number entered by the user. I am struggling to have my program print the math function. Here is what I have so far: import java.util.*; public class MaxSentinel { public static void main(String[] args) {    Scanner input = new Scanner(System.in); System.out.println("Please enter a value. Press -1 to stop prompt."); int number = 0;...
​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user...
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel...
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of...
In Java please: 3.39 Lab 6e: CharacterOps Write a program that prompts the user to enter...
In Java please: 3.39 Lab 6e: CharacterOps Write a program that prompts the user to enter their first name. Say hello to the person. Then display whether their name begins with a vowel or consonant. If it is neither, perhaps a special character or a number digit, then print neither a vowel nor a consonant. Example 1: Enter your first name: Barbara Hello, Barbara! The first letter of your name, 'B', is a consonant. Example 2: Enter your first name:...
Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers....
Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user whether he or she wishes to perform the operation again. If so, the loop should repeat, otherwise, it should terminate. Use Continue branching statement Write a program that reads an integer and display all the positive ODD numbers from 0 to n (integer entered by the user). Use CONTINUE...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
java language NetBeans Write a program that prompts the user to enter the weight of a...
java language NetBeans Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent weight in pounds. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounds.) Format your output with two decimal places.
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
Use Java (Geometry: point in a rectangle?) Write a program that prompts the user to enter...
Use Java (Geometry: point in a rectangle?) Write a program that prompts the user to enter a point (x, y) and checks whether the point is within the rectangle centered at (0, 0) with width 10 and height 5.  For example, (2, 2) is inside the rectangle and (6, 4) is outside the rectangle. (Hint: A point is in the rectangle if its horizontal distance to (0, 0) is less than or equal to10 / 2 and its vertical...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT