CS Labs: Lab 5

CS Labs: Lab 5

To accompany Chapter 5 of An Introduction to Object-Oriented Programming with Java by C. Thomas Wu.

if you are reading this lab
then you are in Chapter 5

There are 10 checkpoints in this lab. If you need help with any exercise, raise your hand.

Copy the lab materials to your account. You will need to use the -r option on the Unix cp command in order to copy not only files, but directories. To copy, enter

             cp -r /home/Classes/Cs1/Labs/Lab05 .
     
Everything you need for the lab exercises today is contained in this new directory.

Change directory into Lab05.

For this lab, you will need paper and pencil.

Boolean Expressions

Go into the subdirectory Bools and open the Demo class. This program reads in an integer score, then tests its value with an if statement. If it satisfies a "test" (which you will add), it will print a message saying that the score is outside its normal range.

For the first checkpoint, write a test expression to replace the comment   "// put score test here". Your test should make the program print out that the score entered is out of range if it is under -50 or over 50.

1 Show us your code and how you tested it.

Replace the test expression with one that makes the program report a score as legal if it is between -50 and 50 (inclusive) or if it is an even number either inside or outside of this range. If it is legal, print a message that says the score is legal. Otherwise, print a message that says the score is illegal. Include the score value in each of your output messages.

if-else-if

The following code is intended to print 'F' for a score under 60, 'D' for a score 60-69, 'C' for a score 70-79, 'B' for a score 80-89 and 'A' for a score 90 or above:
        double score;
        String letterScore;
        ...

        if (score >= 60)
	    letterScore = "D";
        else if (score >=70)
	    letterScore = "C";
        else if (score >= 80)
	    letterScore = "B";
        else
	    letterScore = "A";
What will letterScorebe assigned if the value of score is 50? 87? 90?

Go into the subdirectory Elsey and run the class Demo to see if your predicted results agree with the actual results.

Prepare verbal answers to these questions:

  1. Why doesn't the code work as expected?
  2. How would you go about correcting this code so that the correct letter grade is determined for each input value?
2 Tell us the answers to the questions.

Nested ifs

Time for an aside: One tried and true method for writing algorithms is by using pseudocode. Pseudocode lays out the logic of a solution to a programming problem without using the syntax of any particular programming language. Here is an example of an algorithm for printing the smallest of three different integers.

      get values for integers i1, i2, i3
      set min to the smallest of i1, i2, and i3
      print min
Go into the subdirectory Min and edit the class Demo. Notice that the code is already implemented that gets values for the three integers.

Based on the pseudocode given above, write Java code to determine the smallest value min of the three input values. Your code should use nested if-else statements and should only assign a value to min once.

3 Show us your completed and tested code.

How does your code "scale up"? What we mean by this is, if you had four input values, or five, or one hundred, would your code be easy to modify to handle all these values?

One algorithm for finding min, the smallest of a number of values, can be given as:

  1. set min to the first value.
  2. if min is greater than the second value, set min to this second value
  3. If min is greater than the third value, set min to this third value
  4. and so on...
This algorithm will work no matter how many values you have to examine. If there are only three values, just do the first three steps.

Replace the code your wrote for the previous checkpoint with new code that implements this algorithm. Remember that you have only three values to examine.

4 Show us your completed and tested code.

What Belongs In the If?

Go into the subdirectory Hmmmmm.

Compile and run this program with several input values of your choice, both positive and negative. This program is intended to take an input value and negate it (make it positive) if it is negative but otherwise leave it alone. Fix the program so that it works correctly.

5 Explain what was wrong with the original program, and how you corrected the problem.

Close project Hmmmmm.

The Switch Statement

Go into the subdirectory CircleColor. Edit the class CircleColor. This program draws a black circle and then prompts the user to enter a number selecting a new circle color. The current program always changes the color to cyan (a light blue). See how this works by running the program.

Add code to CircleColor so that the color selected by the user is the one used to draw the circle instead of always drawing it in cyan. You will need to add a switch statement to reset the color variable (legal color names are: Color.red, Color.green, Color.yellow, and Color.blue.

6 Show us your completed and tested program.

A Final Program

Go into subdirectory Triangle and edit the Demo class. This program accepts three numbers (ordered smallest to largest) and prints a message describing the type of triangle that would be produced if these numbers represent the lengths of the sides. It first checks that each number is positive. Then it uses the Triangle Inequality to check if the lengths can be the sides of a real triangle: three numbers can represent the sides of a triangle if the sum of the length of the two shorter sides is greater than the length of the longest side.

A problem with the program is that it isn't checking to make sure that the user followed our directions when inputting the side lengths; at the moment, it is only assuming that the user did as instructed. After the input statements, write code to check that the sides have been entered in the expected order (smallest to largest).

7 Show us your code and running program.

Comparing ints and objects

Go into the subdirectory OurPoint and open the class Demo. OurPoint is the same class that we used in Lab04. You may want to review it first.

Examine the code for the Demo class and predict the output. Now run the program and observe your output. Draw a series of state-of-memory diagrams to explain what happens.

8 Call us over when you are ready with your explanation.

Write an equals() method for the OurPoint class. This should return true if the two OurPoint objects have the same x- and y- coordinates. For example, for the code segment

        p = new OurPoint (2,3);
        q = new OurPoint (2,3);

        if (p.equals(q))
	    System.out.println("(1)The points are equal");
        else
	    System.out.println("(1)The points are NOT equal");

the output should be "(1)The points are equal". Make the above change to the Demo.java file and compile, run, and examine your results.

9 Call us over to show us your modification.

After the Lab

Don't forget to exit Netscape before you log out.

10 Show us that you have logged out, cleaned up, and pushed in your chairs for this last checkpoint.

End of Lab


Susan Haller and Timothy Fossum, University of Wisconsin-Parkside