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.

  1. Condition: The condition is the expression that the if statement evaluates. If it resolves to true, the associated block of code (the body) is executed.

    • Example: i == 3 checks if i equals 3. If this is true, the body of the if statement runs.
    • If the condition is false, the code inside the body is skipped, and the program checks any subsequent else if or else statements.
  2. Body: This is the block of code inside the curly braces {} that runs when the condition is true.

    • Example: The body of the first if statement is System.out.println("i is 3");.

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 if i == 3 again, but it will never execute because the first if condition already evaluated to true.
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 ;:

  1. Initialization: This is where you define and initialize a loop control variable.

    • Example: int i = 0; sets i to 0 before the loop starts.
  2. Condition: Before each iteration, the loop checks this condition.

    • Example: i < 10 checks if i 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.
  3. Update: After each iteration, the loop updates the control variable.

    • Example: i++ increases i by 1 after every loop iteration.

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

  1. Initialization: You usually initialize a variable before the loop.

    • Example: int i = 0; sets i to 0.
  2. Condition: The loop checks this condition before each iteration.

    • Example: i < 10 checks if i is less than 10.
      • If true, the loop executes.
      • If false, the loop exits.
  3. 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

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