Results 1 to 6 of 6
- 02-25-2014, 12:37 AM #1
Member
- Join Date
- Feb 2014
- Posts
- 6
- Rep Power
- 0
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()); }
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()); }
- 02-25-2014, 12:49 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 02-25-2014, 01:08 AM #3
Señor Member
- Join Date
- Jan 2014
- Posts
- 184
- Rep Power
- 0
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 }
- 02-27-2014, 12:32 AM #4
Member
- Join Date
- Feb 2014
- Posts
- 6
- Rep Power
- 0
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);} }
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!
- 02-27-2014, 01:19 AM #5
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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. }
Java Code:public int getFoo() { return x; }
This makes no sense syntactically or mathematicallyJava Code:Sphere.diameter = (()x + ()y + ()z);
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); }
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 02-27-2014, 03:52 AM #6
Señor Member
- Join Date
- Jan 2014
- Posts
- 184
- Rep Power
- 0
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
-
Calendar Class and Accessor Methods
By sherifhanna82 in forum New To JavaReplies: 3Last Post: 04-19-2013, 07:15 PM -
Accessor and mutator methods help
By ajb5876 in forum New To JavaReplies: 11Last Post: 02-15-2013, 04:59 PM -
Accessor & Mutator methods understanding..
By 6thDAY in forum New To JavaReplies: 3Last Post: 08-14-2010, 08:27 PM -
Accessor/Mutator Question
By noble in forum New To JavaReplies: 4Last Post: 02-02-2010, 05:21 AM -
Trying to understand accessor and mutator methods
By DC200 in forum New To JavaReplies: 6Last Post: 12-03-2008, 12:15 AM
Bookmarks