Create a Dog Class and Tester

Hello I was asked to create the following:
Create a Dog class that keeps track of a dog's

  1. name

  2. age

  3. Weight

Have the following methods available to use:

1. getName(): String
2. setName(String): void
3. getAge():int
4. getDogYears():int (assume 1 human year = 5 dog years)
5. addAYear():void
6. setAge(int):void
7. getWeight(): double
8. setWeight(double): void
9. getDogData():String

Create a DogTester class to test each of the methods above

I am following a template of this done before but I am getting confused, this is what I have so far Dog class:

public class Dog
{
    //Instance vars
    private String name;
    private int age;
    private double weight;

    //constructor
    public Dog(String initName)
    {
        name = initName;
        age = 0;
        weight = 0;
    }

    //methods
    public String getName()
    {
        return name;
    }

    public void setName()
    {
        return name;
    }

    public int getAge()
    {
        return age;
    }

    public int getDogYears()
    {
        return age*5;
    }

    public void addAYear()
    {
        age = age + 1;
    }

    public void setAge()
    {
        return age;
    }

    public double getWeight()
    {
        return weight;
    }

    public void setWeight()
    {
        weight = weight;
    }

    public String getData()
    {
        String data = "";
        data = name;
        data = data + "\n\tAge: " + age;
        data = data + "\n\tWeight: " + weight;


        return data;
    }
}

Tester:

import java.util.Scanner;

public class DogTester
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner( System.in );

        //Create two dogs to keep track of
        Dog s1 = new Dog("Balto");

        //get the dogs age
        System.out.print("Enter the dog's age: "); //prompt
        double grade = in.nextDouble();             //read
        s1.addAge( years );                     //store

        //get the dogs weight
        System.out.print("Enter the dog's weight: "); //prompt
        s1.addWeight( in.nextDouble() );                //read AND store

        //output their data
        System.out.println( s1.getData() );

    }
}

I am confused as what it would want from the public void constructors and also as to how to set up a tester for these

3

2 Answers

A constructor is not "void", nor does it really "return" anything. When you do:

new Dog("Balto");

This allocates space for a Dog object and calls the constructor to initialize it.

To test the constructor, all you really need to do is test to make sure that the constructor is behaving as it should. Define some clear rules for the state the constructor needs to leave a Dog in (these are commonly called "postconditions"). In your case, looking at the code, it seems that for a new Dog:

  • The name must be set to the parameter passed to the constructor.
  • The age and weight are initialized to 0.

So all you need to do then is, after constructing a new Dog, verify that those conditions are true. Get the dogs name, see if it is the expected value. Get the age and weight, make sure they are zero. If so, then your constructor stuck to the "contract" you defined, and your test passed.

Your public statement simply states that those methods can be accessed from outside the class. If on the other hand you did not want anyone to change a dog's weight after initially constructing the dog, you would set it to private.

You only have one constructor here, the other statements are methods to change your instance variables:

public Dog(String initName)
{
    name = initName;
    age = 0;
    weight = 0;
}

To test you would need to create a dog and then run some assert statements against it to verify that your methods are doing what you would expect them to:

Dog d = new Dog("Pooch");
assertEquals("The name is incorrect", "Pooch", d.getName());
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest