Week 6 Discussion Wednesday, 11/6/19 Reminders PSA4 Submission due - - PowerPoint PPT Presentation

week 6 discussion
SMART_READER_LITE
LIVE PREVIEW

Week 6 Discussion Wednesday, 11/6/19 Reminders PSA4 Submission due - - PowerPoint PPT Presentation

Week 6 Discussion Wednesday, 11/6/19 Reminders PSA4 Submission due Tuesday, November 12 11:59pm For students that are missing discussion participation and you clicked in, we will handle it all at once at the end of the quarter. Extra credit:


slide-1
SLIDE 1

Week 6 Discussion

Wednesday, 11/6/19

slide-2
SLIDE 2

Reminders

PSA4 Submission due Tuesday, November 12 11:59pm For students that are missing discussion participation and you clicked in, we will handle it all at once at the end of the quarter. Extra credit: Beating the raccoon, but fix your code first!

slide-3
SLIDE 3
  • Inheritance Review
  • Overview of the PA
  • Game mechanics
  • Critters you have to make
  • Inheritance Practice

Today’s agenda

slide-4
SLIDE 4

PSA4 Overview

  • Critter
slide-5
SLIDE 5

Overriding

  • eat() getColor()

getAttack() Critter

  • Critter

Critter

  • Critter
slide-6
SLIDE 6

Critter World

  • X: 0, Y: 0
slide-7
SLIDE 7

Meet the Methods

slide-8
SLIDE 8

Movement

slide-9
SLIDE 9

Movement

  • Critter

getMove

  • Critter

North South West East Center

slide-10
SLIDE 10

Eating

slide-11
SLIDE 11

Eating

  • Critter
  • Critter
  • Critter
slide-12
SLIDE 12

Fighting

slide-13
SLIDE 13

Fighting

  • Critter

ROAR POUNCE SCRATCH

  • ROAR

SCRATCH SCRATCH POUNCE POUNCE ROAR ○

  • FORFEIT

FORFEIT

slide-14
SLIDE 14

Mating

slide-15
SLIDE 15
  • Critter
  • Mating
slide-16
SLIDE 16
  • Critter

Critter Critter

Scoring

slide-17
SLIDE 17

Meet the Critters

slide-18
SLIDE 18
  • Critter.java
slide-19
SLIDE 19
  • FORFEIT

Starfish extends Critter

slide-20
SLIDE 20
  • WEST
  • eat()

Critter

  • ROAR

FORFEIT ○

Turtle extends Critter

slide-21
SLIDE 21
  • eat

  • POUNCE

Feline extends Critter

slide-22
SLIDE 22
  • Overrides

Feline getAttack()

  • eat

Lion extends Feline

slide-23
SLIDE 23
  • Lion
  • Leopard

  • confidence
  • Leopard extends Feline
slide-24
SLIDE 24
  • verride getColor()

generateAttack()

  • Override generateAttack()
  • getAttack()

Ocelot extends Leopard

slide-25
SLIDE 25
  • goalX

goalY ○ Elephant

  • getMove()

○ Elephant Elephant

  • Elephant

getMove()

Elephant extends Critter

slide-26
SLIDE 26
  • Critter

Critter Critter

  • buffBehavior()

Critter

  • debuff()

Critter

  • buffBehavior() & debuff()
slide-27
SLIDE 27
  • Lion
  • Lion's Buff Effect
slide-28
SLIDE 28
  • Leopard
  • Leopard's Buff Effect
slide-29
SLIDE 29
  • Leopard
  • Ocelot's Buff Effect
slide-30
SLIDE 30

Starfish's Buff Effect

slide-31
SLIDE 31
  • void teleport(Point currentLocation,

Critter[][] arena) ○ Starfish ■ ○ Starfish null

Starfish's Teleportation

slide-32
SLIDE 32

More Inheritance Practice Problems

slide-33
SLIDE 33

public class Motorcycle extends Vehicle { @Override public void startEngine() { System.out.println("ZOOM"); } } public static void main(String[] args) { Vehicle v = new Vehicle(); v.startEngine(); Vehicle c = new Car(); c.startEngine(); Motorcycle m = new Motorcycle(); m.startEngine(); } // What is the output? public class Vehicle { String name; int year; public void startEngine() { System.out.println("Engine"); } public String getName() { return name; } } public class Car extends Vehicle { @Override public void startEngine() { System.out.println("VROOM"); } }

slide-34
SLIDE 34

public class Motorcycle extends Vehicle { @Override public void startEngine() { System.out.println("ZOOM"); } } public static void main(String[] args) { Vehicle v = new Vehicle(); v.startEngine(); Vehicle c = new Car(); c.startEngine(); Motorcycle m = new Motorcycle(); m.startEngine(); } // What is the output? Engine VROOM ZOOM public class Vehicle { String name; int year; public void startEngine() { System.out.println("Engine"); } public String getName() { return name; } } public class Car extends Vehicle { @Override public void startEngine() { System.out.println("VROOM"); } }

