Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #39007 > unrolled thread
| Started by | cdalten@gmail.com |
|---|---|
| First post | 2019-07-18 06:29 -0700 |
| Last post | 2019-07-18 13:47 -0400 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.java.programmer
Java missing return statement question... cdalten@gmail.com - 2019-07-18 06:29 -0700
Re: Java missing return statement question... Andreas Leitgeb <avl@logic.at> - 2019-07-18 13:44 +0000
Re: Java missing return statement question... cdalten@gmail.com - 2019-07-18 06:54 -0700
Re: Java missing return statement question... Eric Douglas <e.d.programmer@gmail.com> - 2019-07-18 07:51 -0700
Re: Java missing return statement question... Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-07-18 13:47 -0400
| From | cdalten@gmail.com |
|---|---|
| Date | 2019-07-18 06:29 -0700 |
| Subject | Java missing return statement question... |
| Message-ID | <67615856-0be2-4c3f-ae4c-c9f3cb379451@googlegroups.com> |
I don't quite understand why something like the following gives a missing return statement.
public int withoutDoubles(int die1, int die2, boolean noDoubles) {
if (die1 != die2) {
return die1 + die2;
}
if (noDoubles == true) {
if ((die1 == die2) && (die1 != 6 || die2 != 6)) {
return (die1 + die2) + 1;
} else if (die1 == 6 || die2 == 6) {
return die1 + 1;
}
} else {
return die1 + die2;
}
}
But something like this this doesn't..
public int blueTicket(int a, int b, int c) {
int ab = a + b;
int bc = b + c;
int ac = a + c;
if (ab == 10 || bc == 10 || ac == 10) {
return 10;
}
//ab - bc = 10;
//ab - ac = 10;
if ((ab - bc) == 10 || (ab - ac) == 10 ) {
return 5;
} else {
return 0;
}
}
I both cases the return statement is in both the if and else clause.
[toc] | [next] | [standalone]
| From | Andreas Leitgeb <avl@logic.at> |
|---|---|
| Date | 2019-07-18 13:44 +0000 |
| Message-ID | <slrnqj0tu4.36g.avl@logic.at> |
| In reply to | #39007 |
cdalten@gmail.com <cdalten@gmail.com> wrote:
> I don't quite understand why something like the following gives a missing return statement.
> public int withoutDoubles(int die1, int die2, boolean noDoubles) {
> if (die1 != die2) {
> return die1 + die2;
> }
Since furtheron obviously die1==die2, the following part of code
seems needlessly overcomplicated.
>
> if (noDoubles == true) {
> if ((die1 == die2) && (die1 != 6 || die2 != 6)) {
> return (die1 + die2) + 1;
> } else if (die1 == 6 || die2 == 6) {
> return die1 + 1;
> }
Because ^ here is one "branch" that doesn't currently return.
I mean the "else if"'s (missing) "else" case, where
noDoubles == true is true
(die1 == die2) && (die1 != 6 || die2 != 6) is false
die1 == 6 || die2 == 6 is false
As a human, we can see, that this combination cannot happen
due to equal dice, but apparently the Java compiler cannot.
> } else {
> return die1 + die2;
> }
> }
[toc] | [prev] | [next] | [standalone]
| From | cdalten@gmail.com |
|---|---|
| Date | 2019-07-18 06:54 -0700 |
| Message-ID | <7dec1ee5-a87e-432b-83cc-f5782f185e23@googlegroups.com> |
| In reply to | #39008 |
On Thursday, July 18, 2019 at 6:44:45 AM UTC-7, Andreas Leitgeb wrote:
> cdalten@gmail.com <cdalten@gmail.com> wrote:
> > I don't quite understand why something like the following gives a missing return statement.
> > public int withoutDoubles(int die1, int die2, boolean noDoubles) {
> > if (die1 != die2) {
> > return die1 + die2;
> > }
>
> Since furtheron obviously die1==die2, the following part of code
> seems needlessly overcomplicated.
>
Probably. This was my solution your typical "programming interview" type question. I guess I was more concerned with understanding the problem and then having the method pass *all* the test cases in an allotted period of time.
[toc] | [prev] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2019-07-18 07:51 -0700 |
| Message-ID | <db910e06-39dd-42c3-991f-a2cb23dbcadb@googlegroups.com> |
| In reply to | #39007 |
On Thursday, July 18, 2019 at 9:30:03 AM UTC-4, cda...@gmail.com wrote:
> I don't quite understand why something like the following gives a missing return statement.
>
Always make sure every branch returns a value if the method return is anything other than void. A compiler will complain about the impossible branch.
public static boolean test() {
if (1 != 2) {
return false;
}
}
"This method must return a result of type boolean"
else is not needed if the condition will otherwise exit the method.
Anything after if with no else is your else
public int withoutDoubles(int die1, int die2, boolean noDoubles) {
if (die1 != die2) {
return die1 + die2;
}
if (noDoubles == true) {
if ((die1 == die2) && (die1 != 6 || die2 != 6)) {
return (die1 + die2) + 1;
} else if (die1 == 6 || die2 == 6) { // this if had no else
return die1 + 1;
} else { // impossible condition??
throw new Error("how did we get here??");
}
} // else { // all other branches left the method? this is not needed
return die1 + die2;
// }
}
>
> But something like this this doesn't..
>
public int blueTicket(int a, int b, int c) {
int ab = a + b;
int bc = b + c;
int ac = a + c;
if (ab == 10 || bc == 10 || ac == 10) {
return 10;
}
//ab - bc = 10;
//ab - ac = 10;
if ((ab - bc) == 10 || (ab - ac) == 10 ) {
return 5;
} // else { // all other branches left the method, else not needed
return 0;
// }
}
> I both cases the return statement is in both the if and else clause.
just not in the implied else
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2019-07-18 13:47 -0400 |
| Message-ID | <qgqbbl$d98$1@dont-email.me> |
| In reply to | #39007 |
On 7/18/2019 9:29 AM, cdalten@gmail.com wrote:
> I don't quite understand why something like the following gives a missing return statement.
Andreas Leitgeb has already answered your main question. I'd
just like to point out one additional oddity:
> if (noDoubles == true) {
You begin with the boolean `noDoubles', which (necessarily) has one
of the values `true' or `false'. You want to find out whether the value
is `true', so you compare it to `true' -- and since the comparison
produces either `true' or `false', you have made no "progress." That
is, you *still* have a value that is either `true' or `false', exactly
as you did before making the comparison. What next?
You could continue by writing
if ((noDoubles == true) == true) {
... but you're still in the same situation, so onward to
if (((noDoubles == true) == true) == true) {
... but that's still no help, either. You could try something
along the lines of
if ((((noDoubles == true) == false) != true) == true) {
... but somehow I don't see this line of attack getting anywhere.
"Anywhere useful," that is.
Instead, consider
if (noDoubles) {
... and your Note To Self For Future Reference should be: "It may,
rarely, make sense to compare two boolean values, but comparing a
boolean to a constant never makes sense."
--
esosman@comcast-dot-net.invalid
Five hundred fifty-two days to go.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web