Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
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.
 

XnavxeMiyyep

macrumors 65816
Mar 27, 2003
1,131
4
Washington
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:

// returns a string
public String toString(){

return "Radius = " + radius + " Diameter = " + diameter + " Volume = " + volume + " Surface area = " + surfaceArea;
}

should probably be something like:
// returns a string
public String toString(){

String toReturn = "Radius = " + radius + " Diameter = " + diameter + " Volume = " + volume + " Surface area = " + surfaceArea;
return toReturn;

}

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:
SphereTester spheretester = new SphereTester();
is unnecessary.

I will go to sleep now, so good luck!
 

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
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.
 

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
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?
 

aLoC

macrumors 6502a
Nov 10, 2006
726
0
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
 

macman2790

macrumors 6502a
Original poster
Sep 4, 2006
716
1
Texas
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
thanks man, i didnt catch myself doing that.
 

scan

macrumors 6502
Oct 24, 2005
344
0
Could you please pass the radius in with another constructor? Its driving me nuts! :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.