Results 1 to 6 of 6
  1. #1
    almina is offline Member
    Join Date
    Aug 2008
    Posts
    1
    Rep Power
    0

    Default Proofreading this small Java program

    I've recently got this new spiffy Java textbook, and upon doing one of the programming projects, it appears that I've encountered a problem. The logic of this program appears to be fine, but something sematically appears to have effected it to produce a non-sensical result.

    The purpose of this program is to construct a class which computes the surface area and volume of a sphere, and return the values.

    Java Code:
    class Sphere
    {
    	private final double FT = 1.333333;
    	private final double PI = 3.14159;
    	
    	private double diameter;
    	private double radius;
    	private double volume;
    	private double surfaceArea;
    	
    	public Sphere (double diameterofSphere)
    	{
    		diameterofSphere = diameter;
    	}
    	
    	public double findRadius ()
    	{
    		radius = diameter / 2;
    		return radius;
    	}
    	
    	public double getVolume ()
    	{
    		volume = (FT * PI * Math.pow(radius, 3));
    		return volume;
    	}
    	
    	public double getSurfaceArea ()
    	{
    		surfaceArea = (4 * PI * Math.pow(radius, 2));
    		return surfaceArea;
    	}
    	
    	public String toString ()
    	{
    		return "The surface area and volume of your sphere is: " + surfaceArea +
    		volume;
    	}
    }
    
    public class SphereData
    {
    	public static void main (String[] args)
    	{
    			Sphere sphere1 = new Sphere(100);
    			
    			sphere1.findRadius();
    			sphere1.getVolume();
    			sphere1.getSurfaceArea();
    			
    			System.out.println (sphere1);
    	}
    }
    However, this is the output:

    Java Code:
    The surface area and volume of your sphere is: 0.00.0
    Thanks for reading. Also, if I'm doing something that's unnecessary, please tell me =)

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    	public Sphere (double diameterofSphere)
    	{
    //		diameterofSphere = diameter;
    		diameter = diameterofSphere;
    	}

  3. #3
    Lizzip is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

    Default

    From the looks of it, radius = diameter / 2; is dividing nothing by 2.

    I think the line diameterofSphere = diameter; needs to be diameter = diameterofSphere

    EDIT: Just realised that's what the guy before me was saying

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    you may want to put a space in your output. At first I thought something was wrong when i saw 0.00.0, 2 decimals in a number O_O
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Lizzip View Post
    EDIT: Just realised that's what the guy before me was saying
    And a little over a year earlier than you to boot. We appreciate your helping out, but you may want to reply to more recent threads.

  6. #6
    Lizzip is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    And a little over a year earlier than you to boot. We appreciate your helping out, but you may want to reply to more recent threads.
    Sorry, didn't even notice that D:

Similar Threads

  1. small issues with a program
    By jimJohnson in forum New To Java
    Replies: 6
    Last Post: 04-25-2008, 09:28 AM
  2. Building small web application in java for practice.
    By Saurabh321 in forum New To Java
    Replies: 1
    Last Post: 02-01-2008, 04:38 PM
  3. Small scale Java Editor
    By Greenfrog99 in forum AWT / Swing
    Replies: 0
    Last Post: 01-27-2008, 09:46 PM
  4. Replies: 1
    Last Post: 01-05-2008, 08:48 PM
  5. Small tennis simulation in Java
    By diego in forum New To Java
    Replies: 1
    Last Post: 12-02-2007, 02:32 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
  •