Results 1 to 3 of 3
Thread: Calling accessors
- 07-27-2009, 06:02 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 1
- Rep Power
- 0
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 06:07 PM.
-
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);
- 07-28-2009, 07:02 AM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
It looks like you are trying to reset the radius of the balloon. To do that, you will need a setRadius method:
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.Java Code:public void setRadius(double rad){ radius = rad; }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
-
Unreachable Statement (accessors) - works as a main method!
By thomase in forum New To JavaReplies: 6Last Post: 03-11-2009, 04:38 PM -
Need help calling from a different method
By Mayur in forum New To JavaReplies: 6Last Post: 03-08-2009, 09:27 PM -
Using accessors properly
By LifeWithJava in forum New To JavaReplies: 2Last Post: 12-23-2008, 02:49 PM -
need help calling methods
By lowpro in forum New To JavaReplies: 2Last Post: 11-15-2007, 09:53 AM -
Help with Calling a method
By Albert in forum New To JavaReplies: 3Last Post: 07-10-2007, 03:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks