Results 1 to 4 of 4
  1. #1
    sweetjava is offline Member
    Join Date
    Jul 2009
    Posts
    7
    Rep Power
    0

    Cool a simple conversion problem

    Hey Guys, im new to java and am working though the Textbook the art and science of JAVA, which is awesome by the way (well so far anyway).

    Anyway my problem is I want to write a simple program that converts KG to pounds and ounces.

    Converting KG to pounds is simple enough, (kg * 2.2) but my problem is how do i seperate the ounces part?

    for example say i had 1KG and converted it to 2.2pounds, how to I then take that .2 remainder and turn that into ounces?

    So I can get it to output, 2 pounds and x ounces?

    I hope this makes sense, here is my code so far (please don't laugh too hard)

    Java Code:
    	   public void run() {
    	
    		   println ("this program converts KG into the old" +
    		   		" pounds and ounces ");
    		   
    		  double KG_input = readDouble ("enter KG amount:");
    		  
    		  
    		   double Pound = KG_input * POUNDS_IN_A_KG;
    		   
    		   double ounces = Pound * OUNCES_IN_A_POUND;
    		   	   
    				   
    		  println (Pound);
    	}
    	
    
    
    public static final double POUNDS_IN_A_KG = 2.2 ;
    public static final double OUNCES_IN_A_POUND = 16;
    
    
    }

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    are you trying to do somethign like:
    Java Code:
    println( Pound +"pounds and "+ ounces +" ounces");
    or maybe I'm misunderstanding your intentions...
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    You should take a look at the Java Code Conventions.

    The code below that you have gives you the amount of ounces that are in the variable pound. What you want is the remainder
    Java Code:
    double ounces = Pound * OUNCES_IN_A_POUND;
    try something like
    Java Code:
    double ounces = ( pound - (int)pound ) * OUNCES_IN_A_POUND;
    Mr. Beans

  4. #4
    sweetjava is offline Member
    Join Date
    Jul 2009
    Posts
    7
    Rep Power
    0

    Red face thanks

    :)hey thanks Mr.Beans.

    That was it exactly!

    here is the working code now.

    Java Code:
    public void run() {
        
               println ("this program converts KG into the old pounds and ounces ");
               
              double KG_input = readDouble ("enter KG amount:");
              
              
               double Pound = KG_input * POUNDS_IN_A_KG;
               double ounces = ( Pound - (int) Pound ) * OUNCES_IN_A_POUND;   
                       
                       
              println ((int) (Pound) + "pounds" + (ounces) +"ounces" );
              
              
        }
        
    
    
    public static final double POUNDS_IN_A_KG = 2.2 ;
    public static final double OUNCES_IN_A_POUND = 16;
    
    
    }
    thanks again, I think i get it now.

Similar Threads

  1. simple GUI problem
    By McChill in forum New To Java
    Replies: 4
    Last Post: 04-03-2009, 04:13 AM
  2. [SOLVED] Simple Conversion Calculator
    By dbashby in forum New To Java
    Replies: 6
    Last Post: 03-20-2009, 02:06 AM
  3. Simple IO problem
    By aamp in forum New To Java
    Replies: 2
    Last Post: 12-01-2008, 03:27 PM
  4. My Simple Refresh Problem
    By pmcastillo in forum New To Java
    Replies: 0
    Last Post: 03-26-2008, 08:59 AM
  5. Problem in TIFF Conversion
    By lakshmipriyam in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2007, 11:44 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
  •