Results 1 to 3 of 3
  1. #1
    incog03 is offline Member
    Join Date
    Jul 2009
    Posts
    1
    Rep Power
    0

    Default Calling accessors

    Hi guys, im completely stuck on this, how do i call an accessor method to deal with user input? Just started the other week

    KeyboardInput.readDouble("Enter Radius: ");

    // accessor methods
    public Balloon()
    {
    }

    public Balloon(String Bname, double Bradius, int BnumBalloon)
    {
    name = Bname;
    radius = Bradius;
    numBalloon = BnumBalloon;
    }

    // constructors
    public double getRadius()
    {
    return radius;
    }


    I'm trying to call the getRadius constructor ? or the balloon accessor?

    I've had a little more experience in c++, so i guess what I would have done for c++ is getRadius() = KeyboardInput.readDouble("Enter Radius: ");
    I've spent a whiles trying different ways of doing this but yeah. If anyone could take a look, would be much appreciated.
    Last edited by incog03; 07-27-2009 at 07:07 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    getRadius() is not a constructor. The constructor is Balloon(...) as the constructor has the same name as the class and does not have a return type (not even void).

    getRadius is an accessor method, and is called by first creating a Balloon object then calling the accessor on the object:
    Java Code:
    Balloon myBalloon = new Balloon("My Balloon", 20.0, 5);
    double radius = myBalloon.getRadius(); // call accessor here and place into variable
    System.out.println("the radius is: " + radius);

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    It looks like you are trying to reset the radius of the balloon. To do that, you will need a setRadius method:
    Java Code:
    public void setRadius(double rad){
         radius = rad;
    }
    If you're still stuck or I'm just not understanding you, try some samples in C++, maybe I can convert them to Java for you, I've done some work with C++ myself.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Replies: 6
    Last Post: 03-11-2009, 05:38 PM
  2. Need help calling from a different method
    By Mayur in forum New To Java
    Replies: 6
    Last Post: 03-08-2009, 10:27 PM
  3. Using accessors properly
    By LifeWithJava in forum New To Java
    Replies: 2
    Last Post: 12-23-2008, 03:49 PM
  4. need help calling methods
    By lowpro in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 10:53 AM
  5. Help with Calling a method
    By Albert in forum New To Java
    Replies: 3
    Last Post: 07-10-2007, 04:27 PM

Posting Permissions

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