CSCI 241 Labs: Lab 4
Think Before you Leap


There are 5 checkpoints , including the clean-up checkpoint, in this lab. You and your partner should work together 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 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.

 

Implementing Pseudocode

Open Project Grades in the Lab04 subdirectory. This project contains a program to calculate student grades from exam scores. Compile and run the LetterGradeCalculator class. Enter any three exam scores. From the output it should be obvious that the average and letter grade are not calculated correctly. Follow the pseudocode provided in the prelab to finish the coding in these sections:
  1. Finish the assignGrade section so that it prints the correct letter grade
  2. Finish the calculateAverage section so that it prints a double
  3. Finish the canContinue section by setting canContinue to hold true when the average holds 70 or above, and false otherwise.

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:

  1. Printing if the student could continue on to the next semester (CSCI-242) could have been written without a boolean variable. In your opinion, which way produces more easily readable code?
  2. Could the program assign a letter grade using a switch statement, instead of the multiple if-else statements? Why or why not?

Clear the output window and close the Grades project.

 

The Dangers of Pseudocode

One of the dangers of pseudocode is that students often confuse it with source code. Pseudocode lays out the logic of the algorithm. It does not include the syntactic details of any particular language.

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:

   if n < 0

      print a message saying changing signs

      n = -n

   fi

  
(Sometimes programmers use fi in their pseudocode to indicate where the if ends.)

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.

 

Finding the Minimum

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:

  1. Watch out for equality. Your code should still determine the minimum even if two of the numbers hold the minimum value.
  2. There are multiple approaches to this problem.
    • You may use if statements with && in them.
    • You may use nested ifs (i.e., an if inside another if).

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:

  1. Was your pseudocode correct for all three sections?
  2. Which technique did you use? Could it be easily extended to work on larger groups of integers?

Clear the output window. Close the project Min.

 

Changing Colors

Open project CircleColor. You have been working on lots of main() methods in these first chapters and it's getting a little tedious. The cool thing about programming is that once you know these basic techniques, you can "plug them in" to more complicated programs and classes.

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.

  • Could you have written this code with a switch statement?

  • Close project CircleColor.

     

    After the Lab 

    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.