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.
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.
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:
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 minGo 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:
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.
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.
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.
7
Show us your code and running program.
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.