Java Class

Not long ago I learned how to use the Java classes that came by default with it when using the Java API. Now I was assigned to create my own class with its custom methods and what a best way to learn it than by creating a program to describe X dog features….am I right, right?

Creating this type of exercises is kinda cool because it introduces you for the first time to:

  • constructors which are methods/functions that share the same name as the class and is by convention the first method created.
  • said constructors are use to set the initial values from X new object.
  • by initial values I mean instance/global data that are declared at the class level(before your constructor).
  • visibility identifiers such as public, private and protected. There’s another one, final but its mainly used to declare constants.
  • the encapsulation rules to create methods, variables, etc — which is out of the scope of this post.

Before continuing, I’m letting you know that we will have to create two documents with one working as a class and the second one working as a driver in which we will be calling the class methods.

First, we create the class:

//********************************************************************
//  Dog.java       Author: Kevin Uriel Azuara Fonseca
//
//  Write a class called Dog that contains instance data that represents the dog’s name and age.
//  Define the Dog constructor to accept and initialize instance data. Include getter and setter methods for the name and age.
//  Include a method to compute and return the age of the dog in “person years” (seven times the dog’s age).
//  Include a toString method that returns a one-line description of the dog.
//  Create a driver class called Kennel, whose main method instantiates and updates several Dog objects.
//********************************************************************

public class Dog {

    private String dogsName;  // initiate dogsName
    private int dogsAge;  // initiate dogsAge

    //-----------------------------------------------------------------
    //  Constructor: Sets the initial face value.
    //-----------------------------------------------------------------
    public Dog() {
        dogsName = "";
        dogsAge = 0;
    }
    
    //-----------------------------------------------------------------
    //  Verify dogs 'name' is a string
    //-----------------------------------------------------------------
    public void setName(String value){
        dogsName = value;
    }
    
    //-----------------------------------------------------------------
    //  Verify dogs 'age' is an integer
    //-----------------------------------------------------------------
    public void setAge(int value){
        dogsAge = value;
    }

    //-----------------------------------------------------------------
    //  Returns dogs name
    //-----------------------------------------------------------------
    public String getName(){
        return dogsName;
    }
    
    //-----------------------------------------------------------------
    //  Returns dogs age
    //-----------------------------------------------------------------
    public int getAge(){
        return dogsAge;
    }
    
    //-----------------------------------------------------------------
    //  Compute and return the age of the dog in “person years” (seven times the dog’s age).
    //-----------------------------------------------------------------
    public int computeAge(){
        dogsAge *= 7;
        return dogsAge;
    }

    //-----------------------------------------------------------------
    //  toString method that returns a one-line description of the dog.
    //-----------------------------------------------------------------
    public String toString() {

        String result = "The name of the dog is: " + getName() + " and its age is: " + getAge() + " ";
        
        return result;
    }
}

After that, comes the driver which — as mentioned before — will call the class methods:

//********************************************************************
//  Kennel.java       Author: Kevin Uriel Azuara Fonseca
//
//  Write a class called Dog that contains instance data that represents the dog’s name and age.
//  Define the Dog constructor to accept and initialize instance data. Include getter and setter methods for the name and age.
//  Include a method to compute and return the age of the dog in “person years” (seven times the dog’s age).
//  Include a toString method that returns a one-line description of the dog.
//  Create a driver class called Kennel, whose main method instantiates and updates several Dog objects.
//********************************************************************

public class Kennel
{
    //-----------------------------------------------------------------
    //  Create a driver class called Kennel, whose main method instantiates and updates several Dog objects.
    //-----------------------------------------------------------------
    public static void main(String[] args)
    {
        Dog dog1, dog2, dog3, dog4;
        
        dog1 = new Dog();
        dog2 = new Dog();
        dog3 = new Dog();
        dog4 = new Dog();
        
        // First doggo
        dog1.setName("Lucas");
        dog1.setAge(5);
        dog1.computeAge();
        System.out.println(dog1.toString());

        // Second doggo
        dog2.setName("Guts");
        dog2.setAge(3);
        dog2.computeAge();
        System.out.println(dog2.toString());
        
        // Third doggo
        dog3.setName("Griffith must die");
        dog3.setAge(7);
        dog3.computeAge();
        System.out.println(dog3.toString());

        // Fourth doggo
        dog4.setName("Kaska?");
        dog4.setAge(2);
        dog4.computeAge();
        System.out.println(dog4.toString());
        
    }
}

Have you ever though about creating your own Beautiful Grid System in HTML5 & CSS? Click the link and learn how to create it!.

Too easy, right, right, right? — I think I have a bad taste in humor. Don’t forget to share this article if you found it useful.

Bye-Bye 🙂 .

Leave a Reply

Back to Top