2

My current class, SeriesCircuit class, contains two constructors:

public SeriesCircuit(Circuit input1, Circuit input2)
{
    this.circuit1 = input1;
    this.circuit2 = input2;
}

public SeriesCircuit(ParallelCircuit parallelCircuit, Circuit circuit1)
{
    this.parallelCircuit = parallelCircuit;
    this.circuit1 = circuit1;
}

Using the .toString() method, I can print out the first constructor's values as such:

public String toString()
{
    return circuit1.getResistance() + " + " + circuit2.getResistance();
}

Note: the values returned by .getResistance() get simply doubles, so a sample output would be like this string: 2.0 + 3.0.

My question is, since I have two constructors which offer two different input types, how do I only override one .toString() to output two different types of constructor inputs?

For example, in my main, I do this:

SeriesCircuit s1 = new SeriesCircuit(new Circuit(2.0), new Circuit(3.0));

SeriesCircuit s2 = new SeriesCircuit(new ParallelCircuit(s1, new Circuit(4.0)), new Circuit(1.0));

I could print out s1 using System.out.println(s1) just fine, but since I never specified a .toString() condition for constructor number two, I would expectedly get a NullPointerException.

theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169

4 Answers4

6

Construct your toString() on instance fields not based on constructor and it must be some descriptive and short.

If you need then you can use a boolean flag or not null check on values otherwise you will get null in your output.

Use StringBuilder#append() or String#concat() to append the string values.


EDIT

It's worth reading about

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
2

Regardless of which constructors you have, the toString() should reflect the state of the object, no more or no less. Usually the String returned from toString() will show the state of the fields of the object, and if some are null, then this too should be shown.

I mainly use toString() for debugging purposes, again to display the state of objects as the program runs. It should really not be used for production output, which is why I feel that your issue is a non-issue.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

You could set up a boolean variable that is true for one constructor and false for the other. In toString, you just then do an if else on that variable, and do your two different outputs that way.

Outsider
  • 550
  • 3
  • 9
0

Check first if the attribute parallelCircuit has been initialized (if it's null):

public String toString() {
    if(parallelCircuit == null) {
        return circuit1.getResistance() + " + " + circuit2.getResistance();
    }
    else {
        return parallelCircuit.getResistance() + " + " + circuit1.getResistance();
    }
}
jeojavi
  • 876
  • 1
  • 6
  • 15
  • A suggestion change is `parallelCircuit == null` instead of `!= null`, since if `parallelCircuit` is `null`, then that indicates constructor 1 was used. – theGreenCabbage Apr 13 '14 at 01:37