slide-35
SLIDE 35

Inheritance Practice 2

public class Vehicle { public void startEngine() { System.out.println("Starting Engine"); } } public class Car extends Vehicle { public void startEngine(int x) { System.out.println("Car " + x); } } public static void main(String[] args) { Vehicle c = new Car(); c.startEngine(); }

What gets printed?

A: Starting Engine B: Car 0 C: compiler error

slide-36
SLIDE 36

Inheritance Practice 2

This gets printed.

A: Starting Engine B: Car 0 C: compiler error public class Vehicle { public void startEngine() { System.out.println("Starting Engine"); } } public class Car extends Vehicle { public void startEngine(int x) { System.out.println("Car " + x); } } public static void main(String[] args) { Vehicle c = new Car(); c.startEngine(); }

slide-37
SLIDE 37

Inheritance Practice 3

public class Vehicle { public void startEngine() { System.out.println("Starting Engine"); } } public class Car extends Vehicle { public void startEngine(int x) { System.out.println("Car " + x); } } public static void main(String[] args) { Vehicle c = new Car(); c.startEngine(1); }

What gets printed?

A: Starting Engine B: Car 1 C: compiler error

slide-38
SLIDE 38

Inheritance Practice 3

This gets printed.

A: Starting Engine B: Car 1 C: compiler error public class Vehicle { public void startEngine() { System.out.println("Starting Engine"); } } public class Car extends Vehicle { public void startEngine(int x) { System.out.println("Car " + x); } } public static void main(String[] args) { Vehicle c = new Car(); c.startEngine(1); }

slide-39
SLIDE 39

Inheritance Practice 4

public class Vehicle { public void startEngine() { System.out.println("Starting Engine"); } } public class Car extends Vehicle { public void startEngine(int x) { System.out.println("Car " + x); } } public static void main(String[] args) { Car c = new Car(); c.startEngine(1); }

What gets printed?

A: Starting Engine B: Car 1 C: compiler error

slide-40
SLIDE 40

Inheritance Practice 4

This gets printed.

A: Starting Engine B: Car 1 C: compiler error public class Vehicle { public void startEngine() { System.out.println("Starting Engine"); } } public class Car extends Vehicle { public void startEngine(int x) { System.out.println("Car " + x); } } public static void main(String[] args) { Car c = new Car(); c.startEngine(1); }

slide-41
SLIDE 41

// Inheritance Constructor Example 1

public class Dog { public String name; public Dog() { name = "Dog"; } } public class Husky extends Dog { public Husky() { name = "Husky"; } } // in main() Dog sydney = new Husky(); System.out.println( sydney.name);

slide-42
SLIDE 42

// Inheritance Constructor Example 1

public class Dog { public String name; public Dog() { name = "Dog"; } } public class Husky extends Dog { public Husky() { name = "Husky"; } } // in main() Dog sydney = new Husky(); System.out.println( sydney.name); Answer: Husky

slide-43
SLIDE 43

// Inheritance Constructor Example 2

