Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Sphere (Volume/Surface Area)

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Sphere (Volume/Surface Area)

    A question (not homework, a question from a book, don't worry) asks me to create a program to work out the volume and surface area of a sphere.

    The surface area part works fine, it is the volume which comes up with the wrong answers. I can't see anything wrong with the formula so I assuming it must be a coding error on my part. Also feel free to criticise general coding standards. The more it hurts the more it helps

    Thanks

    public class Sphere {
     
    	public double diameter;
    	private double radius, volume, surfaceArea;
     
    	public Sphere() {
    		diameter = 1;	
    	}
     
    	public void setDiameter (double value) {
    		diameter = value;
    	}
     
    	public double getDiameter() {
    		return diameter;
    	}
     
    	private double radius() {
    		radius = diameter / 2;
    		return radius;
    	}
     
    	private double volume() {
    		volume = (4/3) * Math.PI * Math.pow(radius(), 3);
    		return volume;
    	}
     
    	private double surfaceArea() {
    		surfaceArea = 4 * Math.PI * Math.pow(radius(), 2);
    		return surfaceArea;
    	}
     
    	public String toString() {
    		String result = "Volume: " + Double.toString(volume()) + " Surface area: " + Double.toString(surfaceArea());
     
    		return result;
    	}
     
     
    }

    public class MultiSphere {
     
    	public static void main(String[] args) {
     
    		Sphere sph1, sph2;
     
    		sph1 = new Sphere();
    		sph2 = new Sphere();
     
    		sph1.setDiameter(4);
    		sph2.setDiameter(10);
     
    		System.out.println(sph1);
    		System.out.println(sph2);
     
    	}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,356
    Thanks
    182
    Thanked 833 Times in 776 Posts
    Blog Entries
    5

    Default Re: Sphere (Volume/Surface Area)

    Be sure you are using floating point math. For instance, try out the following code:
    double ratio = 4/3;
    System.out.println(ratio);

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Sphere (Volume/Surface Area)

    Thank you for pointing that out. I didn't notice it. By changing part of the code to this:

    private double volume() {
    		volume = 1.33333333 * Math.PI * Math.pow(radius(), 3);
    		return volume;

    The program works. I have just done some quick reading on the subject but don't really get how I can implement a solution. Could you give a pointer towards a good explanation if you know of one.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,356
    Thanks
    182
    Thanked 833 Times in 776 Posts
    Blog Entries
    5

    Default Re: Sphere (Volume/Surface Area)

    Specify a floating point number by defining a double or float. The above example can be adapted for floating point math:

    double ratio = 4/3d;//specifies double math
    System.out.println(ratio);
    ratio = 4/3f;//specifies float math
    System.out.println(ratio);

  5. The Following User Says Thank You to copeg For This Useful Post:

    Alex L (October 5th, 2011)

  6. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Sphere (Volume/Surface Area)

    Thank you, that works a treat

Similar Threads

  1. Display in a text area
    By susieferrari in forum Java Theory & Questions
    Replies: 21
    Last Post: March 22nd, 2011, 10:56 AM
  2. Creation of a curved surface as an airfoil using java AWT
    By murdplacid in forum AWT / Java Swing
    Replies: 2
    Last Post: September 22nd, 2010, 03:23 AM
  3. 3D Terrain & Sphere
    By quddusaliquddus in forum Java SE APIs
    Replies: 3
    Last Post: March 20th, 2010, 01:59 PM
  4. Problem in implementing mortgage calculator
    By American Raptor in forum AWT / Java Swing
    Replies: 1
    Last Post: April 1st, 2009, 03:09 PM
  5. How to read character from image area(jpg image)?
    By sundarjothi in forum Java Theory & Questions
    Replies: 5
    Last Post: August 6th, 2008, 03:08 AM