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...
Write a well-commented java program that demonstrates the use of loops, and generation of random integers...
Write a well-commented java program that demonstrates the use of loops, and generation of random integers (Code will be written in Dr. Java) 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 the largest number...
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.”...
22.8 LAB 5 D FALL 19 : Using math functions This lab problem demonstrates the use...
22.8 LAB 5 D FALL 19 : Using math functions This lab problem demonstrates the use of import module. The Python programming language has many strengths, but one of its best is the availability to use many existing modules for various tasks, and you do not need to be an experienced computer programmer to start using these modules. We have given you some incomplete code; note that the very first line of that code contains an import statement as follows:...
Write a program that uses loops (both the for-loop and the while loop). This assignment also...
Write a program that uses loops (both the for-loop and the while loop). This assignment also uses a simple array as a collection data structure (give you some exposure to the concept of data structure and the knowledge of array in Java). In this assignment, you are asked to construct an application that will store and retrieve data. The sequence of data retrieval relative to the input is Last In First Out (LIFO). Obviously, the best data structure that can...
Assignment 7 Preparation This assignment will focus on the use of loops, random numbers, named constants,...
Assignment 7 Preparation This assignment will focus on the use of loops, random numbers, named constants, and arrays. Assignment 7 Assignment 7 Submission Follow the directions below to submit Assignment 7: Create a Java program. The class name for the program should be 'RandomDistributionCheck'. In the main method you should perform the following: You should generate 10,000,000 random numbers between 0 - 19. You should use an array to hold the number of times each random number is generated. When...
Assignment 7 Preparation This assignment will focus on the use of loops, random numbers, named constants,...
Assignment 7 Preparation This assignment will focus on the use of loops, random numbers, named constants, and arrays. Assignment 7 Assignment 7 Submission Follow the directions below to submit Assignment 7: Create a Java program. The class name for the program should be 'RandomDistributionCheck'. In the main method you should perform the following: You should generate 10,000,000 random numbers between 0 - 19. You should use an array to hold the number of times each random number is generated. When...
Java Prorgramming Skills Looping Branching Use of the length(), indexOf(), and charAt() methods of class String...
Java Prorgramming Skills Looping Branching Use of the length(), indexOf(), and charAt() methods of class String Use of the static Integer.toHexString method to convert a character to its ASCII hex value Description In this assignment, you'll be URL encoding of a line of text. Web browsers URL encode certain values when sending information in requests to web servers (URL stands for Uniform Resource Locator). Your program needs to perform the following steps: Prompt for a line of input to be...
Find the Standard Deviation in Java: Use Arrays and Looping. Calculate the Standard Deviation once you...
Find the Standard Deviation in Java: Use Arrays and Looping. Calculate the Standard Deviation once you have the m numbers in an Array of size m. *Don't use different methods
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT