PDA

View Full Version : simple java program: what am i doing wrong?




macman2790
Jan 29, 2007, 01:03 AM
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.

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.

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
Jan 29, 2007, 01:23 AM
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
Jan 29, 2007, 01:26 AM
the output displays
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
Jan 29, 2007, 01:35 AM
/*
* 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
Jan 29, 2007, 01:58 AM
Your first post was nearly right, just need to change the toString method a little:


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
Jan 29, 2007, 02:11 AM
Your first post was nearly right, just need to change the toString method a little:


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.

aLoC
Jan 29, 2007, 02:24 AM
thanks man, i didnt catch myself doing that.

No worries. Little mistakes are a good way to learn...

ChrisBrightwell
Jan 29, 2007, 03:13 AM
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.

scan
Jan 29, 2007, 02:49 PM
Could you please pass the radius in with another constructor? Its driving me nuts! :D