HW01 - james-bern/CS136 GitHub Wiki
README
Java
Control Flow in Java
Control flow refers to the order in which individual statements, instructions, or function calls are executed or evaluated in a program. In Java, control flow is managed using various loops and conditional statements. We'll cover the three most common ways of managing control flow: if
statements, for
loops, and while
loops.
If Statements
An if
statement allows you to execute a block of code only when a specific condition is met. Here's an example:
int i = 4; // initialize i
if (i == 3) { // check if i is equal to 3
System.out.println("i is 3"); // if i is equal to 3, print this message
} else if (i == 4) { // check if i is equal to 4
System.out.println("i is 4"); // if i is equal to 4, print this message
} else { // runs only if none of the above conditions are true
System.out.println("i is neither 3 nor 4"); // prints this message if no other conditions were met
}
System.out.println("All done.");
In this if-else
block, different messages are printed depending on the value of i
.
How an If Statement Works
An if
statement consists of two main parts: a condition and a body.
-
Condition: The condition is the expression that the
if
statement evaluates. If it resolves totrue
, the associated block of code (the body) is executed.- Example:
i == 3
checks ifi
equals 3. If this istrue
, the body of theif
statement runs. - If the condition is
false
, the code inside the body is skipped, and the program checks any subsequentelse if
orelse
statements.
- Example:
-
Body: This is the block of code inside the curly braces
{}
that runs when the condition istrue
.- Example: The body of the first
if
statement isSystem.out.println("i is 3");
.
- Example: The body of the first
If none of the conditions in an if-else
block are true
, the else
block (if present) is executed as a fallback.
Example with Explanation
Let's break down the example:
int i = 4; // initialize i
if (i == 3) { // check if i is equal to 3 (resolves to false)
System.out.println("i is 3"); // since the condition was false this body is skipped
} else if (i == 4) { // checks if i is equal to 4 (resolves to true)
System.out.println("i is 4"); // since i equals 4 this line runs
} else { // since the second condition was true, this block is skipped
System.out.println("i is neither 3 nor 4"); // this line does not run
}
System.out.println("All done."); // this line runs after the if-else block completes
⚠️ WARNING: In an if-else
block, once a condition evaluates to true
, all following else if
and else
blocks are skipped, even if their conditions are also true
.
- Example: In the following code, the second
else if
condition checks ifi == 3
again, but it will never execute because the firstif
condition already evaluated totrue
.
int i = 3;
if (i == 3) {
System.out.println("wow1"); // this will print because i equals 3
} else if (i == 3) { // this condition is true, but will be skipped
System.out.println("wow2"); // this line will never be reached
}
For Loops
A for
loop allows you to repeat a block of code a specific number of times. Here’s an example:
for (int i = 0; i < 10; i++) {
System.out.println(i); // prints the current value of i
}
System.out.println("All done"); // prints when the loop finishes
In this for
loop, the numbers 0 to 9 are printed, followed by "All done."
How a For Loop Works
A for
loop consists of three main parts, separated by semicolons ;
:
-
Initialization: This is where you define and initialize a loop control variable.
- Example:
int i = 0;
setsi
to 0 before the loop starts.
- Example:
-
Condition: Before each iteration, the loop checks this condition.
- Example:
i < 10
checks ifi
is less than 10.- If
true
, the loop body (the code inside the braces{}
) executes. - If
false
, the loop exits, and the program moves on.
- If
- Example:
-
Update: After each iteration, the loop updates the control variable.
- Example:
i++
increasesi
by 1 after every loop iteration.
- Example:
This cycle—checking the condition, executing the body, and updating the variable—continues repeatedly until the condition becomes false
, at which point the loop exits.
⚠️ WARNING: If the condition never becomes false
, the loop will run indefinitely, causing an infinite loop.
While Loops
A while
loop is similar to a for
loop but is more flexible. It keeps executing as long as its condition remains true
. Here’s an example:
int i = 0; // initializes i
while (i < 10) {
System.out.println(i); // prints the current value of i
i++; // increments i
}
System.out.println("All done"); // prints when the loop finishes
In this while
loop, the numbers 0 to 9 are printed, followed by "All done."
How a While Loop Works
-
Initialization: You usually initialize a variable before the loop.
- Example:
int i = 0;
setsi
to 0.
- Example:
-
Condition: The loop checks this condition before each iteration.
- Example:
i < 10
checks ifi
is less than 10.- If
true
, the loop executes. - If
false
, the loop exits.
- If
- Example:
-
Body: The code inside the braces
{}
runs on each iteration.
Just like in a for
loop, this cycle—checking the condition, executing the body, and updating (if necessary)—continues until the condition becomes false
and the loop exits.
⚠️ WARNING: Just like in a for
loop, if the condition never becomes false
, the loop will continue forever.
TODO's
- A-
- draw a circle in the center of the screen (using the COW API)
- have the circle change size when number keys are pressed
- you shouldn't use hard coded values for the keys (remember chars have int values you can use)
- make the circle change color when the mouse is over it (change color only when mouse is over it)
- use COW to get the mouse position
- A
- make the physics box
- draw a square outline around the cursor (it needs to follow the cursor)
- make the circle move using either arrow keys or WASD
- the center of the circle should always be on screen no matter user input
- make the circle move to a random position on screen when it collides with the cursor (use Math.random)
- to prevent flashing the circle should swap between colors
- A+
- if the user holds the mouse the circle should move toward the cursor
- make sure that the circle doesn't move erratically
- if the user holds the mouse the circle should move toward the cursor
Starter Code
class HW01 extends Cow {
// your code here
}
👀 Hints
- if you are looking for how to get things from the cow API it is very helpful to know how to search the file. This is a very important skill as you will often be given code that you did not write and will need to understand and use it yourself.
- on mac press
⌘
+F
and then type the phrase you are looking for (mouse, draw, etc.) - on windows press
ctrl
+F
- I recommend only using functions that don't have an underscore before them as they are built for students to use, only use underscore functions if you feel confident and understand that the app may break
- on mac press