While Loops
and the super keyword
What does the code in the yellow box output?
public class Foo
{
public void fizz()
{
System.out.println(“A”);
}
public void buzz()
{ System.out.println(“C”); }
}
public class Bar extends Foo
{
public void fizz()
{
System.out.println(“B”);
}
public void buzz()
{
fizz();
}
}
Foo b = new Bar();
b.buzz();
What does the code in the yellow box output?
public class Foo
{
public void fizz()
{
System.out.println(“A”);
}
public void buzz()
{ System.out.println(“C”); }
}
Foo b = new Bar();
b.buzz();
public class Bar extends Foo
{
public void fizz()
{
System.out.println(“B”);
}
public void buzz()
{
fizz();
}
}
B
What does the code in the yellow box output?
public class Bar extends Foo
{
public void fizz()
{
System.out.println(“B”);
}
public void buzz()
{
super.fizz();
}
}
public class Foo
{
public void fizz()
{
System.out.println(“A”);
}
public void buzz()
{ System.out.println(“C”); }
}
Foo b = new Bar();
b.buzz();
What does the code in the yellow box output?
public class Foo
{
public void fizz()
{
System.out.println(“A”);
}
public void buzz()
{ System.out.println(“C”); }
}
Foo b = new Bar();
b.buzz();
public class Bar extends Foo
{
public void fizz()
{
System.out.println(“B”);
}
public void buzz()
{
super.fizz();
}
}
A
super is a build-in variable that refers to the super class
public class Foo
{
public void fizz()
{
System.out.println(“A”);
}
public void buzz()
{ System.out.println(“C”); }
}
Foo b = new Bar();
b.buzz();
public class Bar extends Foo
{
public void fizz()
{
System.out.println(“B”);
}
public void buzz()
{
super.fizz();
}
}
A
Foo
Bar
extends Foo
super
this
this
super
?
If a class doesn’t explicitly extend another class, then it implicitly extends the Object class
public class Foo
{
…
public class Foo extends Object
{
…
Object
Foo
Bar
extends Foo
super
this
super
this
Conditional Code
Code that only executes if a condition is true.
if( condition )
{
// do this thing
}
Iterating Code
Code that repeats until the condition is false
while( condition )
{
// do this thing
}
While Loop
Iteration
while ( condition )
{
// do something
}
Boolean Expression
Loop Body
Each time the body executes, it is called an iteration
The body will execute over and over again until the condition is false
(while the condition is true)
Example
int i = 3;
int count = 0;
while(i > 0)
{
i--;
count+=i;
}
What is the value of count after this code has executed?
Example
int i = 3;
int count = 0;
while(i > 0)
{
i--;
count+=i;
}
i | count |
3 | 0 |
| |
| |
| |
true
3
Example
int i = 3;
int count = 0;
while(i > 0)
{
i--;
count+=i;
}
i | count |
3 | 0 |
2 | 2 |
| |
| |
1st Iteration
i--
count+=2
Example
int i = 3;
int count = 0;
while(i > 0)
{
i--;
count+=i;
}
i | count |
3 | 0 |
2 | 2 |
| |
| |
true
2
Example
int i = 3;
int count = 0;
while(i > 0)
{
i--;
count+=i;
}
i | count |
3 | 0 |
2 | 2 |
1 | 3 |
| |
2nd Iteration
i--
count+=1
Example
int i = 3;
int count = 0;
while(i > 0)
{
i--;
count+=i;
}
i | count |
3 | 0 |
2 | 2 |
1 | 3 |
| |
true
1
Example
int i = 3;
int count = 0;
while(i > 0)
{
i--;
count+=i;
}
i | count |
3 | 0 |
2 | 2 |
1 | 3 |
0 | 3 |
3rd Iteration
i--
count+=0
Example
int i = 3;
int count = 0;
while(i > 0)
{
i--;
count+=i;
}
i | count |
3 | 0 |
2 | 2 |
1 | 3 |
0 | 3 |
false
count 🡪 3
0
Funsheet!
Mayflower Collision Detection
public void act()
{
int oldX = getX();
int oldY = getY();
if( …KEY_RIGHT… )
{
setLocation(getX() + 1, getY());� }
if(isTouching(Wall.class))
{
setPosition(oldX, oldY);
}
}
Remember original location
Check for keypresses
Go back if touching a wall
Wall
Actor
Wall
Actor
Wall
Actor
Wall
Actor
touching!
What happens if the actor moves more than 1 pixel?
public void act()
{
int oldX = getX();
int oldY = getY();
if( …KEY_RIGHT… )
{
setLocation(getX() + 5, getY());� }
if(isTouching(Wall.class))
{
setPosition(oldX, oldY);
}
}
Remember original location
Check for keypresses
Go back if touching a wall
Wall
Actor
Wall
Actor
Wall
Actor
Wall
touching!
?
8px
3px
2px
Eww!
public void act()
{
int oldX = getX();
int oldY = getY();
if( …KEY_RIGHT… )
{
setLocation(getX() + 5, getY());� }
if(isTouching(Wall.class))
{
setPosition(oldX, oldY);
}
}
Remember original location
Check for keypresses
Go back if touching a wall
Wall
Actor
Wall
Actor
Wall
Actor
Wall
touching!
Actor
(oldX, oldY)
8px
3px
2px
Funtivity: Rewrite this code
public void act()
{
int oldX = getX();
int oldY = getY();
if( …KEY_RIGHT… )
{
setLocation(getX() + 5, getY());� }
if(isTouching(Wall.class))
{
setPosition(oldX, oldY);
}
}
Remember original location
Check for keypresses
Go back until not touching a wall
Wall
Actor
Wall
Actor
Wall
Actor
Wall
touching!
Actor
8px
3px
2px
Change
This!
GOAL
fixPosition
public void act()
{
int oldX = getX();
int oldY = getY();
if( …KEY_RIGHT… )
{
setLocation(getX() + 5, getY());� }
while(isTouching(Wall.class))
{
setPosition(getX() - 1, getY());
}
}
Don’t need this!
Check for keypresses
Go back until not touching a wall
Wall
Actor
Wall
Actor
Wall
Actor
Wall
touching!
Actor
8px
3px
2px
GOAL
Activity: What do you need to change to make this work with all 4 directions?
public void act()
{
if( …KEY_LEFT… )
{setLocation(getX() - 5, getY());}
if( …KEY_RIGHT… )
{setLocation(getX() + 5, getY());}
if( …KEY_UP… )
{setLocation(getX(), getY() - 5);}
if( …KEY_DOWN… )
{setLocation(getX(), getY() + 5);}
while(isTouching(Wall.class))
{
setPosition(getX() - 1, getY());
}
}
Check for keypresses
Go back until not touching a wall
Wall
Actor
Wall
Actor
Wall
Actor
Wall
touching!
Actor
8px
3px
2px
GOAL
public void act()
{
int dx = 0;
int dy = 0;
if( …KEY_LEFT… )
{ setLocation(getX() - 5, getY());
dx = -1;
}
if( …KEY_RIGHT… )
{ setLocation(getX() + 5, getY());
dx = 1;
}
if( …KEY_UP… )
{ setLocation(getX(), getY() - 5);
dy = -1;
}
if( …KEY_DOWN… )
{ setLocation(getX(), getY() + 5);
dy = 1;
}
while(isTouching(Wall.class))
{
setPosition(getX() - dx, getY() - dy);
}
}
Check for keypresses
And update dx/dy to keep track of direction traveled
Go in opposite direction
Wall
Actor
Wall
Actor
Wall
Actor
Wall
touching!
Actor
8px
3px
2px
GOAL
Keep track of direction traveled
Lab 2.4