public class Dog { public String name; public Dog() { name = "Dog"; } } public class Husky extends Dog { public Husky() { // do nothing } } // in main Dog sydney = new Husky(); Husky cindy = new Husky(); System.out.println( sydney.name + " " + cindy.name);

slide-44
SLIDE 44

// Inheritance Constructor Example 2

public class Dog { public String name; public Dog() { name = "Dog"; } } public class Husky extends Dog { public Husky() { // do nothing call super() by default } } // in main Dog sydney = new Husky(); Husky cindy = new Husky(); System.out.println( sydney.name + " " + cindy.name); Answer: Dog Dog

slide-45
SLIDE 45

// Inheritance Constructor Example 3

public class Dog { public String name; public Dog() { name = "Dog"; } public Dog(String name) { this.name = name; } } // in main Dog sydney = new Husky(); Husky cindy = new Husky("husky"); System.out.println( sydney.name + " " + cindy.name); public class Husky extends Dog { public Husky() { // do nothing } public Husky(String name) { // do nothing } }

slide-46
SLIDE 46

// Inheritance Constructor Example 3

public class Dog { public String name; public Dog() { name = "Dog"; } public Dog(String name) { this.name = name; } } // in main Dog sydney = new Husky(); Husky cindy = new Husky("husky"); System.out.println( sydney.name + " " + cindy.name); Answer: Dog Dog public class Husky extends Dog { public Husky() { // do nothing } public Husky(String name) { // do nothing } }

slide-47
SLIDE 47

// Inheritance Constructor Example 4

public class Dog { public String name; public Dog() { name = "Dog"; } public Dog(String name) { this.name = name; } } // in main Dog sydney = new Husky(); Husky cindy = new Husky("Husky"); System.out.println( sydney.name + " " + cindy.name); public class Husky extends Dog { public Husky() { // do nothing } public Husky(String name) { super(name); } }

slide-48
SLIDE 48

// Inheritance Constructor Example 4

public class Dog { public String name; public Dog() { name = "Dog"; } public Dog(String name) { this.name = name; } } // in main Dog sydney = new Husky(); Husky cindy = new Husky("Husky"); System.out.println( sydney.name + " " + cindy.name); Answer: Dog Husky public class Husky extends Dog { public Husky() { // do nothing } public Husky(String name) { super(name); // super() not called } }

slide-49
SLIDE 49

Inheritance Practice 5

public class Vehicle { public Vehicle(int x) { System.out.println("NEW VEHICLE " + x); } } public class Plane extends Vehicle { public Plane() { super(1); System.out.println("NEW PLANE"); } } public static void main(String[] args) { Vehicle v = new Vehicle(2); Plane c = new Plane(); }

What gets printed?

A: NEW VEHICLE 2 NEW PLANE B: NEW VEHICLE 2 NEW VEHICLE 1 NEW PLANE C: compiler error

slide-50
SLIDE 50

This gets printed.

A: NEW VEHICLE 2 NEW PLANE B: NEW VEHICLE 2 NEW VEHICLE 1 NEW PLANE C: compiler error

Inheritance Practice 5

public class Vehicle { public Vehicle(int x) { System.out.println("NEW VEHICLE " + x); } } public class Plane extends Vehicle { public Plane() { super(1); System.out.println("NEW PLANE"); } } public static void main(String[] args) { Vehicle v = new Vehicle(2); Plane c = new Plane(); }

slide-51
SLIDE 51

Inheritance Practice 6

public class Vehicle { public Vehicle(int x) { System.out.println("NEW VEHICLE " + x); } } public class Plane extends Vehicle { public Plane() { super(1); System.out.println("NEW PLANE"); } } public static void main(String[] args) { Vehicle v = new Vehicle(2); Plane c = new Plane(3); }

What gets printed?

A: NEW VEHICLE 2 NEW VEHICLE 1 NEW PLANE B: NEW VEHICLE 2 NEW VEHICLE 3 NEW PLANE C: compiler error

slide-52
SLIDE 52

This gets printed.

A: NEW VEHICLE 2 NEW VEHICLE 1 NEW PLANE B: NEW VEHICLE 2 NEW VEHICLE 3 NEW PLANE C: compiler error

  • We don’t inherit the constructor method-

Inheritance Practice 6

public class Vehicle { public Vehicle(int x) { System.out.println("NEW VEHICLE " + x); } } public class Plane extends Vehicle { public Plane() { super(1); System.out.println("NEW PLANE"); } } public static void main(String[] args) { Vehicle v = new Vehicle(2); Plane c = new Plane(3); }

slide-53
SLIDE 53

Inheritance Practice 7

public class Vehicle { public Vehicle(int x) { System.out.println("NEW VEHICLE " + x); } } public class Plane extends Vehicle { public Plane() { System.out.println("NEW PLANE"); } } public static void main(String[] args) { Vehicle v = new Vehicle(2); Plane c = new Plane(); }

What gets printed?

A: NEW VEHICLE 2 NEW PLANE B: NEW VEHICLE 2 NEW VEHICLE 0 NEW PLANE C: compiler error

slide-54
SLIDE 54

This gets printed.

A: NEW VEHICLE 2 NEW PLANE B: NEW VEHICLE 2 NEW VEHICLE 0 NEW PLANE C: compiler error

Inheritance Practice 7

public class Vehicle { public Vehicle(int x) { System.out.println("NEW VEHICLE " + x); } } public class Plane extends Vehicle { public Plane() { super(); // why? System.out.println("NEW PLANE"); } } public static void main(String[] args) { Vehicle v = new Vehicle(2); Plane c = new Plane(); }

slide-55
SLIDE 55

public class Vehicle { public Vehicle() { System.out.println("NEW VEHICLE"); } } public class Plane extends Vehicle { public Plane() { this(1); System.out.println("YAY"); } public Plane(int x) { System.out.println("NEW PLANE"); } } public static void main(String[] args) { Vehicle v = new Vehicle(); Plane c = new Plane(); }

What gets printed?

A: NEW VEHICLE NEW PLANE YAY B: NEW VEHICLE NEW VEHICLE NEW PLANE YAY C: NEW VEHICLE NEW VEHICLE NEW VEHICLE NEW PLANE YAY

Inheritance Practice 8

slide-56
SLIDE 56

This gets printed.

A: NEW VEHICLE NEW PLANE YAY B: NEW VEHICLE NEW VEHICLE NEW PLANE YAY C: NEW VEHICLE NEW VEHICLE NEW VEHICLE NEW PLANE YAY

public class Vehicle { public Vehicle() { System.out.println("NEW VEHICLE"); } } public class Plane extends Vehicle { public Plane() { this(1); System.out.println("YAY"); } public Plane(int x) { System.out.println("NEW PLANE"); } } public static void main(String[] args) { Vehicle v = new Vehicle(); Plane c = new Plane(); }

Inheritance Practice 8