Loopy
Loopy

Reputation: 3

Beginner Java OOP

I am developing a simple Dog class to create dog objects and display the results.

Dog
_________

int - size
String - breed;
String - name;

__________

Dog(int, String, String)
bark()
bark(int)
toString(): String
print()

I have made the class successfully using getters and setters but my problem is the toString and print methods. I understand the toString method should return a string with the dog details but don't know how to print them.

I could in theory print it like this:

Dog a = new Dog ();
String details = a.toString();
System.out.println(details);

But this isn't how it's specified in the UML spec.

Should the toString method call the print method itself? and if so, how?

Dog class:

class Dog{
    private int size;
    private String breed;
    private String name;


    //Create getter and setter methods:
    public void setName (String name){
        this.name = name;
    }

    public void setSize (int size){
        this.size = size;
    }

    public void setBreed (String breed){
        this.breed = breed;
    } 

    public int getSize () {
        return this.size;
    }

    public String getBreed () {
        return this.breed;
    }

    public String getName () {
        return this.name;
    }

    public void bark () {
        System.out.println("Ruff! Ruff!");
    }

    public void bark (int amtOfBarks){
        for (int i = 0; i < amtOfBarks; i++){
        System.out.print("Ruff! Ruff! ");
        }
    }


     public String toString () {
        return "Name: "+this.name+"\nBreed: "+this.breed+"\nSize: "+this.size;
    }



}

Main Method:

class TestDog {
    public static void main (String [] args){

        Dog rex = new Dog();
        rex.setName("Rex");
        rex.setBreed("poodle");
        rex.setSize(1);

        rex.toString();



        rex.bark(3);
    }
}

Upvotes: 0

Views: 605

Answers (8)

Mykola Evpak
Mykola Evpak

Reputation: 156

To represent your object as String you have to override toString() method.
It is usually done as below (Eclipse as an example can generate it for you, using fields you will tell)

class Dog {

    private int size;
    private String breed;
    private String name;

    //constructors, methods, getters and setters

    @Override
    public String toString() {
        return "Dog [size=" + size + ", breed=" + breed + ", name=" + name
            + "]";
    }

}

Then you may use your instance object directly in System.out.println() and you will get String representation. You do NOT need to do as you said String details = a.toString(); and only then use it.

    Dog dogInstance = new Dog(21, "Dog Breed", "Dog Name");
    System.out.println(dogInstance);

As a result will be:

Dog [size=21, breed=Dog Breed, name=Dog Name]

Upvotes: 0

user2176576
user2176576

Reputation: 756

Agree with klemenp.

write your toString() method as follows:

@override
public String toString () {
        return "Name: "+this.name+"\nBreed: "+this.breed+"\nSize: "+this.size;
    }

and it will be implicitly called when you print the Object:

Edit to your code :

class TestDog {
    public static void main (String [] args){

        Dog rex = new Dog();
        rex.setName("Rex");
        rex.setBreed("poodle");
        rex.setSize(1);

        //rex.toString();

System.out.println(rex);

        rex.bark(3);
    }
}

Upvotes: 1

klemenp
klemenp

Reputation: 76

The toString() method should not call the print() method. It should be the other way around. Override toString() and call it from the print() method.

@Override
public String toString()
{
    return "details";
}

public void print()
{
    System.out.println(this.toString()); // same as System.out.println(this);
}

Upvotes: 0

Ashay Patil
Ashay Patil

Reputation: 152

in Class Dog write toString() as follows :

@Override
public String toString() {
    return "String describing the details about Dog class";
}

Here @Override annotation is required, because every class has Default toString() method inherited from Object class....

Upvotes: 0

Martijn de Langh
Martijn de Langh

Reputation: 425

Creating a dog in this case is :

Dog a = new Dog (120, "breedname", "name");

In order to print the details you need getters,

in a create a get function like

getBreed(){ return breed; }

in dog, then to print you use:

System.out.println(a.getBreed());

Upvotes: 0

TheLostMind
TheLostMind

Reputation: 36304

From your Class structure diagram, the print() method should call System.out.println(this);.

Note : The toString() will be called implicitly. in System.out.println

Upvotes: 1

stinepike
stinepike

Reputation: 54672

from the doc of toString

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

so to show your own details you have to override this method. a casual example is given here

public String toString(){
    String s = /// add your detials 
    return s;
}

now from the code where you are using dog instance print dog.toString()

Upvotes: 0

user2219372
user2219372

Reputation: 2455

in class Dog

public String toString(){
    return "details of dog";
}

so you can:

a = new Dog();

.... System.out.println(a);

Upvotes: 0

Related Questions