Question

In: Computer Science

JAVA Lab Assignment #13:  Looping Lab with both types of loops. This lab demonstrates the use of...

JAVA

Lab Assignment #13:  Looping Lab with both types of loops.

  • This lab demonstrates the use of the While Loop and the Do While Loop as error checking mechanisms.
  • You will be using each of these loops to solve the same problem.
  • Please put them both in the same program.
  • When you test the code, you will not see a difference in the way they execute - but there will be a difference in the logic when writing the code.
  • You will want to write a refined algorithm for both to see the logic differences.

Lab 13 Part a:

Using a While Loop, write the code that does the following:

  • Prompts the User for a score between 0 and 100 (inclusive).
  • Repeats the prompt until the User enters a valid number within the specified range.

Lab 13 Part b:

Using a Do While Loop, write the code that does the following:

  • Prompts the User for a score between 0 and 100 (inclusive).
  • Continues to prompt for scores until the User enters a valid number within the specified range.

Include the following in your programs:

  • A refined algorithm for each looping structure.
  • Internal documentation for your code.

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)
3. Do not place an equation for computation on the same line as an input statement.  (-.1)

Solutions

Expert Solution

Solution :

Initiallize n to -1. For both the while and do-while loops, write the condition for n to not be in the range [0-100] and continue till the condition is not satisfied.  

Following is the Java code for the same :

import java.util.*;
class Main { 
        public static void main(String args[]) 
        { 

                // initialisation expression 
                int n; 
                n=-1;
                Scanner sc= new Scanner(System.in); //System.in is a standard input stream.
                System.out.println("\nWhile loop demonstration : \n");
                while(n<0 || n>100) { 

                        // Prompt the user 
                        System.out.println("\nEnter a score between 0 and 100(inclusive)\n");
                        n=sc.nextInt();
                } 
            
            System.out.println("\nDo While loop demonstration : \n");
            do
            {   // Prompt the user 
                        System.out.println("\nEnter a score between 0 and 100(inclusive)\n");
                        n=sc.nextInt();
                
            }
                while(n<0 || n>100);
        } 
} 

Code demo for reference :


Related Solutions

Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates the use of loops, and generation of random integers. Instructions You are taking some time off from your paint business and currently are on vacation in Bahamas. You decide to write a Java program that generates 10 random numbers between 1 and 20 (all integers). You cannot use arrays (even if you know what they are) to store these numbers. It then picks up...
Assignment Purpose Write a well commented java program that demonstrates the use and re-use of methods...
Assignment Purpose Write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht can olny sepll a wrod one way.”...
This assignment will acquaint you with the use of while loops and boolean expressions. You will...
This assignment will acquaint you with the use of while loops and boolean expressions. You will create a program that acts as a score keeper of a racquet ball game between two players. The program will continually ask the user who the winner of every point is as the game is played. It will maintain the scores and declare the match is over, using these rules: (1) A game is over when a. one of the players wins 7 points...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods.(Need Comment, Write by Java Code) Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of two dimensional arrays, input validation, and methods. (Write by Java Code, Need Comment) Instructions A theater seating chart is implemented as a two-dimensional array of ticket prices, like this: Seat Ticket Price 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
This assignment is about creating a online movie rental store. Use arrays and loops for this...
This assignment is about creating a online movie rental store. Use arrays and loops for this assignment. 1) Create a web page containing a list of movie names available for rent (at least 10 movies), so that each time you visit the home page, you see a different order of movies in the list. The movie names are required, all other information is optional.) 2) The rental price for each movie ranges between $1.99 to $7.99.             • Create an...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of the following sorting methods (Insertion Sort, Selection Sort, Quick Sort, and Merge Sort) to sort ArrayList of objects using Comaprable interface. (60 points)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT