Java

OOP GUIDE / WORK

 

DOG CLASS

 

Topics

  • Introductory structure of a class (without constructors).
  • Accessing instance variables and methods

 

 

TASK – PART 1 – DOG CLASS

 

Create the structure of class named Dog that will represent a dog object.  It has the following specification:

  • It has three characteristics/properties that will be called instance variables.

 

·       public String breed – The breed of the dog.

 

·       public String name – The dog’s name

 

·       public int age – The dog’s age

 

  • It has four behaviours that will be called by using instance methods. 

 

·       public void bark() – This method simply outputs “name is barking” on the screen.

 

·       public void eat() – This method simply outputs “name is eating” on the screen.

·       public void sleep() – This method simply outputs “name is sleeping” on the screen.

·       public void fetch() – This method simply outputs “name is fetching” on the screen.

 

TASK – PART 2 – TESTING THE DOG CLASS

 

Create another class called DogTester and follow the following steps:

 

  • Add a main function to the DogTester class.

  • Inside main, Create a Dog object (using the built-in constructor).

  • Set the dog’s three instance variables.

  • Make the dog object bark, eat, sleep and fetch to understand how we can call instance methods.