CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Help with Scientific Notation in Java (http://www.codingforums.com/showthread.php?t=272038)

kawaiiresachan 09-01-2012 11:48 PM

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

}


VIPStephan 09-02-2012 12:08 AM

Code:

Java != JavaScript
Moved to the correct forum.

Fou-Lu 09-02-2012 05:40 PM

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.


All times are GMT +1. The time now is 09:00 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.