insomnius1
insomnius1

Reputation: 11

overriding toString but still using it to do the work

I'm working on an assignment and I have to create a parent class that another class extends and I have to over ride the toString() method in the parent class and use the parent toString method to display the new one, but add a line.

All that I'm fine with, but where I'm stuck, the original toString() has a number format object that formats a double into money, and I need a double formatted in the new line. The instructions say I should not need a number formatter in the new toString method, and to let the old toString() method handle "most of the work" but I can't figure out how it is possible to format this double.

Here's some code so you can see what I mean:

    import java.text.NumberFormat;

    public abstract class First {

    public First(int x, double y)

    protected int num = x;
    protected double num2 = y;
    }

    public String toString()
    {   
        NumberFormat x = NumberFormat.getCurrencyInstance();

        return "Int num: " + num + 
             "\n     num2: " + x.format(num2);

    }
    }

Then I have the child class:

public class Second extends First {

protected double num3;

public Second(int x, double y, double z) 
{
 super(x, y);
 num3 = z

}

public String toString()
{
    return super.toString() + "\n num3 " + num3; 
}

}

If I do this (I left out a lot of irrelevant code) it works, but I need num3 to be formatted like money, like num2 is in the parent class but I can't have a formatter object in the child class.

If someone could push me in the right direction, I would very much appreciate it.

Upvotes: 1

Views: 1155

Answers (2)

Carsten Schipke
Carsten Schipke

Reputation: 1

Without having checked, NumberFormat.getCurrencyInstance() probably returns a singleton and therefore calling it as the parent does, would not result in another object but the same one as the parent uses... Maybe a fun question :).

Upvotes: 0

Java Devil
Java Devil

Reputation: 10959

You could separate the Formatting from the toString() method in your parent class then use this in your child like:

In First

protected String formatNum(double number)
{
    NumberFormat x = NumberFormat.getCurrencyInstance();
    return x.format(number); 
}

public String toString()
{   
    return "Int num: " + num + 
         "\n     num2: " + formatNum(num2);
}

Then in second

public String toString()
{
    return super.toString() + "\n num3 " + formatNum(num3); 
}

EDIT An alternative is to have the NumberFormat Object as part of the First Object then initialise in in the constructor

public abstract class First {

    protected int num;
    protected double num2;    
    protected NumberFormat f;

    public First(int x, double y)
    {
        num = x;
        num2 = y;
        f = NumberFormat.getCurrencyInstance();
    }

    public String toString()
    {   
        return "Int num: " + num + 
             "\n     num2: " + f.format(num2);
    }
}

Then just use that in Second

public String toString()
{
    return super.toString() + "\n num3 " + f.format(num3); 
}

Upvotes: 2

Related Questions