Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Angry 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...

    please help, i'm so fed up with this!


    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:

    Java 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:

    Java 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());
                   
        
        
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    164
    Rep Power
    0

    Default 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
    Java Code:
    public classname(int a, double b, String c, int[] d) {
    //constructor
    }
    should suffice.

  4. #4
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Unhappy 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:
    Java 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:

    Java 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!

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default 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:

    Java 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:

    Java Code:
    public int getFoo() {
      return x;
    }
    And your set diameter and set radius are wrong. Your set r but return radius. Same for diameter.

    This makes no sense syntactically or mathematically
    Java 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.

    Java 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    164
    Rep Power
    0

    Default 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.

    Check your variable names over. Check your formulas.

    Honestly, I'd say take a look at what Jim told you and just start over. If you read over what he said and understand it, it should be much quicker to write from nothing then to try to fix all of these errors.

Similar Threads

  1. Calendar Class and Accessor Methods
    By sherifhanna82 in forum New To Java
    Replies: 3
    Last Post: 04-19-2013, 06:15 PM
  2. Accessor and mutator methods help
    By ajb5876 in forum New To Java
    Replies: 11
    Last Post: 02-15-2013, 03:59 PM
  3. Accessor & Mutator methods understanding..
    By 6thDAY in forum New To Java
    Replies: 3
    Last Post: 08-14-2010, 07:27 PM
  4. Accessor/Mutator Question
    By noble in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 04:21 AM
  5. Replies: 6
    Last Post: 12-02-2008, 11:15 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •