Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help with Scientific Notation in Java

    Hi, I am a college freshman and I am taking my very first Java programming class. I have never programmed before, so I am having a little bit of trouble. For our second lab, we are to put numbers into scientific notation using the mod (%) and printf operator. I am having trouble doing this, so could some body help me figue out what I need to do? Here is a copy of my code:

    Code:
     import java.util.Scanner;
    
    public class UniversalGravitation {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    				
    		double m1;
    		double m2;
    		double r;
    		double a1;
    		double a2;
    		final double G = 6.67384e-11;
    				
    		Scanner keyboard =  new Scanner(System.in);
    		
    				
    		System.out.print("Enter the mass of the first object (in kilograms) ");
    		m1 = keyboard.nextDouble();
    		System.out.print("Enter the mass of the second object (in kilograms) ");
    		m2 = keyboard.nextDouble();
    		System.out.print("Enter the dsitance (in meters) between them ");
    		r = keyboard.nextDouble();
    		
    		
    		double F = G * ((m1*m2)/Math.pow(r, 2));
    		a1 = F/m1;
    		a2 = F/m2;
    		
    		System.out.printf("Mass of first object: \t\t" + m1 + "kg");
    		System.out.println("Mass of second object: \t\t" +  m2 + " kg");
    		System.out.println("Distance: \t\t\t" + r + " m");
    		System.out.println("Force: \t\t\t\t" + F + "N");
    		System.out.println("Acc. of first object: \t\t" + a1 + "m/s^2");
    		System.out.println("Acc. of second object: \t\t" + a2 + " m/s^2");
    	
    	}
    
    }
    Last edited by VIPStephan; 09-02-2012 at 12:08 AM. Reason: fixed code BB tags

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,474
    Thanks
    6
    Thanked 980 Times in 953 Posts
    Code:
    Java != JavaScript
    Moved to the correct forum.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I'm not sure why you are using the modulus operator here at all. With a printf, you can simply use %e or %E to display as scientific notation. Although, with doubles of these magnitudes, they will likely all show up as notation in the print/println calls anyway.


  •  

    Tags for this Thread

    Posting Permissions

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