Java

OOP GUIDE / WORK

 

BADGUY SUPERCLASS

 

Topics

  • Intro to Inheritance
  • Using extends
  • Superconstructors
  • Testing inheritance

 

 

TASK – PART 1 – SETUP & EXAMINATION – THE BADGUY CLASS

 

Copy and paste the following BadGuy and BadGuyTester classes.  Examine them to make sure you understand how they work.

 

 

public class BadGuy

{

     public int hp;

     public int maxHp;

     public String name;

    

     public BadGuy(String n)

     {

           hp = 1;

           maxHp = 1;

           name = n;

     }

    

     public String toString()

     {

           return "This badguy (" + name + ") is bad!";

     }

}

 

 

 

public class BadGuyTester

{

     public static void main(String[] args)

     {

           BadGuy bg = new BadGuy("Henry");

           System.out.println(bg);

     }

}

 

 

TASK – PART 2 – EXTENDING BADGUY

 

The BadGuy class creates a very generic BadGuy object.  We now want to create a more specific type of bad guy class that has all of the features and characteristics of the generic BadGuy.

 

Create a Goomba class!

 

          Instant download / Nintendo Svg, Goomba Game Mario, Super, Brothers, Bros svg, png, Digital Download. Cricut Cut File vector art svg, png

 

Here are the specifications for the class:

 

·       It inherits from the BadGuy class.  (You need to use the extends keyword on the class header line to do this).

·       It has these four instance variables (plus the ones in BadGuy):

o   public int teeth;

o   public int damage;

o   public boolean stompable;

o   public String colour;

·       It has a constructor that gets a parameter for name and colour.  Teeth are set to two (always), damage is set to 1, stompable is set to true.  Remember that the first line of the constructor should call the superconstructor.

·       It has its own toString method that returns “This badguy is a fungi!”  (very funny stuff)

 

 

TASK – PART 3 – TESTING GOOMBA

 

Inside BadGuyTester, create a Goomba and test all of the methods and instance variables.