CSCI 157 - HW 5

## Homework 5 - Loopy Homework

Important for all loops:
• I   -   initialize the variable(s) for controlling the loop
• C   -   condition involving the variable(s) for controlling the loop
• U   -   update the value(s) of the variable(s) for controlling the loop
If you get ALL THREE RIGHT, correct loop, yeah!!!

And a correct loop:

• starts & stops when it should
• does what it's supposed to

Getting yourself into a pattern that helps you remember all the parts...

 // comment about what the loop is supposed to be doing initialize while (condition){       ----------------;       -------------------;       --------------;       ---------------;       -----------;       ---------------;       update } // NOTE: default size of a loop body is ONE STATEMENT // but this one has several statements because of {}'s

Examples
```// count 0 to 9 on a line with spaces between digits
System.out.printf("\n");
int count = 0;
while ( count < 10 ) {
System.out.printf( "%d  ",count);
count ++;
}
System.out.printf("\n");

```
```// keep asking until user provides a negative integer
int x = 999;
while ( x >= 0 ) {
System.out.printf("\nPlease enter a negative integer: ");
x = inputScanner.nextInt();
}
```
```// draw a row of 20 ovals
final int DIAMETER = 50;
int cnt = 0;
while ( cnt < 20 ) {
new FilledOval (p, DIAMETER, DIAMETER, canvas);
p.translate( DIAMETER, 0);
cnt ++ ;
}
```

1. Written Work

(2 pts each) The following (a thru e) are intended as written work but there is NO reason to get them wrong. Check your answers on the computer when you think you have the correct answer (good practice for exams!). If you have gotten some wrong, be sure you understand why then write up the CORRECT answer to submit as homework. I'm happy to receive all the right stuff, (-: But as hand-written, not not not not code. And do not submit typed work either. Write out every loop by legible beautiful hand.

What I mean is.... hand write the code, don't submit compiling code. And all I want is the pertinent code fragment... So for example if there was an f) that said...

f) Count from 2 to 18 by 2's

```            2  6  8 10 12 14 16 18
```
For full credit on the "counting loops" below, use System.out.printf appropriately and include the start and end numbers indicated.

1. Count from 50 to 150 by 5's

2. Count from 3 to some number just less than 933 by 11's

3. Count backwards from 75 to down to -75 by 25's

4. Count from -10 to 5 by 0.5's

5. A complete mouse press method that contructs a horizontal row of exactly 10 framed ovals which are 10 pixels wide and 10 pixels tall from the press point to the right. Every oval should just barely touch the one to it's left. (See screenshot of result of several presses.)

2. Programs
(40 pts) for well-written code that adheres to class coding standards to date.

The following are CLIENTS. You should use your own Fish class & some other team's Fish class from our FIRST FISH LAB without modification. In fact, I'd prefer you did not modify the Fish classes just yet. Instead let's observe the usefulness of a multi-fish world as originally written. Since you'll only need the CONSTRUCTORS, pretty much any fish is a good fish here!

1. Program (5 pts) Write a client which will on begin draw a row of 5 of your fish. Use loop(s)!

2. Program (5 pts) Write a client which will on begin draw a COLUMN of 5 of SOMEONE ELSE's fish. Use loop(s)!

3. Program (Class & Client) (10 pts)

Revisit & improve your DiceyClient.java program from HW#3. You should do this by correcting whatever was marked as wrong and converting the dice into a class so that a client class can use it for each of the three dice. I expect we (have/will) discuss how to do this in class so you should have a headstart.

This class & client probably do not need any loop(s)...so don't force it, (-:

4. Program (10 pts) Write another client for your Dice class, which will on begin ask the user for some number of dice between 3 & 10. Any value between 3 & 10 inclusive should be accepted. Values strictly less than 3 or strictly greater than 10 should be rejected. Your client should keep asking until it gets a value in the range. As many times as it takes.

Then, and only then, on press the client should draw a diagonal row of the number of dice user has requested. Each die should have randomly selected value showing.

Use loop(s)!

5. Program (10 pts) Write another client for your Dice class which will on begin draw a border of dice all the way around the canvas. All parts of each dice must show. They should not overlap! No row or column can have fewer than 10 dice. Size your window & dice accordingly.

Use loop(s)!