This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
I am currently studying java but the book im learning from is not particularly great. Please could some one answere some of the following questions. 1)how do I Pass a Value as an argument to a method and add it to another field 2)I know that method overlaoding is something to do with getting methods to do things in a different way depending on how you call it . Could someone please give me a better explanation and an example. 3)what is inheritance and could you plaese give an example. any help on any of the questioins would be greatly appreciated. Thank you
1)how do I Pass a Value as an argument to a method and add it to another field
Is the "other" field local to the method? Can you be a little more specific? Thanks.
Please ignore post, I have no idea what I am talking about.
antony magnier
Greenhorn
Joined: Jul 10, 2001
Posts: 11
posted
0
Thanks for the first answere question 3) Its in a question im answering where i have to model the customer accounts of an electricity company ive done the first part which is write a class definition initailising a set of variables: Customer type,power used and ammount outstanding. the second thing i have to do is Write a method to increase the ammount of power used by a customer. The ammount of power should be passed as an argument to the method then added to the power used field
Karen Lowe
Greenhorn
Joined: Aug 30, 2001
Posts: 3
posted
0
Originally posted by antony magnier: Write a method to increase the ammount of power used by a customer. The ammount of power should be passed as an argument to the method then added to the power used field
Antony, I am a greenhorn too, but I'd write a method for your customer object that allows it to update its own powerUsed instance variable. <PRE> public void addPowerUsage( int kilowattsUsed ) { powerUsed += kilowattsUsed; } </PRE> If you're like me you'd use shorter variable identifiers and maybe even use the this keyword to allow you to use the same name in both places. Could someone else chime in to let me know if this is good style? <PRE> public void addPowerUsage( int kw ) { this.kw += kw; } </PRE> Then to add 100 kilowatts to Customer cust you'd call the method from another part of the program and pass in the usage: <PRE> kwUsed = 100; cust.addPowerUsage( kwUsed ); </PRE> Thanks for prompting me to make my first post. Bonne chance! - Karen [This message has been edited by Karen Lowe for code formatting (edited August 30, 2001).]
[This message has been edited by Karen Lowe (edited August 30, 2001).]
antony magnier
Greenhorn
Joined: Jul 10, 2001
Posts: 11
posted
0
Thanks for that it was very helpfull, but can anybody answere my 2nd question aboutmethod overloading.
Overloading is used to have multiple methods with the same name that do slightly different things... to differentiate between the methods they must have different parameters.
The most common methods that are overloaded are constructors... suppose you have a Person class, with name, age and kiloWatt usage attributes :
When creating a Person you may want to specify the initial attributes... but you may not want to specify all of them every time, and sometimes you'll just need a Person and don't want to specify any... You could add these overloaded constructors to the class to do this :
Hope this helps you understand overloading... -Nate
-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I have to say something about method overloading. In very common terms, we can say that it is doing the same thing on different things. For example "wash". You wash a car, wash a floor, wash a dirty pot. "Wash" is the method and you are overloading it with different objects. Otherwise it would be very difficult. That is you would have to say "carWash the car" or "floorWash the floor". That's enough about washing / method overloading
------------------
-----<br />If you think education is expensive, try ignorance.