Conditional Execution Assignment: Selection Control Structures

advertisement
Conditional Execution Assignment: Selection Control Structures
Java Version: 1.7-1.8
Game Version: SpaceSmasher 1.0
Authors: Kelvin Sung, Mike Panitz, Rob Nash, and ET. AL.
Prerequisites
You must have completed both the Setting Up Java Lab and Setting Up
SpaceSmasher Lab to start the first Assignment.
Summary
The focus of this assignment is to demonstrate your understanding of conditional
execution (also called branching) in our SpaceSmasher sandbox. There are three
levels to this assignment, and all students must complete the first level for credit;
the next levels are more challenging, optional, and must be completed in order like a
dungeon.
Assignment Outcomes
Demonstrate proficient use of the Selection Control structures in Java in the context
of a real-world, industry grade software simulation. The different forms of the IF
Control Structure you should use are highlighted here:
 The Single If
if( <someBooleanExpression> ) {…}
 A Set of Sequential If Statements
if( <someBooleanExpression> ) {…}
if( <someBooleanExpression> ) {…}
if( <someBooleanExpression> ) {…}
 Compound Boolean Conditions (using && and ||)
if( <booleanExpressionA> && <booleanExpressionB> ) {..}
if( <booleanExpressionA> || <booleanExpressionB> ) {..}
 The Single If/Else Structure (With Default Else)
if( <someBooleanExpressionA> ) {
//codeBodyA
} else ( <someBooleanExpressionB> ) {
//codeBodyB
}
 Chained If/ElseIF Statements Without Default Else
if( <queryA> ) {
//codeBodyA
} else if( <queryB> {
//codeBodyB
}
 Chained If/ElseIF Statements With Default Else
if( <queryA> ) {
//codeBodyA
} else if( <queryB> {
//codeBodyB
} else {
//defaultCodeBodyC
}
Warmup with the Single If Statement
Given the following variable declarations and main(), write IF statements that
correspond with the questions below. Compile and execute your code, and submit
both the code you wrote and the console output it produces when you are done.
1. Report to the console if the variable called “number” is negative using
System.out.println().
2. Report if the number is zero next.
3. Print out “the number is even” if the “number” variable holds an even
value, else print out “the number is odd”.
4. Print to the console “The grade is above an 89” if it is, in fact, an A.
5. Describe the current temperature as “higher than 78 degrees” or “less
than or equal to 78 degrees”
6. Print to the console if the class average was below a 65
7. Let the user know if the value in the variable “answer” is true
8. Next, report if the value in “answer” is not true
9. Output to the console if the value in “grade” was above 83 AND less
than 90
10. Is the number (positive AND odd) OR (zero AND even)? If it is, say
“yes” and if it’s not, say “no” on the console.
public static void main(String[] args) {
int number = -1;
//TODO 1
number = 0;
//TODO 2
number = 2;
//TODO 3
double grade = 89.5;
//TODO 4
double temperature = 79.2;
//TODO 5
double average = 70;
//TODO 6
boolean answer = false;
//TODO 7
//TODO 8
grade = 85.5;
//TODO 9
number = 50;
//TODO 10
}
SpaceSmasher Level 1 Assignment
In this part, we’ll complete the first homework level by enabling the paddle to move
up and down in addition to the left and right movements already implemented in
the lab. In more advanced versions of this level, we could watch out for paddle and
block collisions, but for now, the paddle will move right on through the blocks and
still produce a bounce effect on the ball when colliding with the ball. To accomplish
this behavior, you’ll need to use one of the IF or IF/ELSE structures covered in the
lab or the slides. Provided for you is an animated gif that displays how our new
game would play when completed.
INSERT GIF HERE
SpaceSmasher Level 2 Assignment
Prerequisites
This level of assignment is inaccessible until level one has been completed.
Summary
In level one, you played the block breaker game from “left-to-right” where you lose a
life at the bottom of the screen. For level two, rotate your head 90 degrees
clockwise and you have the next (much harder) SpaceSmasher version. In this
version, your paddle will be on the left side of the screen and move only up and
down to defend your life (or lives). You’ll guard the leftmost part of the screen from
the ball, since you’ll lose a life if the ball hits that edge of the screen rather than the
bottom. Look at the included animation to see this radically altered version in
action.
ADD GIF HERE
SpaceSmasher Level 3 Assignment
Prerequisites
This level of assignment is inaccessible until level two has been completed.
Summary
This level offers the student a choice. Modify the SpaceSmasher game in any
meaningful way to obtain credit here. For example, add a score, power-up, or
different sounds, as long as the IF or IF/ELSE structures are used.
ADD GIF HERE
SpaceSmasher Level 4 Assignment
Prerequisites
This level of assignment is inaccessible until level two has been completed; it is not a
requirement that level three be completed before approaching level four.
Summary
Extend SpaceSmasher to be 2 players, similar in spirit to a game of Pong (search
term: Nolan Bushnell). The first player will operate the paddle down south as usual,
but a second player using the keys {W,S,A,D} will operate a second paddle at the top
of the screen. The shortest version of this has the players sharing lives and working
cooperatively to clear the board of blocks, but longer competitive versions could also
be designed here. Look at the animated gif for an example implementation of this in
action.
ADD GIF HERE
END OF ASSIGNMENT
Sequential If Statements (add flowchart)
If Else (add flowchart)
If/Else Chains with no Default Else
If/Else Chains with Default Else
Download