Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #29373 > unrolled thread
| Started by | Scott Dunning <scottwd01@gmail.com> |
|---|---|
| First post | 2014-07-27 17:46 -0700 |
| Last post | 2014-07-28 21:29 +0200 |
| Articles | 11 — 6 participants |
Back to article view | Back to comp.lang.java.programmer
Help understanding methods Scott Dunning <scottwd01@gmail.com> - 2014-07-27 17:46 -0700
Re: Help understanding methods Knute Johnson <eternal@knutejohnson.com> - 2014-07-27 18:13 -0700
Re: Help understanding methods Scott Dunning <scottwd01@gmail.com> - 2014-07-27 18:19 -0700
Re: Help understanding methods Knute Johnson <eternal@knutejohnson.com> - 2014-07-27 18:49 -0700
Re: Help understanding methods Knute Johnson <eternal@knutejohnson.com> - 2014-07-27 21:36 -0700
Re: Help understanding methods Martin Gregorie <martin@address-in-sig.invalid> - 2014-07-28 01:18 +0000
Re: Help understanding methods Scott Dunning <scottwd01@gmail.com> - 2014-07-27 18:56 -0700
Re: Help understanding methods "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2014-07-28 12:07 +0100
Re: Help understanding methods "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2014-07-28 12:44 +0100
Re: Help understanding methods Roedy Green <see_website@mindprod.com.invalid> - 2014-07-28 07:20 -0700
Re: Help understanding methods Josip Almasi <joe@vrspace.org> - 2014-07-28 21:29 +0200
| From | Scott Dunning <scottwd01@gmail.com> |
|---|---|
| Date | 2014-07-27 17:46 -0700 |
| Subject | Help understanding methods |
| Message-ID | <lr46d1$ub3$1@dont-email.me> |
So, I'm very new to Java, I have a summer college course and we're on
functions (methods) and I'm having a bit of trouble understanding them.
There is a question on a lab I'm having a little trouble with:
"Write a method called MaxOfThree that accepts three integer parameters
and returns the largest of the three."
This is what I have so far but I'm not sure whats wrong. I added the
print statement at the end because I wasn't getting a return value when
I ran it but now I'm getting errors and it's not compiling. If you could
help me understand methods a bit more I'd greatly appreciate it. For
instance how the parameters work and calling it and if what's included
in the function call is correct and how that works. I just get so
confused when I read through the material and was hoping for an
explanation in more layman's terms. Thanks for any help, here is what I
have so far.
public class Test {
public static void main(String[] args) {
int a = 2, b = 3, c = 4;
int maxValue = max3(a, b, c);
}
public static int max3(int a, int b, int c) {
int max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
System.out.println(max);
}
}
Here are the errors I'm receiving just in case...
Test.java:16: error: unreachable statement
System.out.println(max);
^
Test.java:17: error: missing return statement
}
^
2 errors
[toc] | [next] | [standalone]
| From | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| Date | 2014-07-27 18:13 -0700 |
| Message-ID | <lr483r$bpk$1@dont-email.me> |
| In reply to | #29373 |
On 7/27/2014 17:46, Scott Dunning wrote:
> So, I'm very new to Java, I have a summer college course and we're on
> functions (methods) and I'm having a bit of trouble understanding them.
>
> There is a question on a lab I'm having a little trouble with:
>
> "Write a method called MaxOfThree that accepts three integer parameters
> and returns the largest of the three."
>
> This is what I have so far but I'm not sure whats wrong. I added the
> print statement at the end because I wasn't getting a return value when
> I ran it but now I'm getting errors and it's not compiling. If you could
> help me understand methods a bit more I'd greatly appreciate it. For
> instance how the parameters work and calling it and if what's included
> in the function call is correct and how that works. I just get so
> confused when I read through the material and was hoping for an
> explanation in more layman's terms. Thanks for any help, here is what I
> have so far.
>
> public class Test {
>
> public static void main(String[] args) {
> int a = 2, b = 3, c = 4;
> int maxValue = max3(a, b, c);
> }
>
> public static int max3(int a, int b, int c) {
> int max = a;
> if (b > max) max = b;
> if (c > max) max = c;
> return max;
> System.out.println(max);
> }
> }
>
>
> Here are the errors I'm receiving just in case...
>
> Test.java:16: error: unreachable statement
> System.out.println(max);
> ^
> Test.java:17: error: missing return statement
> }
> ^
> 2 errors
The return statement ends the code path so the System.out line can never
be reached.
The second error is because there is not a return statement as the last
thing in the method.
To fix it: move the print statement to the main method
That should do it.
--
Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | Scott Dunning <scottwd01@gmail.com> |
|---|---|
| Date | 2014-07-27 18:19 -0700 |
| Message-ID | <lr48at$c53$1@dont-email.me> |
| In reply to | #29374 |
> The return statement ends the code path so the System.out line can never > be reached. > > The second error is because there is not a return statement as the last > thing in the method. > > To fix it: move the print statement to the main method > > That should do it. > Awesome, thanks Knute! I have a couple other questions about this if you guys don't mind. What I'm a little unclear about is when calling the function why I need maxValue? I kinda just added that because of other methods I've seen. Why isn't (like python) I'm just calling the method like this... max3(a, b, c);? Why do I need a new (..variable I guess)?
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| Date | 2014-07-27 18:49 -0700 |
| Message-ID | <lr4a83$lo9$1@dont-email.me> |
| In reply to | #29375 |
On 7/27/2014 18:19, Scott Dunning wrote: > >> The return statement ends the code path so the System.out line can never >> be reached. >> >> The second error is because there is not a return statement as the last >> thing in the method. >> >> To fix it: move the print statement to the main method >> >> That should do it. >> > Awesome, thanks Knute! > > I have a couple other questions about this if you guys don't mind. > > What I'm a little unclear about is when calling the function why I need > maxValue? I kinda just added that because of other methods I've seen. > Why isn't (like python) I'm just calling the method like this... > max3(a, b, c);? > > Why do I need a new (..variable I guess)? You don't. System.out.println(max3(1,2,3)); -- Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| Date | 2014-07-27 21:36 -0700 |
| Message-ID | <lr4k00$8qq$1@dont-email.me> |
| In reply to | #29379 |
On 7/27/2014 20:10, Stefan Ram wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>> void f
>> ( final int ... a );
>
> public static int max( final java.lang.Integer ... args )
> { return java.util.Arrays.asList( args ).stream().
> max( java.lang.Integer::compare ).get(); }
>
The final is implied (I think) and the java.lang. s are unnecessary but
it works just fine. I've got a way to go before I can write that way
though.
--
Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Date | 2014-07-28 01:18 +0000 |
| Message-ID | <lr48dr$4ur$1@dont-email.me> |
| In reply to | #29373 |
On Sun, 27 Jul 2014 17:46:53 -0700, Scott Dunning wrote:
> public class Test {
>
> public static void main(String[] args) {
> int a = 2, b = 3, c = 4;
> int maxValue = max3(a, b, c);
> }
>
> public static int max3(int a, int b, int c) {
> int max = a;
> if (b > max) max = b;
> if (c > max) max = c;
> return max; System.out.println(max);
> }
> }
>
The compilation error occurred because the print statement is after the
return statement. The compiler is telling you that it can never be
executed because the preceding return will have forced an exit from the
method before the print statement is executed.
The other problem is that, without a print statement in main() you'll
never know what max3() returned. Fix those two problems and it will
work, though calling max3() a few more times (and displaying the result
each time) with different arguments values with the values in a different
order will give you more confidence in its correctness.
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
[toc] | [prev] | [next] | [standalone]
| From | Scott Dunning <scottwd01@gmail.com> |
|---|---|
| Date | 2014-07-27 18:56 -0700 |
| Message-ID | <lr4afk$ngv$1@dont-email.me> |
| In reply to | #29373 |
>
> In the end, it is easier when the layman learns the language's
> terms! But you need to take your time! Summer courses are often
> to fast for a human brain to cope with all the information.
Yeah tell me about it! It's super fast and I feel like I'm falling
behind. Just need to study more!
>
>> max3(a, b, c)
> ...
>> public static int max3(int a, int b, int c) {
>
> This is a method invocation with three arguments and a start
> of a method declaration with three parameters. At run-time,
> when the message invocation is being evaluated, each
> argument value is being stored in the corresponding
> parameter, and then the body of the method declaration is
> being executed, and in the end, the value of the return
> expression becomes the value of the method invocation.
>
Ok cool, this helped a lot!
One more quick question. My function call is...
int maxValue = max3(a,b,c);
right?
I kinda just added maxValue because nothing was working and I seen a few
methods being called with an entirely different (variable I guess).
However, I just reran it without the maxValue part and it ran just fine.
Why would you want to create a new what I'm assuming is a variable to
call your function?
Thanks everyone for all your help! Not sure what I'd do without you
guys and gals!
Scott
[toc] | [prev] | [next] | [standalone]
| From | "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> |
|---|---|
| Date | 2014-07-28 12:07 +0100 |
| Message-ID | <K4OdnRfV5LuYskvOnZ2dnUVZ7sidnZ2d@bt.com> |
| In reply to | #29380 |
Scott Dunning wrote:
> One more quick question. My function call is...
>
> int maxValue = max3(a,b,c);
>
> right?
>
> I kinda just added maxValue because nothing was working and I seen a few
> methods being called with an entirely different (variable I guess).
> However, I just reran it without the maxValue part and it ran just fine.
> Why would you want to create a new what I'm assuming is a variable to
> call your function?
There's no /technical/ reason why you need to assign the value returned from
the function (method) to a variable. You can just call:
max3(a,b,c);
if you want. All that does is executes the function and then discards the
value returned from the function. Probably you won't want to do that very
often, though, because in real code -- i.e. code you are running for some
real-world reason (even if it's just curiosity) -- the returned value will mean
something and you'll want to do something with that value (print it out
perhaps, or save as a high score or something).
Normally if a function (method) returns a value, it will be telling you
something interesting or important (that's true in this case too -- without
looking at the value it returns you can't tell whether it's working). If the
function doesn't have anything interesting or worthwhile to tell you about what
it did then there's a way to declare the function as "returning void" -- not
returning anything at all. I won't go into that more because I don't know
whether you've covered that yet.
Also, as someone else mentioned, if all you are going to do is pass the
returned value to another function you can do that "in line", although it's
often a good idea to assign the value to a temporary variable (which has a
descriptive name) so that you/other people can see what's going on more easily.
However, just as an example, here's a method (function) that computes the
maximum of 9 ints using the max3() function you have already defined.
public static int maxOf9(int a1, int a2, int as3, [...snipped...] int a9)
{
return max3(
max3(a1, a2, a3),
max3(a4, a5, a6),
max3(a7, a8, a9));
}
which invokes max3() a total of four times, but uses no explicit temporary
variables[*]. It could as easily be written:
public static int maxOf9(int a1, int a2, int as3, [...snipped...] int a9)
{
int maxOfFirst3 = max3(a1, a2, a3),
int maxOfSecond3 = max3(a4, a5, a6),
int maxOfThird3 = max3(a7, a8, a9));
int overalMax = max3(maxOfFirst3, maxOfSecond3, maxOfThird3);
return overalMax;
}
I leave it to you to decide which you think is clearer. (Personally I prefer
the first in this case, but normally would prefer something more like the
second in more complex -- real-world -- code).
Of course, if you want to use the returned value more than once, then you will
have to save it to a variable. You can only use the "inline" style if you only
want to make one, and immediate, use of that value.
-- chris
[*] at the risk of confusing you: there still are temporary variables to hold
the results of the first 3 calls to max3() but the Java compiler looks after
all that for you, and you don't have to declare anything explicitly -- it's all
done "by magic" for you.
[toc] | [prev] | [next] | [standalone]
| From | "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> |
|---|---|
| Date | 2014-07-28 12:44 +0100 |
| Message-ID | <ZfidnRff0e9Qq0vOnZ2dnUVZ8s2dnZ2d@bt.com> |
| In reply to | #29385 |
I wrote:
> int maxOfFirst3 = max3(a1, a2, a3),
> int maxOfSecond3 = max3(a4, a5, a6),
> int maxOfThird3 = max3(a7, a8, a9));
Sorry, several typos. Should have been:
int maxOfFirst3 = max3(a1, a2, a3);
int maxOfSecond3 = max3(a4, a5, a6);
int maxOfThird3 = max3(a7, a8, a9);
-- chris
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2014-07-28 07:20 -0700 |
| Message-ID | <5tmct914haj4t7k2natmho2fsg55qhasqp@4ax.com> |
| In reply to | #29373 |
On Sun, 27 Jul 2014 17:46:53 -0700, Scott Dunning <scottwd01@gmail.com> wrote, quoted or indirectly quoted someone who said : > return max; > System.out.println(max); If you return, you never get to print. You must print before you return. Return is a verb, that returns right away. It does not mean, "when you eventually finish, here is the result as in Eiffel". -- Roedy Green Canadian Mind Products http://mindprod.com The art of strongly-typed language design is largely arranging that errors are automatically detected as soon as possible in the compose, compile, run cycle.
[toc] | [prev] | [next] | [standalone]
| From | Josip Almasi <joe@vrspace.org> |
|---|---|
| Date | 2014-07-28 21:29 +0200 |
| Message-ID | <lr689v$f8$1@gregory.bnet.hr> |
| In reply to | #29373 |
On 07/28/2014 02:46 AM, Scott Dunning wrote: > So, I'm very new to Java, I have a summer college course and we're on > functions (methods) and I'm having a bit of trouble understanding them. ... Certanly seems so. Let me point out the difference. A method is not a function. It's a pointer to function. An object is a structured collection of pointers to functions and variables. A class is definition of that structure. Give or take, that's how we used to explain back in the day when folks were switching from C to java. Nowadays folks switching from python to java, I need explanation without pointers and structures;> Regards...
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web