Java

OOP GUIDE / WORK

 

DOG CLASS

 

Topics

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

 

 

TASK – PART 1 – DOG CLASS

 

Solution:

 

 

public class Dog

{

     public String breed;

     public String name;

     public int age;

    

     public void bark()

     {

           System.out.println(name + " is barking.");

     }

    

     public void eat()

     {

           System.out.println(name + " is eating.");

     }

    

     public void sleep()

     {

           System.out.println(name + " is sleeping.");

     }

    

     public void fetch()

     {

           System.out.println(name + " is fetching.");

     }

}

 



 

TASK – PART 2 – TESTING THE DOG CLASS

 

Solution:

 

 

public class DogTester

{

     public static void main(String[] args)

     {

           Dog d = new Dog();

           d.name = "Fido";

           d.breed = "pug";

           d.age = 5;

          

           d.sleep();

           d.eat();

           d.sleep();

          

           Dog d2 = new Dog();

           d2.sleep();

           d.fetch();

           d.bark();

     }

}