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!
Object Oriented Programming
A program is a collection of objects that solve a problem by interacting with each other.
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?
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 |
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 |
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 |
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 */ }
}
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
Invoking Behavior
Mario player = new Mario();
player.jump();
Dot notation
Argument List
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
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
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.
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
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
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
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
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 ?
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
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 ?
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
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 ?
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!
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 )
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!