Results 1 to 12 of 12
- 04-14-2011, 06:44 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
difficulty understanding writing classes
Hey again guys, I got stuck again and was wondering if people wouldn't mind helping out this guy again. This time I got stuck learning how to create classes and then using them in different methods. So here's what the assignment is about:
Design and implement a class called Sphere that contains instance data that represents the sphere's diameter.
1. Define the Sphere constructor to and initialize the diameter, and include getter and setter methods for the diameter.
2. Include methods that calculate and return the volume and surface area of the sphere
3. Include a toString method that returns a one-line description of the sphere.
4. Create a driver class called MultiSphere, whose main method instantiates and updates several Sphere objects.
Java Code:// Sphere project for pp0401. *Works together with MultiSphere project. public class Sphere { { private double diameter; public Sphere (double width) { diameter = width; } public double radius() { radius = diameter / 2; r = radius; } public double volume() { volume = 4 * Math.PI * Math.pow(r, 3); } public double SA() { SA = 4 * Math.PI * Math.pow(r, 2); } public String toString() { return diameter + "/t" + radius + "/t" + volume "/t" + SA; } } }WHEN I RUN MultiSphere I GET THE ERROR:Java Code:// MultiSphere project for pp0401. *Works together with Sphere project. public class MultiSphere { public static void main (String[] args) { Sphere sphere1 = new Sphere (54); Sphere sphere2 = new Sphere (24); Sphere sphere3 = new Sphere (1); System.out.println (sphere1); System.out.println (sphere2); System.out.println (sphere3); } }
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor Sphere(int) is undefined
The constructor Sphere(int) is undefined
The constructor Sphere(int) is undefined
WHEN I RUN Sphere I GET THE ERROR:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor Sphere(int) is undefined
The constructor Sphere(int) is undefined
The constructor Sphere(int) is undefined
at MultiSphere.main(MultiSphere.java:7)
What am I doing wrong? Please tell me in details, but in English... Speaking in Java still confuses me to the point where I say "huh?" and would point back the the question like you might not have understood my question. :P
Thanks in advance guys~! I am just waiting until I get to the point I am fluent enough in Java to give back to the community, but it's not happening as quickly as possible... please be understanding.Last edited by elecleoalune; 04-14-2011 at 07:11 AM. Reason: adding code tags
-
Your code is chock full of errors telling me a couple of things:
1) you're not compiling often enough
2) you're trying to make things up that just isn't Java.
I recommend that you scrap this code and start over. When you do this, remember to:
1) compile early and often, probably after adding each new line of code.
2) If you encounter a compilation error, don't add any more code until you fix the current error(s), lest you continue to add bad code to bad code.
3) use your texts and tutorials to guide you as you can't guess in Java.
4) you've posted several times before on this site, so it's time you started using code tags when posting code. My signature below can help you out with this.
Good luck, and come on back if you're still stuck.
- 04-14-2011, 06:54 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
thanks buddy~ I'll check out your code tags post!
- 04-14-2011, 07:13 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
okay, although i get compilation error, I don't exactly know how to compile it correctly.
I guess I am having such a difficult time with this is because the textbook examples are giving me limited examples to understand it from.
does anyone know any example codes I could take a look at?
I've been trying to find it through searching "class" but I get hundreds of search results...
if someone is willing to even give me a keyword that i could search for, it would be really helpful!
- 04-14-2011, 07:23 AM #5
Fubar's comments aer very valid and will help you learn and write better code in the future. Your immediate problem is explained in the error message. When calling methods and constructors the number, the order and the type of parameters must match. In your code you do not have a constructor that accepts a single int.
-
It's even more basic that that. You've got an extra set of braces in your code:
That will mess up everything (and that's just error number one).Java Code:public class Sphere { { // !!! this guy here and its pair at the end
Again, get rid of that code as it's borked and use class examples that are in the Java tutorials as the basis for your class skeleton.
- 04-14-2011, 08:13 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
I'll keep your tips in mind.
I guess I don't quite understand this whole chapter very well so I guess I'll reread it and then work on this project after trying to understand it fully.
The weird thing is that I am getting error when I am following the textbook's examples.
Maybe I shouldn't learn from this text book and learn straight from Java Tutorial's web site?
What do you guys think?
- 04-14-2011, 10:57 AM #8
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
There is no getter and setter methods for the diameter. At least, no standard getter and setter methods.
- 04-14-2011, 01:46 PM #9
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
oh is that what the problem is??
darn it! I have to figure out how to do that.
Any suggestion or tips on setting getter and setter method?
-
You need to read the tutorials. Please start here: Tutorials
- 04-15-2011, 08:44 AM #11
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
yup, I am reading it every time I have time available. It's just difficult with my time schedule currently. I will read the turorial and work on it again and then tell you guys what the result is.
- 04-18-2011, 03:06 PM #12
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
Hi again guys, I just wanted to let you guys know that after putting the time in whenever possible, I have finished reading the "Object-Oriented Programming" section of Java as you guys suggested. I thank you guys again for showing me the way. I just wanted to post my codes for futures noobs like me, if they were to need it for any reason.
Java Code:// Sphere project for pp0401. *Works together with MultiSphere project. import java.text.DecimalFormat; public class Sphere { double diameter = 0, radius, volume, SA, r; void changeDiameter (double newValue) { diameter = newValue; radius = diameter / 2; r = radius; volume = 4* Math.PI * Math.pow(r, 3); SA = 4 * Math.PI * Math.pow(r, 2); } void printStates() { DecimalFormat fmt = new DecimalFormat ("0.###"); System.out.println ("Diamter: " + fmt.format(diameter) + " Volume: " + fmt.format(volume) + " SA: " + fmt.format(SA)); } }Java Code:// MultiSphere project for pp0401. *Works together with Sphere project. public class MultiSphere { public static void main (String[] args) { // Create 2 Sphere objects Sphere sphere1 = new Sphere (); Sphere sphere2 = new Sphere (); // Invoke methods on those objects sphere1.changeDiameter(2); sphere1.printStates(); sphere2.changeDiameter(10); sphere2.printStates(); } }
Similar Threads
-
Writing classes???
By Bgreen7887 in forum New To JavaReplies: 1Last Post: 11-04-2010, 08:06 AM -
Applications in different difficulty
By bubbless in forum New To JavaReplies: 2Last Post: 03-11-2009, 12:31 AM -
Writing importable classes
By Katsu in forum New To JavaReplies: 6Last Post: 03-08-2009, 09:10 PM -
difficulty
By Daniela_v in forum New To JavaReplies: 2Last Post: 03-04-2009, 05:36 PM -
Writing classes in graphics
By CyberFrog in forum Java AppletsReplies: 2Last Post: 04-05-2008, 05:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks