Copy the lab materials to your account. They are located in the directory
/home/student/Classes/Cs241/Labs/Lab04.If you forgot how to copy, refer to lab exercises 1 or 2.
Change directories into Lab04 and start BlueJ by entering bluej&.
For this lab, you and your partner should get out some paper and some pens or pencils.
Thoroughly test your program. Do not clear the output window - we'll want to look at it when we check you off.
1 Tell us the answers to the questions. Be prepared to answer the following questions:
Absolute value is defined as the positive equivalent of any number.
In mathematics we typically write it using vertical bars.
|-3| = 3 |3| = 3
Pseudocode to take the absolute value of a number n is:
(Sometimes programmers use fi in their pseudocode to indicate where the if ends.)if n < 0 print a message saying changing signs n = -n fi
Open project Hmmmmm.
Compile and run the AbsoluteValue main() method with several
input values of your choice,
both positive and negative. This program is supposed to take
the print the absolute value of the number entered.
It doesn't, even though the code closely follows the pseudocode.
Isolate the bug and remove it so that it works correctly.
2 Show us the corrected program. Be prepared to answer the following question:
Clear the output window and close project Hmmmmm.
Now it is your turn. Finding the minimum of two integers is easy. Here is the pseudocode:
findMinOf2 coding: if first < second minimum = first else minimum = second fi end findMinOf2 coding
Finding the minimum of three or four integers is more difficult. On a separate sheet of paper, write pseudocode for two sections of code: one that calculates the minimum of three integers and one that calculates the minimum of four numbers.
Hints:
Open project Min and use the editor to examine the MinFinder class. The MinFinder class contains sections that determine the minimum of 2, 3 and 4 integers. Notice that the code is already implemented that gets values for the integers. Close the editor and compile and run the main() method. The program should run, but returns incorrect results. The sections to determine the minimum values are not yet written.
Edit the class MinFinder. Replace the code in all three sections by following your pseudocode for each.
When ready, thoroughly test each section by running the main()
method with
differing sets of values.
3 Show us your pseudocode and completed and tested code. Be ready to answer the following questions:
Clear the output window. Close the project Min.
Use the editor to examine the Demo class. It also contains a main() method, but with very different contents than what you have seen so far in the textbook. Remember how you create a Scanner object for the keyboard? You write:
Scanner keyboard = new Scanner(System.in);We know that keyboard is the name of this Scanner, and that when we wish to read input data, we use keyboard. in front of a method which describes the type of data we are reading, such as nextInt().
Instead of Scanner objects, the Demo class works with a CirclePicture object. The CirclePicture class was written by Dr. Hansen and exists inside this BlueJ project. When we wish to use the CirclePicture object named myCircles, we can write myCircles. in front of an action we wish it to do.
The Demo program draws eight circles in the picture. For each circle, we choose a color and radius before it can be drawn. Can you see how the coding works so far? The part which asks for a color is hidden inside the CirclePicture class. Part of the code uses a keyboard Scanner to list the colors available and ask the user for a color number. After finding out the color and setting the radius, there is code in the CirclePicture class which uses this information to draw the circle in a window. Currently, the circle drawn will always use the color CYAN (this is a light blue). See how this works by compiling and running the Demo program main() method.
You and your partner will complete the CirclePicture class to allow the user to select a variety of colors. The Demo program will then use the selected color to draw the circle. To do this, open CirclePicture in the editor window. Look for the Scanner. The input prompt currently lists only one color to choose from. However, there are many more! Legal color names must be written as Color.NAME, where NAME can be any of this list: BLACK, WHITE, BLUE, RED, YELLOW, CYAN, DARK_GRAY, ORANGE, GRAY, GREEN, PINK, LIGHT_GRAY, MAGENTA. Add 7 or more colors to the options given in the input dialog to give you a total of at least 8 choices. You can use the \n character within a String inside one print() statement to make all colors appear in the same prompt line, if you prefer that look.
The value returned from the input dialog needs to be translated into a matching Color object. The if statement in the chooseNewColor() method takes care of this translation. Add code to expand the if statement so that it returns the appropriate color for each choice.
When ready, thoroughly test your changes using the Demo
class.
Feel free to draw more circles in the Demo class if you wish.
4 Show us your completed and tested program.
Close project CircleColor.
Your new programming assignment is available on Canvas.
5 Show us that you have logged out, cleaned up, turned off your monitor and pushed in your chairs for this last checkpoint.