# sphere class using accessor/mutator methods

• 02-25-2014, 12:37 AM
youcantbesyrian
sphere class using accessor/mutator methods
so i've been working on this sphere class forever and have tried rewriting it a number of times. each time i change something i get a different error...

instructions: create a sphere class with the following properties:
private attributes: x, y, z coordinates of center, and the radius.
accessor and mutator methods to: set and get the x,y,z coordinates, set and get the radius, get the volume and surface area of the sphere.

this is the main sphere class:

Code:

```package spheretester; <pre class="brush:java;"> */ import java.io.*; import java.util.Scanner; public class Sphere {     public static void main(String[] argv) throws Exception     {         BufferedReader cin;         cin = new BufferedReader(new InputStreamReader(System.in));}       private static double x, y, z;     private static double diameter;     private static double radius;     private static double volume;     public static double surfaceArea;         public static final double Pi = 3.14;            Sphere(static double x, static double y, static double z){         double d = (x+y+z);     }             Sphere(static double d, static double r, static double v, static double sa){     diameter = d;     radius = r;     volume = v;     surfaceArea = sa;     }         static double getDiameter(){         return diameter;     }         static double getRadius(){         return radius;     }         double getVolume(){         double r = diameter / 2.0;         double v = 4.0/3.0*Math.PI*r*r*r;         return v;     }         double getSurface(){         double r = diameter / 2.0;         double sa = 4.0*Math.PI*radius*radius;         return sa;     }             System.out.println("Please enter in the coordinates of the sphere");         System.out.println("as three different integers:" + Sphere.x() + y() + z());         }```
and this is my sphere tester class:

Code:

```package spheretester; public class SphereTester {     /**     * @param args the command line arguments     */     public static void main(String[] args) {         // TODO code application logic here                 Sphere s1;         s1 = new Sphere(1.0);         Sphere s2;         s2 = new Sphere(2.0);                 System.out.println("Sphere 1:  Diameter: " + Sphere.getDiameter() + " Surface: " + s1.getSurface() + " Volume: " + s1.getVolume());         System.out.println("Sphere 2:  Diameter: " + s2.getDiameter() + " Surface: " + s2.getSurface() + " Volume: " + s2.getVolume());                       }```
• 02-25-2014, 12:49 AM
jim829
Re: sphere class using accessor/mutator methods
You should not be using static variables. And you don't need a main method in your Sphere class. Just make those variables declared in Sphere.main() instance fields. Do your prompting for input in your test class.

Regards,
Jim
• 02-25-2014, 01:08 AM
AlexGraal
Re: sphere class using accessor/mutator methods
Small note - your final variables should be all capital. So pi should be called PI. EDIT: Also, why have that variable if you aren't going to use it?

Anyway, as Jim said - your classes there shouldn't be static, as they are only useable after an instance of a class is created. You need to "define" the numbers by calling on the constructor before you can do any calculations.

Furthermore, your parameters for the methods shouldn't be static either I don't think. A simple
Code:

```public classname(int a, double b, String c, int[] d) { //constructor }```
should suffice.
• 02-27-2014, 12:32 AM
youcantbesyrian
Re: sphere class using accessor/mutator methods
okay i fixed most of the errors (i think). it's still not compiling though (and it tells me that my error is on a line where there is no code written...wha??).

errors:
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: spheretester.Sphere
at spheretester.SphereTester.main(SphereTester.java:2 1)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

here is my sphere class code:
Code:

```public class Sphere {   public void main(String[] argv) throws Exception {     /* declare coordinate variables as well as diamater&radius */         /* private methods */         private double x, y, z;     private double diameter;     private double radius;     double volume;     double surfaceArea;     }       public double getX(){         return x;}     public double setX() {         Sphere.x = new x;}     public double getY(){         return y;}     public double setY() {         Sphere.y = new y;}       public double getZ() {         return z;}     public double setZ() {         Sphere.z = new z;}     public double getDiameter(){         return d;}     public double setDiameter (double x, double y, double z, double diameter){         Sphere.diameter = (()x + ()y + ()z);}       public double getRadius(){         return r;}      public double setRadius(double diameter, double radius){         Sphere.radius = Math.pow(diameter/2.0);}           public double getVolume(){         return v;}     public double setVolume(double radius, double volume){         double radius = (diameter / 2.0);         double volume = (4.0/3.0*Math.PI* radius * radius * radius);}     public double getSurfaceArea(){         return sa;}     public double setSurfaceArea(double radius, double diameter, double surfaceArea){         double radius = diameter / 2.0;         double surfaceArea = (4.0*Math.PI*radius*radius);}     }```
and here is the sphereTester:

Code:

```package spheretester; public class SphereTester {     /**     * @param args the command line arguments     * @throws java.lang.Exception     */  public static void main (String[] args) throws Exception  {         // TODO code application logic here                     Sphere s1;         s1 = new Sphere();         s1.getX();         s1.getY();         s1.getZ();         s1.getDiameter();         s1.getRadius();         s1.getVolume();         s1.getSurfaceArea();                         Sphere s2;         s2 = new Sphere();         s2.getX();         s2.getY();         s2.getZ();         s2.getDiameter();         s2.getRadius();         s2.getVolume();         s2.getSurfaceArea();                         System.out.println("Please enter in the coordinates of the sphere");         System.out.println("as three different integers:" + s1.getX() + s1.getY() + s1.getZ());         System.out.println("Sphere 1:  Diameter: " + s1.getDiameter() + " Surface: " + s1.getSurfaceArea() + " Volume: " + s1.getVolume());         System.out.println("Sphere 2:  Diameter: " + s2.getDiameter() + " Surface: " + s2.getSurfaceArea() + " Volume: " + s2.getVolume());                        } }```

i appreciate the help guys!
• 02-27-2014, 01:19 AM
jim829
Re: sphere class using accessor/mutator methods
You clearly have lots of problems in your code. Has the instructor taught anything at all about method creation?

Setters must set something but you are not passing anything to be set. And setters don't return anything
so they don't have a return and have a return type of void. Assume you want to set x to something:

Code:

```public void setFoo(int x) {     this.x = x;  // set the instance field to the passed parameter. }```
Getters have a return type but no argument:

Code:

```public int getFoo() {   return x; }```

This makes no sense syntactically or mathematically
Code:

`Sphere.diameter = (()x + ()y + ()z);`
Nor does this. Math.pow is incorrect and isn't required to compute the radius. You need to
check out a math book to get the correct formulae.

Code:

```public double setRadius(double diameter, double radius){       Sphere.radius = Math.pow(diameter/2.0); }```
And remember, setters don't return anything. Getters do.

Regards,
Jim
• 02-27-2014, 03:52 AM
AlexGraal
Re: sphere class using accessor/mutator methods
I highly recommend that you sit down with a piece of paper and write out what you want your code do, along with all of the formulas.

A big mistake beginners make is jumping into something before they know what they are doing, hoping they can just do it "along the way." It's very tempting to just sit down and write "class blah{ public static void main ( String[] args ) { . . .", but you REALLY should plan out what's going on.