Results 1 to 16 of 16
- 02-04-2011, 12:17 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 33
- Rep Power
- 0
help plz confused with constructors
Hello I am new to java and I am confused on a project that I am working on. I have to design and implement a class called Sphere that contains instance data that represents the sphere’s diameter. I have to design the Sphere constructor to accept and initialize the diameter and include getter (getDiameter) and setter (setDiameter) methods for the diameter. I also have to include methods that calculate and return the volume (computeVolume) and surface area (computeArea) of the sphere. I also have to use the formula for volume and surface area where r represents the radius(of course) in a java format that I am also confused about. Also, I have to include a "toString" method returns a formatted one-line description of the sphere. if someone can help me by giving me some direction to approach this, I'll really appreciate it.
- 02-04-2011, 12:25 AM #2
Baby steps.
Write the class with just the method and constructor declarations and nothing else. Then compile it. then fix any errors. Once there are no errors move on. Try writing one method. Use dummy data. Write a driver class to create a Sphere and test your method. etc etc etc.
Here's a start:
Do that and when you get stuck come back here, post your code and the EXACT error message if you get them and most importantly of all ask a specific question. "I don't know how to do it" is not specific.Java Code:class XXXXXXXX { // instance variables // constructor // methods public String toString() { // code } }
- 02-04-2011, 01:09 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 33
- Rep Power
- 0
ok so here is where I'm at so far:
//************************************************** ************************************************** *
// Sphere.java
//
//Design and implement a sphere class that contains instance data that represents the sphere's diameter.
//************************************************** ************************************************** **
public class Sphere {
public Sphere(double r) {
// TODO Auto-generated constructor stub
}
//--------------------------------------------------------------------------------------------------
//Computes the surface area and volume of a sphere.
//--------------------------------------------------------------------------------------------------
{
double r,volume,surfacearea;
volume= ((4/3)*(3.14159)*(r^3));
surfacearea= (4*3.14159*(r^2));
Sphere s= new Sphere(r);
is this how the constructor works??
- 02-04-2011, 01:19 AM #4
Does it compile? Does it run?
Java Code:public class Sphere { public Sphere(double r) { // TODO Auto-generated constructor stub } { // what is this for? double r,volume,surfacearea; // declare instance variables at the top of the class volume= ((4/3)*(3.14159)*(r^3)); // cannot have code floating about anywhere in the class surfacearea= (4*3.14159*(r^2)); // same as above // missing something?
- 02-04-2011, 01:25 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 33
- Rep Power
- 0
- 02-04-2011, 01:28 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Not quite, the constructor simply lets you create the object. for example, if you do not supply a constructor and use
The compiler automatically supplies one which sets all instance variables for you(either 0 or 0.0 for primitives, and null for objects).Java Code:Sphere x = new Sphere();
If you declare a constructor that takes an argument it will no longer work with supply a default constructor.
So you would want to generally create 2 constructors, one default, and one that takes args(of course, you can make as many as you want) So you want to create a constructor which will set the diameter? It should take an argument then(which will set the sphere instance value) With the no arg constructor you can set a default constructor to control what happens if someone does
So by now you should have two constructs, the next step is to work on methods. Ill give you the contracts to work fromJava Code:Sphere x = new Sphere();
Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)Java Code:public double getDiameter(){ //your code } public double computeVolume(){ //your code } public double computeArea(){ //your code }
- 02-04-2011, 01:34 AM #7
Yes you can have the formulas inside the class but they also need to be inside a method or constructor not just floating about like you have them.
For ease of reading you should declare instance variables at the top of the class, after the class declaration but before any constructors and methods. Just like my example above. Why? When someone else reads your code and sees a variable then they do not have to scroll up and down the whole class looking for the declaration. All they need is to go to the top.
- 02-04-2011, 01:55 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
To help you understand how to construct a class view below
Java Code:class SomeClass{ //instance variables float f; int i; double d; Object o; char c; short s; //constructors SomeClass(){ //no arg constructor(provided for you if you don't create one) } SomeClass(dataType args){ //takes arguments to initialize the instance variables when the object is //created. If you create an arg constructor, the default is not supplied for //you by the compiler } //methods public int method1(args){ //you can have as many as you want, they have a return type of any primitive type //any object, or void, they can also have many arguments } public double method2(args){ } public float method3(args){ } }
- 02-04-2011, 02:48 AM #9
Member
- Join Date
- Feb 2011
- Posts
- 33
- Rep Power
- 0
- 02-04-2011, 02:50 AM #10
Member
- Join Date
- Feb 2011
- Posts
- 33
- Rep Power
- 0
- 02-04-2011, 02:52 AM #11
Yes you can use any variable name. That was just an example of what you might put in your main method to test your Sphere class.
- 02-04-2011, 02:58 AM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I would suggest you start from scratch, it might be easier.
Sphere x is just the name you use to reference the object. I simply used x as an example, you can use anything you want as the name
it's just the reference name, the same as when you declare a primitive typeJava Code:Sphere x = new Sphere(); Sphere y = new Sphere(); Sphere aSphere = new Sphere(); Sphere anotherSphere = new Sphere(); Sphere thirdSphere = new Sphere();
When you design a class, first create the instance variables the class should have. The instance variables can be thought of the state an object will have. If you have a dog class you can have an instance variable for name, and weight.Java Code:int aNumber; int x = 1; int anotherNumber = 4;
The second thing you should work on creating is constructors, then finally methods.
Start small and build up
for example, first create
This will create an object, set the instance variable and then print it.Java Code:class aClass{ int x public static void main(String[] args){ aClass classx = new aClass(); classx.x = 1; System.out.println(classx.x); } }
I suggest you start like that, testing each new item you add making sure it compiles and runs before you move on to the next step of the class.
- 02-04-2011, 01:21 PM #13
Member
- Join Date
- Feb 2011
- Posts
- 33
- Rep Power
- 0
When I type in "Sphere()" do I put like radius inside the "()"??
- 02-04-2011, 02:44 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Yes, but (from your OP:
It's the diameter you're supposed to be using apparently.I have to design the Sphere constructor to accept and initialize the diameter...
- 02-04-2011, 02:55 PM #15
Member
- Join Date
- Feb 2011
- Posts
- 33
- Rep Power
- 0
I get it now, I think, I want to show my code but the copy and paste here does not look right.
- 02-04-2011, 03:17 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,417
- Blog Entries
- 7
- Rep Power
- 17
Put a [code] tag before your code and a [/code] tag following your code; you can also select all your code and press the '#' button.
w.r.t. your question: the way you can create a new object X given a class X depends on the constructor(s), i.e if you have a constructor X(double y) you can construct an X as new X(42.42); i.e. pass a single double as a parameter to that constructor. similarly if you have a constructor X(int z) you have to pass an int, such as new X(54) etc. etc. The parameter lists (especially the type of the parameters) determine how you can, or have to create a new object of class X. Your constructor needs a value of type double, so you have to create a Sphere like this: new Sphere(42.0) (or any other double value will do fine).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Need help with constructors
By tpfaff in forum New To JavaReplies: 10Last Post: 10-22-2010, 04:33 AM -
Constructors
By suresh.sa in forum New To JavaReplies: 5Last Post: 10-20-2010, 12:10 AM -
constructors?
By shroomiin in forum New To JavaReplies: 4Last Post: 10-13-2009, 02:14 PM -
Constructors
By new2java2009 in forum New To JavaReplies: 5Last Post: 08-18-2009, 06:46 AM -
Help with constructors
By Minime in forum New To JavaReplies: 3Last Post: 04-09-2008, 07:59 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks