File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple questions. Please help.

 
antony magnier
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
antony magnier
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that it was very helpfull, but can anybody answere my 2nd question aboutmethod overloading.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overloading is when you have more than one method with the same name. Here is a really bad example.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Must better than my example, thanks.
 
Iqbal Ali
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

------------------
 
Gaia Nathan
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic