CSCI 241 Labs: Lab 5
CSCI 241 Labs: Lab 5
Going Around in Circles
There are 5 checkpoints
, including the
clean-up checkpoint, in this lab.
Work together with your partner using just one of your accounts.
CHANGE WHO IS CONTROLLING THE COMPUTER AFTER EACH CHECKPOINT!
If you need help with any exercise, raise your hand.
Copy the directory /home/student/Classes/Cs241/Labs/Lab05 to your account.
Change directories into Lab05 and start BlueJ by
entering bluej&.
Using the Debugger
Open the project Debugger and edit the VeryLongLoop class.
Look at the for loop.
It is not an infinite loop, just a loop that will run for a very long time.
Look for the for loop line. Left-click on the left side of that line
(if you have line numbers displayed, click on the line number).
This will set a breakpoint where the program will stop running, and let you
single-step through the code.
Close the editor, compile the class, and run its main() method.
When BlueJ reaches the breakpoint you set, stop and examine the values of ALL variables
in the program (i, j and AVOGADROS)
by reviewing the variables in the lower-right panel of the Debugger window.
Click the Continue button to run all lines within the loop and then see how the
variable values change. Once you see the patterns of how each variable changes (or
does not change), you can close the debugger window by clicking the
Terminate button.
Close the VeryLongLoop class editor window.
----------------------------------------------------------------------
There are two other classes in the Debugger project, TabulateFractions
and TabulateFractionsAgain.
Both of these (at least sometimes) contain infinite loops.
Your job is to use the debugger to find and remove the problems with the loops.
Open the TabulateFractions class in the BlueJ editor.
The main() method is supposed to print a table of the fractions with a
given denominator up to and including 1.0.
For example, if the input is: 4, the output from the program is
Fraction Decimal
1/4 0.25000000
2/4 0.50000000
3/4 0.75000000
4/4 1.00000000
Follow through the code and make certain both you and your partner understand
what each line is doing.
Compile the class and run its main() method with an input of 4.
The table should appear exactly as above.
Now we will set a breakpoint
in the program and walk through the code line by line.
CAUTION: BlueJ's debugger can be
very tempermental, and it is important to use it
in just the right way. Follow these directions carefully.
If BlueJ hangs or won't let you terminate a program, it may be
necessary to kill off BlueJ and restart it.
See the bottom of the last page of this lab for complete directions on how
to stop a runaway program in BlueJ.
- Open the editor window.
Use the mouse to set a breakpoint on line number 36, the first line of code
after the if() statement.
Do this by left-clicking on the line number in the vertical bar on the left side
of the window directly to the left of the double decimal = 0.0;
statement. (Note: you don't need to have line numbers displayed in order
to set breakpoints.) A stop sign will appear.
- Run the main() method.
Enter an input of 4, again.
The program will stop with the double decimal = 0.0;
statement highlighted. We know it has not yet run because the variable
named decimal doesn't appear in the lower-right
debugger window.
- Click the Step button in the debugger window to execute
the highlighted line. After it runs, the decimal variable
appears in the window.
- Press the Step button again and again watch the program
execute the loop. Between clicking the Step button, you may examine
the current state of all the variables in the program. Pay particular
attention to the values the decimal variable holds.
- You will know the main()method has finished running when all the buttons in the
debugger turn grey, as does BlueJ's barberpole. You will still need
to close BlueJ's terminal window and the debugger window, by clicking on
the X in the upper left corner of each.
Now we will re-run the program, but entering an input value that
doesn't work (i.e., causes an infinite loop).
Run the main() method and use an input value of 6.
Leave the breakpoint set, and use Step
so we can look at the results after each line runs.
(Note: The Step Into is used when we have more methods than
main() to jump into. We'll start seeing them in
chapter 6.)
After the fifth time through the loop, the program should print out
5/6 0.83333333
Now watch the value of decimal very carefully.
After the sixth time through the loop, the program should print out
6/6 1.0000000
and terminate.
What happened instead?
Terminate the run of the program by clicking the Terminate button.
(If BlueJ does not want to stop the program, you may have to terminate
and restart BlueJ. There are notes at the end of this lab on how to do this.)
As you learned in class (we hope :), it is very dangerous to use = =
and !=
with doubles. Change the while loop condition so that the loop
continues until decimal is within 0.0001 of the value 1.0.
Compile your class, and re-run the main() method with an input of 6.
Try another test case by running the main() method with an input of 7.
Have you fixed the bug?
Once you are convinced that the bug is gone, remove the breakpoint by
clicking on the stop sign.
----------------------------------------------------------------------
The final class in the Debugger project TabulateFractionsAgain
is supposed to print the same table as the previous project.
Unfortunately, this one, too contains a bug that causes an infinite loop.
Use the debugger to find and remove the bug.
1
Be prepared to answer the following questions:
- In the VeryLongLoop program, could i's value have been printed after
the loop had finished?
- Explain how each of the values were changing (or not).
- Explain what the bug was in TabulateFractions and how you used the debugger to remove it.
- Explain what the bug was in TabulateFractionsAgain and how you used the debugger to remove it.
Show us your corrected programs, and explain the problem with each.
Close project Debugger.
Loops in Graphics
Open project Dazzle. Compile the CoolCircles class and run
its main() method.
The program creates a series of concentric circles on a window.
While it looks kind of cool, all the colors are currently some shade of gray.
Open CoolCircles in the BlueJ editor and locate the for
loop beginning in line 32. The loop is already working as written, and your
job is to change the lines within the loop body to give the circles color.
To construct a Color, you must give three integer
values red, green, and blue, which stand for the amounts
of red, green, and blue, respectively, to mix and create the color.
In the current program, the
amounts of red, green, and blue used for each color are always the same.
This creates the shades of gray.
Modify the values assigned
to red, green, and blue so that the circle you draw
will have color. We want you to use certain constraints while
assigning the values.
On each iteration of the loop:
- red, green, and blue must have different values
- red, green, and blue must have their values assigned
in terms of the loop variable radius
- red, green, and blue must always have a value between
0 and 255 inclusive for every iteration of the loop
Use your imagination.
There are plenty of different formulas that meet these constraints.
Here are some examples to give you ideas:
int red = 255 - radius;
int red = (radius*radius)%256;
int red = (int)(radius/5.8)*5;
2
Show us your revised code, and how it runs. Make sure you have used something
different than the examples above in at least 2 of the color settings.
Close project Dazzle.
Input Validation
Both decision statements and loops are used when validating input.
Input validation is the very important process of making sure the input the user supplies
makes sense in the context of the program.
For example, if working on a cash register program, it would not make sense to purchase
-3 items.
If a student enters a phone number (including area code) it should consist of 10 digits.
In this day and age, checking input is critical, since one of the primary ways
hackers break into systems is by supplying invalid input values.
Open project AnswerMe.
It contains a single class, OneToTen.
Open it in the editor.
This program is supposed to keep asking the user for a number between 1 and 10 (inclusive) and to stop asking
when it received valid input.
However, when you run it, it takes any integer you decide to enter.
Modify the program in these ways:
- You are required to use a do..while loop to accomplish these tasks.
- Once the user provides valid input, print the value they entered and the number of tries it took them.
e.g., if they enter 7 on the first try, the program should print 7 and 1.
- Give the user up to three (3) chances to enter a number between 1 and 10.
- If, after three tries, the user still hasn't entered a valid number, print an error message
politely telling them that they are "Not Reading Directions!" and terminate.
- In addition, you may also use an if inside (and/or outside) the do...while
if you think it will help.
3
Show us this program when you are ready.
Close project AnswerMe.
Files are Forever
Using Java to read from and write to files is covered in detail in Chapter 12 of the text.
Now that we have Scanners and loops available, however, we can do some simple file processing.
The Scanner class that we use to read from the keyboard may also be used to
read from files. When we make a Scanner to read from a file, we provide
it the name of a File object, instead of System.in.
Open the project StudentFile.
Compile and run the main() method of the Student class.
It should compile and run without problems.
It reads the data for single student from a data file and prints the student's
name and two exam scores to the output window.
Open the Student class in the editor.
First, look at the filename.
The File class constructor requires that we specify the entire
(absolute) filename path in order for the system to be able to find the file.
As you can see, the current code specifies where the original Lab05 files
exist.
There is also a copy of the file in your Lab05 directory.
Modify the filename to point to your copy.
Hint: Type pwd (the present-working-directory command) in a
terminal window to find the name
of the directory in which you are currently working.
To view the actual file we are reading from, go to terminal window, cd to the Lab05
directory, and type the command more students.dat.
The contents of the file should appear on the screen.
Note that there is more than one student in the file, but the program
currently reads only the first one.
Your job is to modify the program so that it processes the entire file.
Return to the Student class in the BlueJ editor window.
Add a while loop below the Scanner declaration so that all data
in the file will be read.
The while loop should look like:
while (fileIn.hasNext())
{
. . .
}
In clearer English, the while says to read until we reach the end of the file.
Recompile and run the program.
Now your program should print data for all the students in the file.
4
Show us your completed program.
Close project StudentFile.
 
Extras for Experts
No extra credit for this, just the fun of trying it.
Modify your Student class so rather than printing the entire file,
it performs a search for a particular student and prints their name and
grades. To do this:
- Above the loop have the user enter the name of the student being sought.
- Within the loop place an if statement that checks to see if
the two names are equal.
We cannot compare names using ==, instead we need to use the String
class' equals method. E.g. s1.equals(s2) checks to see
if String s1 is equal to String s2.
Only print the name and scores when the student is found.
- If the student is not found in the file, print an error message when
the end of file is reached.
After the Lab
Return to Firefox, log on to Canvas and print the next programming assignment, #5, if you haven't
already.
Be sure to exit Firefox before you log out and leave the lab.
5
Show us that you have logged out, turned off your monitor, cleaned up, and pushed in your chairs
for this last checkpoint.
Stopping a Runaway Program in BlueJ
The BlueJ Debugger can be tempermental. If you hang BlueJ or get BlueJ into a state where it
is running an infinite loop that you can't terminate, each of the following may help.
- You can stop a runaway program in BlueJ by right-clicking the moving arrow
in the lower right area of the main BlueJ window.
The only choice is "Reset Machine" - choose it and it will stop
moving, indicating that the program has stopped.
- Another easy way to kill BlueJ is to go to its main window and click the X in the upper right corner.
This should kill BlueJ including all its windows. 98.375% of the time this works.
- The next easiest way to kill BlueJ is to right click on the BlueJ item on the task bar at the
bottom of your screen. A popup menu appears that includes an option that says kill App.
Choose this option.
- If none of these work, call over your instructor or lab assistant.