Sarah Mitchell wrote:First problem, I don't really understand the whole concept of ( arguments and parameters )
I don't know how to use them and I don't even know when I should use them !
So I would really appreciate it if someone could give me an idea on when do we need to have arguments, and how can we use them ? syntax or anything ?
Second, this (return) thing is also a little annoying for me at this point, I mean why use the command return when I can just use ( variable = value or formula ) ? anyone care to explain ?
I'm so sorry that this had to be my first post, I just really wanna learn all about Java. And excuse my English if there were any mistakes or what so ever, English is not my native language =)
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
why use the command return when I can just use ( variable = value or formula )
Winston Gutkowski wrote:
So, if you were going to write a Java method to do it, you might write something like:
Winston
Jeff Verdegan wrote:Also, did you study these, or their equivalents in some other tutorial or book? If not, you should, and if you did, what parts didn't you understand?
http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html
As for this:why use the command return when I can just use ( variable = value or formula )
Two points:
1) If you're talking inside the method called, then sometimes its result is to just set the value of a member variable. However that's a different scenario. When you do that, you're changing the state of an object, and you want that state to persist as long as that object exists, unless somebody comes along and changes it again. For instance, in a Student object, I might store a currentGPA value. That stays with the student until something comes along to change it. Returning a value, on the other hand, a way of providing the result of some operation or querying the state of the object without actually changing it. For instance, a Person object might have a private Date birthDate member variable. If I want to know that person's age in seconds at a particular instant in time, I might call person.getAgeInSeconds(some time). The method will then use the birthDate and the given time to calculate the age. It wouldn't make sense to update the state of the Person object for that, as a person's age in seconds as of some arbitrary instant is not an attribute of that person.
2) When you do variable = forumula, if that "formula" is, for instance, x = Math.sin(y), we couldn't do that unless the sin() method returned a value.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Sarah Mitchell wrote:Ok, so if I made such a code, should I assign the formula's outcome to the variable 'result' by using the equal sign '=' ? I mean something like this :
and in this case should we also use return as well ?
but then how will it know in which variable to store the outcome if I have more than one variable ?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Campbell Ritchie wrote:Welcome to the Ranch
Careful with books; some books are very good, others not so good. We have some book reviews which we hope will help you.
fred rosenberger wrote:I think of calling a method as asking a question. "hey...What time is it?" or "What is the sine of 27.43 degrees"? or "what is the result set when you query THIS database with THAT sql query?"
Note that each and every question can only return one answer. I can't say "What time is it and what is Fred's dog's name?" and expect to get back "8:30 a.m. and Mamie". One question, one answer.
Now, that answer can be a rather complicated single thing...the aforementioned result set may contain hundreds or thousands of records, but they are all contained in one object (which may then have methods for accessing those records).
The reason you write methods rather than just 'in-lining' the calculation everywhere is that the methods can actually be rather complicated. You may have a method that connects to the database, passes the query, gets the results, formats them, and a bunch of other things. You don't want to have the same basic 200 lines of code written over and over again. Plus, if you write it in a single method, you can test that method extensively. Once you know it works, you KNOW it works, and you don't have to worry about it anymore. If you re-wrote they method by hand a bunch of times, you increase the changes of introducing an error somewhere.
Winston Gutkowski wrote:
Sarah Mitchell wrote:Ok, so if I made such a code, should I assign the formula's outcome to the variable 'result' by using the equal sign '=' ? I mean something like this :
Pretty much; although getting the answer may take more than just one line.
and in this case should we also use return as well ?
Yes. Any method that is defined to return a value other than void must contain at least one return statement.
but then how will it know in which variable to store the outcome if I have more than one variable ?
Because you tell it which one to return. Despite all the advances of the last 50 years, computers are still basically stupid - they do exactly what they're told.
HIH
Winston
Sarah Mitchell wrote:
OMG, I totally forgot about what I learned in the very beginning. the void methods doesn't return any value but other types does, and that's where we need to use arguments !
uffff, I kinda hate myself for forgetting this.
At least you gave her the simple version and didn’t confuse her with really dubious classifications of methods!Jeff Verdegan wrote: . . . Partly right. A void method doesn't return any value, that is true. However,r eturning a value and taking arguments are totally independent of each other.
. . .
Campbell Ritchie wrote:At least you gave her the simple version and didn’t confuse her with really dubious classifications of methods!
I would have to take legal advice before answering that questionJeff Verdegan wrote: . . . Do you now, or did you at some point, work for the government or the military? . . .
Campbell Ritchie wrote:Maybe we should bring this thread back to its official topic.
Campbell Ritchie wrote:Maybe we should bring this thread back to its official topic.
Jeff Verdegan wrote:
Campbell Ritchie wrote:Maybe we should bring this thread back to its official topic.
I think that ball is in Sarah's court now. We haven't heard form her in a while, but as far as I can tell, all her questions have been answered.
Sarah Mitchell wrote:I mean what is this public Account() ? it's not a class header of course, it's not variables deceleration, it's not a method(at least I don't think so), so what is it ?
As far as I know it's only about assigning some values to the variables, but in this case why did they use ' this. ' ? what is that exactly ? why did we have to use it in this case? why didn't they just say holder = ""; or balance = 0.0; ?
and what other cases it must be used in ?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Sarah Mitchell wrote:From the bottom of my heart THANK YOU.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|