simple java program: what am i doing wrong?

Discussion in 'Mac Programming' started by macman2790, Jan 28, 2007.

  1. macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Location:
    Texas
    #1
    I'm creating a program that returns the values of a sphere's diameter, surface area, and volume. Heres the class I made to make a sphere object, there is probably something wrong with it. Please help identify what I'm doing wrong.
    Code:
    package sphere;
    
    public class Sphere {
        //attributes
        
        private double radius;
        private double PI =  3.14159265;
        
        /** Creates a new instance of Main */
        public Sphere(){
             
        }
    //  radius mutator
        public void setRadius(double r){
            radius = r;
        }
    // returns the radius
        public double getRadius(){
            return radius;
        }
    // returns the diameter
        public double getDiameter(){
            double diameter = radius + radius;
            return diameter;
        }
    //returns the volume of the sphere
        public double getVolume(){
            double volume = (4/3)*PI*(Math.pow(radius, 3));
            return volume;
        }
    // Returns the surface area of the sphere
        public double getSurfaceArea(){
            double surfaceArea = 4*PI*(Math.pow(radius, 2));
            return surfaceArea;
        }
        
        
    // returns a string    
        public String toString(){
            
            return "Radius = " + radius + " Diameter = " + diameter + " Volume        = " + volume + " Surface area = " + surfaceArea;
        }
    }
    
    now heres the program i made to display the information of a sphere with a radius of 10.
    Code:
    package sphere;
    public class SphereTester {
    
    
        
        /** Creates a new instance of SphereTester */
        public static void main(String [] args){
         
         
            
            SphereTester spheretester = new SphereTester();
            
            Sphere mySphere = new Sphere();
            mySphere.setRadius(10);
            mySphere.getDiameter();
            mySphere.getSurfaceArea();
            mySphere.getVolume();       
     System.out.println(mySphere);        
        
        }
    }

    Thanks for the help in advance.
     
  2. macrumors 65816

    XnavxeMiyyep

    Joined:
    Mar 27, 2003
    Location:
    Washington
    #2
    Okay. You haven't actually set the volume or the surface area in this code; only written methods to get them. You should make global variables for Surface Area and Volume and in the setRadius method, set the Surface Area and Volume to getSurfaceArea and getVolume. Also, depending on the level of your class,(I'm assuming this is for a programming class) you may want to test for values, and return error messages for the values if they haven't been set yet.


    Also, the code:

    should probably be something like:
    Although it's been awhile since I last used Java, I think when you return a String, it has to already have been made into a string. (Pretend I worded that better:p )


    The line:
    is unnecessary.

    I will go to sleep now, so good luck!
     
  3. thread starter macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Location:
    Texas
    #3
    the output displays
    Code:
    Radius = 10.0 Diameter = 0.0 Volume = 0.0 Surface area = 0.0
    thats where the real problem is.

    it does return the string the way i did it, we were taught in class to do it that way. i forgot to say that it compiled, that was my fault.
     
  4. thread starter macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Location:
    Texas
    #4
    Code:
    /*
     * Sphere.java
     *
     * Created on January 28, 2007, 11:28 PM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    
    package sphere;
    
    /**
     *
     * @author Kevin
     */
    public class Sphere {
        //attributes
        
        private double radius;
        private double PI =  3.14159265;
        private double diameter;
        private double surfaceArea;
        private double volume;
        
        /** Creates a new instance of Main */
        public Sphere(){
             radius = 1;
             PI =  3.14159265;
        }
        public void setRadius(double r){
            radius = r;
        }
        
        public double getRadius(){
            return radius;
        }
        public void setDiameter(){
            double diameter;
        }
        public double getDiameter(){
            double diameter = radius + radius;
            return diameter;
        }
        public void setVolume(){
            double volume;
        }
        public double getVolume(){
            double volume = (4/3)*PI*(Math.pow(radius, 3));
            return volume;
        }
        public void setSurfaceArea(){
            double surfaceArea;
        }
        public double getSurfaceArea(){
            double surfaceArea = 4*PI*(Math.pow(radius, 2));
            return surfaceArea;
        }
        
         
        public String toString(){
            
            return "Radius = " + radius + " Diameter = " + diameter + " Volume = " 
                    + volume + " Surface area = " + surfaceArea;
        }
    }
    
    my output stayed the same, what am i doing wrong?
     
  5. macrumors 6502a

    Joined:
    Nov 10, 2006
    #5
    Your first post was nearly right, just need to change the toString method a little:

    Code:
    public String toString() {
        return "Radius = " + radius + " Diameter = " + getDiameter() 
            + " Volume = " + getVolume() + " Surface area = " + getSurfaceArea();
    }
    
    Edit: also, if you wanted you could delete the PI member variable and use Math.PI
     
  6. thread starter macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Location:
    Texas
    #6
    thanks man, i didnt catch myself doing that.
     
  7. macrumors 6502a

    Joined:
    Nov 10, 2006
    #7
    No worries. Little mistakes are a good way to learn...
     
  8. macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #8
    You might also consider making a habit of using a .0 on the end of any number you expect to be used for double-precision math.

    3/4 != 3.0/4.0 in all cases.
     
  9. macrumors 6502

    Joined:
    Oct 24, 2005
    #9
    Could you please pass the radius in with another constructor? Its driving me nuts! :D
     

Share This Page