aspose file tools*
The moose likes Beginning Java and the fly likes Multiple questions.  Please help. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multiple questions.  Please help." Watch "Multiple questions.  Please help." New topic
Author

Multiple questions. Please help.

antony magnier
Greenhorn

Joined: Jul 10, 2001
Posts: 11
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
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986


3)what is inheritance and could you plaese give an example.

See if this helps: http://java.sun.com/docs/books/tutorial/java/concepts/inheritance.html

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
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
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
Thanks for that it was very helpfull, but can anybody answere my 2nd question aboutmethod overloading.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Overloading is when you have more than one method with the same name. Here is a really bad example.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Must better than my example, thanks.
Iqbal Ali
Greenhorn

Joined: Aug 21, 2001
Posts: 9
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.
Gaia Nathan
Ranch Hand

Joined: Aug 01, 2001
Posts: 62
Hi Antony,
Try visiting these sites. They have a lot of good tips. http://www.jchq.net/ http://www.ibiblio.org/javafaq/course/
Gaia.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple questions. Please help.