0

Why am I keeping getting 0.0000 in the output ? I've put here some code which is main class, one of the child classes FragGrenade and parent class -Explosive. I keep getting 0.0000 from the method public void explode() and I guess I'm doing smth wrong with printf(). Could someone help me with that?

public class ExplosiveDemo{

    public static void main(String args[]){

        Explosive frag = new FragGrenade(15.5);

        System.out.println( frag.isRadioactive() ); 
        // false
        frag.explode();
        System.out.println();
        // OUTPUT:
        // Boom: blast radius 15.50 meters

        Explosive nuke = new Nuke(3.2);
        System.out.println(nuke.isRadioactive() ); 
        // true
        nuke.explode();
        // OUTPUT:
        // Boom: blast radius 3200.00 meters
        // Area irradiated
  }
public class FragGrenade extends Explosive{

    protected double blastRadiusMeters;

    public FragGrenade(double blastRadiusMeters)
    {
        super(blastRadiusMeters);
    }

    public void explode()
    {
        System.out.printf("Boom: blast radius %f meters\n", this.blastRadiusMeters);
    }
    public boolean isRadioactive()
    {
        return false;
    }


}

}


public abstract class Explosive {

    protected double blastRadiusMeters;

    public Explosive(double blastRadiusMeters)
    {
        this.blastRadiusMeters = blastRadiusMeters;
    }

    public void explode()
    {
        System.out.printf("Boom: blast radius %.2 meters\n", this.blastRadiusMeters);
    }
    public boolean isRadioactive()
    {
        return false;
    }

}
Nikola
  • 23
  • 1
  • 6

3 Answers3

0

You define the exact same variable protected double blastRadiusMeters; in both the subclass and superclass. When this happens, the subclass shadows the superclass, which means that accessing this.blastRadiusMeters will access the one in FragGrenade, which has never been set.

Try removing everything from your subclass except for the constructor

public class FragGrenade extends Explosive{

    public FragGrenade(double blastRadiusMeters)
    {
        super(blastRadiusMeters);
    }
}
Zach Sadler
  • 570
  • 3
  • 11
  • Here's some more info to help you understand: http://stackoverflow.com/questions/12086298/why-instance-variable-of-super-class-is-not-overridden-in-sub-class-method – Zach Sadler Feb 26 '15 at 15:26
  • You're right @Zach Sadler. But I also have another subclass which has different variable into the constructor, i've described that below. – Nikola Feb 26 '15 at 15:29
  • @Nikola The reason that Nuke works is because you used a different variable name - `blastAreaKilometers` instead of `blastRadiusMeters` – Zach Sadler Feb 26 '15 at 15:31
0

In derived class you declared another

protected double blastRadiusMeters;

but in constructor via

public FragGrenade(double blastRadiusMeters) {
    super(blastRadiusMeters);//here
}

you are setting value of this field from super class Explosive.

You should probably change your classes to

public class FragGrenade extends Explosive {

    public FragGrenade(double blastRadiusMeters) {
        super(blastRadiusMeters);
    }

}

and simply use inherited versions of methods from Explosive class.

To make it work you will also need to correct %.2 in your printf. It should be %.2f.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Thank you, @Pshemo! One last question: why do I get an error in another child-class in this line:System.out.printf("Boom: blast radius %f kilometers\n", this.blastAreaKilometers * 1000);.... But when I change to blastRadiusMeters then it gives me result? – Nikola Feb 26 '15 at 15:43
  • @Nikola What error you are getting? Judging by your example I suspect that this may be related with tack that you are trying to multiply `this.blastAreaKilometers` (which you probably don't have) by 1000, instead of `this.blastRadiusMeters`. – Pshemo Feb 26 '15 at 15:50
  • thank you again, I'm struggling about how the protected variable of the parent class can be accesses in a child class – Nikola Feb 26 '15 at 15:56
  • @Nikola Then make your variable `private` in parent class so it will be accessible only by this class. You can add `public` or `protected` method `double getBlastRadiusMeters(){return blastRadiusMeters;}` inside it, so derived class would be able to get value from inside its super class, but wouldn't be able to change it. – Pshemo Feb 26 '15 at 16:09
0
public class Nuke extends Explosive {

    protected double blastAreaKilometers;

    public Nuke(double blastAreaKilometers)
    {
        super(blastAreaKilometers);
    }

    public void explode()
    {
        System.out.printf("Boom: blast radius %f kilometers\n", this.blastAreaKilometers * 1000);
    }
    public boolean isRadioactive()
    {
        return true;
    }

}
Nikola
  • 23
  • 1
  • 6