1 of 28

While Loops

and the super keyword

2 of 28

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();

3 of 28

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

4 of 28

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();

5 of 28

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

6 of 28

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

7 of 28

Foo

Bar

extends Foo

super

this

this

super

?

8 of 28

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

9 of 28

Conditional Code

Code that only executes if a condition is true.

if( condition )

{

// do this thing

}

10 of 28

Iterating Code

Code that repeats until the condition is false

while( condition )

{

// do this thing

}

While Loop

11 of 28

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)

12 of 28

Example

int i = 3;

int count = 0;

while(i > 0)

{

i--;

count+=i;

}

What is the value of count after this code has executed?

13 of 28

Example

int i = 3;

int count = 0;

while(i > 0)

{

i--;

count+=i;

}

i

count

3

0

true

3

14 of 28

Example

int i = 3;

int count = 0;

while(i > 0)

{

i--;

count+=i;

}

i

count

3

0

2

2

1st Iteration

i--

count+=2

15 of 28

Example

int i = 3;

int count = 0;

while(i > 0)

{

i--;

count+=i;

}

i

count

3

0

2

2

true

2

16 of 28

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

17 of 28

Example

int i = 3;

int count = 0;

while(i > 0)

{

i--;

count+=i;

}

i

count

3

0

2

2

1

3

true

1

18 of 28

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

19 of 28

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

20 of 28

Funsheet!

21 of 28

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!

22 of 28

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

23 of 28

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

24 of 28

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

25 of 28

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

26 of 28

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

27 of 28

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

28 of 28

Lab 2.4