Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-01-2012, 11:48 PM   PM User | #1
kawaiiresachan
New to the CF scene

 
Join Date: Sep 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
kawaiiresachan is an unknown quantity at this point
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
kawaiiresachan is offline   Reply With Quote
Old 09-02-2012, 12:08 AM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,601
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Code:
Java != JavaScript
Moved to the correct forum.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 09-02-2012, 05:40 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
code, java, mod operator

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:09 AM.


Advertisement
Log in to turn off these ads.