Results 1 to 16 of 16
  1. #1
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

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

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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:
    Java Code:
    class XXXXXXXX {
        // instance variables
    
        // constructor
    
        // methods
        public String toString() {
            // code
        }
    }
    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.

  3. #3
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default

    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??

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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?

  5. #5
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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?
    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??

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    Java Code:
    public double getDiameter(){
    //your code
    }
    
    public double computeVolume(){
    //your code
    }
    
    public double computeArea(){
    //your code
    }
    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by thorobred View Post
    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.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

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

  9. #9
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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
    Java Code:
    public double getDiameter(){
    //your code
    }
    
    public double computeVolume(){
    //your code
    }
    
    public double computeArea(){
    //your code
    }
    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    what is the Sphere "x" at the beginning?? is that just some variable or can I use any variable??

  10. #10
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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
    Java Code:
    public double getDiameter(){
    //your code
    }
    
    public double computeVolume(){
    //your code
    }
    
    public double computeArea(){
    //your code
    }
    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Also, the previous code that I had, should I just do it over from scratch with the examp. you just showed??

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    Java Code:
    Sphere x = new Sphere();
    Sphere y = new Sphere();
    Sphere aSphere = new Sphere();
    Sphere anotherSphere = new Sphere();
    Sphere thirdSphere = new Sphere();
    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.

    Start small and build up

    for example, first create
    Java Code:
    class aClass{
    int x
    public static void main(String[] args){
      aClass classx = new aClass();
      classx.x = 1;
      System.out.println(classx.x);
    }
    }
    This will create an object, set the instance variable and then print it.

    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.

  13. #13
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default

    When I type in "Sphere()" do I put like radius inside the "()"??

  14. #14
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default

    Yes, but (from your OP:
    I have to design the Sphere constructor to accept and initialize the diameter...
    It's the diameter you're supposed to be using apparently.

  15. #15
    thorobred is offline Member
    Join Date
    Feb 2011
    Posts
    33
    Rep Power
    0

    Default

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

  16. #16
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by thorobred View Post
    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).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Need help with constructors
    By tpfaff in forum New To Java
    Replies: 10
    Last Post: 10-22-2010, 04:33 AM
  2. Constructors
    By suresh.sa in forum New To Java
    Replies: 5
    Last Post: 10-20-2010, 12:10 AM
  3. constructors?
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 10-13-2009, 02:14 PM
  4. Constructors
    By new2java2009 in forum New To Java
    Replies: 5
    Last Post: 08-18-2009, 06:46 AM
  5. Help with constructors
    By Minime in forum New To Java
    Replies: 3
    Last Post: 04-09-2008, 07:59 AM

Posting Permissions

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