Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default Help with java math... SOLVED

    Java Code:
    import java.util.Scanner;
    public class Sphere
    {
    	public static void main(String [] a)
    	{
    		System.out.println("This program prints surface area and volume");
    		System.out.println("of spheres for radii ranging from 10 to N,");
    		System.out.print("in increments of 10. Please input the value of N: ");
    		Scanner userInput = new Scanner(System.in);
    		int valueOfN = userinput.nextInt();
    		double pi = 3.14;
    		System.out.println("---------------------");
    		System.out.println("\t\tSurface");
    		System.out.println("r\tArea\tVolume");
    		System.out.println("---------------------");
    		
    		for(int r = 10; r <= valueOfN; r += 10)
    		{
    			System.out.print(r + "\t" + (4*pi*(r*r)) + "\t" + (4/3*pi*(r*r*r)));
    		}
    	}
    }
    hi everyone, im currently in university doing an intro to java course and we were give some sample questions from the previous years midterm to try out. I wrote this in order to find surface area and volum of a sphere from radii 10 to N. other than some rounding issues the surface area works however the volume doesn't. it appears to me when running the program that all it will do is the math of pi*r^3 but completely leave out the 4/3. anyone have an idea of either why its doing that or how I can fix it. I've tried rewriting the math several ways but still appears to be doing the same thing with omitting the 4/3
    Last edited by gabrielfirestorm; 10-25-2010 at 01:25 PM. Reason: solved

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    4/3 == 1 because both 4 and 3 are integers so integer division is used. Use 4.0/3.0 instead; oh, b.t.w. the Math class has a better approximation for pi.

    kind regards,

    Jos

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    do "4/3.0" or "4/(double) 3" or something similar. bare numbers in code without decimals are integers and integer math can only produce integers, thereby making 4/3 = 1.

    Edit: Man, am I slow.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Eranga View Post
    Seems your question is not a valid one, it's 0.75 x pi x r3 or other way?
    Sorry guys. Seems my knowledge in maths is not good. Misses fundamentals here. :o

  6. #6
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    thank you very much that explains everything :)

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Eranga View Post
    Seems your question is not a valid one, it's 0.75 x pi x r3 or other way?
    That's an incorrect formula; the volume of a sphere is 4/3*pi*r^3.

    kind regards,

    Jos

    edit: darn, where do all those answers come from all of a sudden? ;-)
    Last edited by JosAH; 10-25-2010 at 02:08 PM.

Similar Threads

  1. hi guys help me in math.random () in java
    By funkygarzon in forum New To Java
    Replies: 5
    Last Post: 08-09-2010, 04:48 AM
  2. Help need on math java program
    By zidangus in forum New To Java
    Replies: 27
    Last Post: 07-07-2010, 02:48 AM
  3. Java Math
    By levent in forum Java Tutorial
    Replies: 1
    Last Post: 05-12-2008, 09:03 AM
  4. java.lang.Math
    By eva in forum New To Java
    Replies: 2
    Last Post: 01-31-2008, 04:17 PM
  5. Help with math in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:05 AM

Posting Permissions

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