Cynosure
02-22-2008, 07:18 PM
Hey =)
I've got a Java assignment for school... I'm not asking for you to do the code for me ;)
Here is the problem:
"Design and implement a class called 'Sphere' that contains instance data that represents the sphere's diameter. Define the 'sphere constructor' to accept and initialize the diameter, and include getter and setter methods for the diameter. Include methods that calculate and return the volume and surface area of the sphere. Included a 'toString' method that returns a one-line description of the sphere. Create a driver class called 'Multisphere', whose 'main' method instantiates and updates several Sphere objects"
Apparently there are two files that are required.. I've got the Sphere.java file down.. I think. But what I am unsure about is what needs to be in the other file.. and in the 'main' method. Am I just supposed to make up values? I'm not really sure what I am doing. haha. I think I know how to create a driver class inside that file.. maybe.. but I don't know what it needs to do.
Should I just like.. create 3 spheres and then assign them random diameters?
Here is the code that I have for the Sphere.java file.
public class Sphere {
public double diameter;
public Sphere(){
this.diameter = 0;}
public void setDiameter(double d){
this.diameter = d;}
public double getDiameter(){
return diameter;}
public String toString(){
System.out.println("The diameter of the Sphere is: ");
String result = Double.toString(diameter);
return result;}
public double volume(double v, double vr, double r){
r = ( diameter / 2 );
vr = ( r * r * r );
v = ( 4 / 3 ) * (Math.PI * vr);
return v;}
public double surface(double sa, double r, double sr){
r = ( diameter / 2);
sr = ( r * r );
sa = ( 4 ) * (Math.PI * sr);
return sa;
}
}
It compiles.. but I'm not sure if it's right.
Again, I'm not asking for answers; I just need a little push in the right direction, because I am stuck =/
I've got a Java assignment for school... I'm not asking for you to do the code for me ;)
Here is the problem:
"Design and implement a class called 'Sphere' that contains instance data that represents the sphere's diameter. Define the 'sphere constructor' to accept and initialize the diameter, and include getter and setter methods for the diameter. Include methods that calculate and return the volume and surface area of the sphere. Included a 'toString' method that returns a one-line description of the sphere. Create a driver class called 'Multisphere', whose 'main' method instantiates and updates several Sphere objects"
Apparently there are two files that are required.. I've got the Sphere.java file down.. I think. But what I am unsure about is what needs to be in the other file.. and in the 'main' method. Am I just supposed to make up values? I'm not really sure what I am doing. haha. I think I know how to create a driver class inside that file.. maybe.. but I don't know what it needs to do.
Should I just like.. create 3 spheres and then assign them random diameters?
Here is the code that I have for the Sphere.java file.
public class Sphere {
public double diameter;
public Sphere(){
this.diameter = 0;}
public void setDiameter(double d){
this.diameter = d;}
public double getDiameter(){
return diameter;}
public String toString(){
System.out.println("The diameter of the Sphere is: ");
String result = Double.toString(diameter);
return result;}
public double volume(double v, double vr, double r){
r = ( diameter / 2 );
vr = ( r * r * r );
v = ( 4 / 3 ) * (Math.PI * vr);
return v;}
public double surface(double sa, double r, double sr){
r = ( diameter / 2);
sr = ( r * r );
sa = ( 4 ) * (Math.PI * sr);
return sa;
}
}
It compiles.. but I'm not sure if it's right.
Again, I'm not asking for answers; I just need a little push in the right direction, because I am stuck =/