Thursday, February 26, 2015

Wrong output with printf()


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 Nuke 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?



Java Code:



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 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;
}

}


No comments:

Post a Comment