# Outlab 4: Loops

Due: Monday 25 July at 10 pm

## Purpose

Gain experience with for and while loops.

## Partners

This is an individual assignment, though collaboration (not solution sharing) is allowed.

## Problem Statement

The goal of this assignment is fairly simple: I describe 5 methods that you need to implement and you implement and test them. This is designed to not take too much time, so get it done quickly.

• Create an Outlab4 project and make a class called Loop.
• The Loop class with not have any instance variables and the constructor will be empty (no parameters, nothing in the body).
• In Loop you will make the following 5 methods:
1. loop1(int lb, int ub, int incr) where lb is the lower bound, ub is the upper bound, and incr is the increment amount. In this method you will make a for loop that prints out the integer values from lb up to (and possibly including) the ub with an increment of incr. See output for more details about how loop1 works.
2. loop2(int lb, int ub, int incr). In this method you will accomplish the same thing as loop1 by using a while loop. The output should be the same as loop1 for the same input.
3. loop3(). This method takes no parameters and prints out the alphabet as shown in the output.
4. loop4(int val). This method uses a loop (for or while?) to do the following. Generate a random number between 1 and 10 (including 1 and 10) 10 times and count how many times val is generated, then print that number out.
5. loop5(int val). this method uses a loop (for or while?) to do the following. Generate a random number between 1 and 10 (including 1 and 10) as many times as it takes to generate val 5 times. Print the number of times it took.
• Create a Driver and test each loop method thoroughly.
• You must have the class name and method declarations as I describe above. Your Loop class may be tested, so you need to use these method names, or else it will not compile for them.

## Hints

1. Work through one method at a time. They get progressively harder, so start with loop1. Test, test, test,
2. Ask questions if a method's operation does not make sense. Here is sample output.
3. Consider using nested for loops for loop3(). Also, consult the java String class online documentation to make loop3() fairly simple (Google: "Java String"). Find methods in the String class that will help you do what you want to do. Consider using String alph = "abcdefghijklmnopqrstuvwxyz" for something.
4. Think first about what type of loop you want to do for loop4 and loop5. There is a correct answer.
5. Be careful what parameters you pass the loops. It is not hard to get stuck in infinite loops. To kill program execution, right click the candycane icon in the lower left hand BlueJ screen. Select "Reset Java Virtual Machine".

## Submission

By Monday 25 July at 10:00 pm, submit the files Driver.java and Loop.java into the appropriate D2L dropbox folder. DO NOT SUBMIT .class files!

## Grading - 20 points

• 4 points for each method including solution works, efficient, correct loop chosen, clean output, comments.
5 points lost automatically if Loop.java does not compile with the a different Driver.