Results 1 to 3 of 3
  1. #1
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Wrong output with printf()

    I keeping getting 0.0000 in the output
    What is wrong with that output? What do you want to get instead?

    Take a look at the Formatter class's API doc for how to write formatting Strings.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Wrong output with printf()

    You haven't shown us the Explosive class, but this implies it has a blastAreaKilometers of its own, which is not the one you are trying to print out:
    Java Code:
        protected double blastAreaKilometers;
     
        public Nuke(double blastAreaKilometers)
        {
            super(blastAreaKilometers);
        }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. Getting wrong output. Please Help!!
    By FAkamo in forum New To Java
    Replies: 2
    Last Post: 02-23-2012, 11:45 PM
  3. Concerning a small output problem with PRINTF
    By Kaizer in forum New To Java
    Replies: 2
    Last Post: 11-04-2011, 06:27 PM
  4. output not as expected.What's wrong?
    By abdullahansari in forum New To Java
    Replies: 1
    Last Post: 12-03-2010, 08:01 PM
  5. Whats Wrong I m nt Getting output -please please help
    By divakantdinesh in forum New To Java
    Replies: 3
    Last Post: 10-16-2010, 09:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •