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.

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.

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.

//--------------------------------------------------------------------------------------------------
//Computes the surface area and volume of a sphere.
//--------------------------------------------------------------------------------------------------

Not quite, the constructor simply lets you create the object. for example, if you do not supply a constructor and use

Java Code:

Sphere x = new Sphere();

The compiler automatically supplies one which sets all instance variables for you(either 0 or 0.0 for primitives, and null for objects).
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

Java Code:

Sphere x = new Sphere();

So by now you should have two constructs, the next step is to work on methods. Ill give you the contracts to work from

Crap I'm messing up, so I cant have the formulas inside the class but I can have them outside the class?? And what do you mean by declaring instance variables at the to of the class??

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.

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){
}
}

Not quite, the constructor simply lets you create the object. for example, if you do not supply a constructor and use

Java Code:

Sphere x = new Sphere();

The compiler automatically supplies one which sets all instance variables for you(either 0 or 0.0 for primitives, and null for objects).
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

Java Code:

Sphere x = new Sphere();

So by now you should have two constructs, the next step is to work on methods. Ill give you the contracts to work from

Not quite, the constructor simply lets you create the object. for example, if you do not supply a constructor and use

Java Code:

Sphere x = new Sphere();

The compiler automatically supplies one which sets all instance variables for you(either 0 or 0.0 for primitives, and null for objects).
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

Java Code:

Sphere x = new Sphere();

So by now you should have two constructs, the next step is to work on methods. Ill give you the contracts to work from

it's just the reference name, the same as when you declare a primitive type

Java Code:

int aNumber;
int x = 1;
int anotherNumber = 4;

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.

The second thing you should work on creating is constructors, then finally methods.

I get it now, I think, I want to show my code but the copy and paste here does not look right.

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