1 of 24

if / else if / else

I can use branching code to solve problems conditionally

Remember, to sit at the front white tables to start the class!

2 of 24

Object Oriented Programming

A program is a collection of objects that solve a problem by interacting with each other.

3 of 24

Objects

Java programs are made up of things.

Things are called Objects

Objects have behavior (what they can do) and state (what they know)

What Objects can you identify in this screenshot from Super Mario Bros?

What behavior do they have?

What state do they have?

4 of 24

Behavior and State

Mario is an Object

Behavior

Move left

Move right

Jump

Duck

Grow

Shrink

State

What direction is he facing

Is he jumping

Is he falling

Is he ducking

Is he big or small

What are his (x, y) coordinates

5 of 24

Behavior and State

Goomba is an Object

Behavior

Move left

Move right

Stomp

State

What direction is it facing

Is it squished

Is it falling

What are its (x, y) coordinates

6 of 24

Behavior and State

Question Blocks are Objects

Behavior

Smash

State

What are its (x, y) coordinates

What is inside it

Has it already been smashed

Is it invisible

7 of 24

Objects are represented by Classes

Behavior

Methods represent an Object’s behavior

State

Instance Variables represent an Object’s state

public class Mario

{

private int x;

private int y;

private boolean facingLeft;

private boolean isBig;

private boolean hasFirePower;

public void move(boolean left)

{ /* implementation not shown */ }

public void jump()

{ /* implementation not shown */ }

public void grow()

{ /* implementation not shown */ }

}

8 of 24

Creating Objects

Mario player;

player = new Mario();

Mario player2 = new Mario();

The player variable has a reference to this Mario object

The player2 variable has a reference to this Mario object

The new keyword is used to create new Objects from a Class

9 of 24

Invoking Behavior

Mario player = new Mario();

player.jump();

Dot notation

Argument List

10 of 24

Types of Methods

Getter Methods

int x = player.getX();

int y = player.getY();

Setter Methods

player.setLocation(75, 25);

Mutator Methods

player.move(25);

player.turn(60);

arguments

11 of 24

Constructors

Constructors are a special type of method

Constructors are called only when an Object is first created

Mario player = new Mario();

Constructors initialize an object’s state.

They tell the object where it is and what it is doing when it is first created.

Invoking the constructor

12 of 24

Mayflower

Mayflower is a Java Library that lets you build graphical programs.

Actor

setImage(String img)

setLocation(int x, int y)

move(int amnt)

turn(int amnt)

act()

getX()

getY()

World

setBackground(String img)

addObject(Actor a)

removeObject(Actor a)

update()

Mayflower

isKeyDown(int key)

A World contains actors. Actors can move around the screen and update their state in response to keyboard events.

13 of 24

Logic

Boolean Expressions evaluate to true or false

It is okay for a Boolean Expression to be false.

1 == 2 is a perfectly fine boolean expression that evaluates to false

Complete the Logic Questions in your packet

14 of 24

Branching

Some Code

Yes / No Question

Do this code if answer is yes

Do this code no matter what

yes

no

// some code goes here

if( )

{

// do this if true

}

// do this no matter what

BOOLEAN EXPRESSION

15 of 24

Branching

Some Code

Yes / No Question

Do this code if answer is yes

Do this code no matter what

yes

no

// some code goes here

if( x < 10 )

{

// do this if true

}

// do this no matter what

16 of 24

Else

Some Code

Yes / No Question

Do this code if answer is yes

Do this code no matter what

yes

no

// some code goes here

if( x < 10 )

{

// do this if true

}

else

{

// do this if false

}

// do this no matter what

Do this code if answer is no

17 of 24

Else if

if( x < 10 )

{

System.out.println(“A”);

}

else if( x < 100 )

{

System.out.println(“B”);

}

else

{

System.out.println(“C”);

}

What outputs if x = 500 ?

18 of 24

Else if

if( x < 10 )

{

System.out.println(“A”);

}

else if( x < 100 )

{

System.out.println(“B”);

}

else

{

System.out.println(“C”);

}

What outputs if x = 500 ?

C

19 of 24

Else if

if( x < 10 )

{

System.out.println(“A”);

}

else if( x < 100 )

{

System.out.println(“B”);

}

else

{

System.out.println(“C”);

}

What outputs if x = 50 ?

20 of 24

Else if

if( x < 10 )

{

System.out.println(“A”);

}

else if( x < 100 )

{

System.out.println(“B”);

}

else

{

System.out.println(“C”);

}

What outputs if x = 50 ?

B

21 of 24

Else if

if( x < 10 )

{

System.out.println(“A”);

}

else if( x < 100 )

{

System.out.println(“B”);

}

else

{

System.out.println(“C”);

}

What outputs if x = 5 ?

22 of 24

Else if

if( x < 10 )

{

System.out.println(“A”);

}

else if( x < 100 )

{

System.out.println(“B”);

}

else

{

System.out.println(“C”);

}

What outputs if x = 5 ?

A

Only the first true condition will execute!

23 of 24

Keyboard Methods in Mayflower (are Boolean Expressions)

Mayflower.isKeyDown( Keyboard.KEY_UP )

Mayflower.isKeyDown( Keyboard.KEY_SPACE )

Mayflower.isKeyDown( Keyboard.KEY_W )

Mayflower.isKeyDown( Keyboard.KEY_7 )

24 of 24

What’s next

The goal of Computer Science labs is not the final product. The journey is the goal.

Do not rush to “finish.” Take your time. Read all of the words in the instructions. If you have a question, ask.

If you “finished” but you don’t know how the program works or why it works that way, then you should reread the code and the instructions, or you should ask questions.

Impress me by using the skills you learned in the lab to add additional feature to the finished lab. Enhance and expand!

  1. Do Branching Q’s in packet

  • Get packet checked

  • If worksheet is correct, start lab�Otherwise, go back to step 1