Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

MOOC Excercise 44

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code:



I tried doing exercise 44(14.3) on MOOC https://materiaalit.github.io/2013-oo-programming/part1/week-3/

This is my code so far, I tried from many different ways but that only end up with errors.

halp
 
Sheriff
Posts: 6551
176
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What errors do you get from the code above?  Copy and paste the entire error message here.
 
Rancher
Posts: 126
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a look at the exercise. You were not supposed to read anything from the console, just complete the method. Regarding your error, your method needs to return an int in every scenario.
 
Dan Red
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:What errors do you get from the code above?  Copy and paste the entire error message here.



Console is blank, but there is error by 18 line "This method must return a result of type int".

Adrian Grabowski wrote:Regarding your error, your method needs to return an int in every scenario



But it is returning at the lines 20 and 23.
 
Knute Snortum
Sheriff
Posts: 6551
176
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But there is no way to know that those two if statements will cover every situation. [Edit: and they don't]  If it's an error to get past the two ifs, throw an exception.
 
Bartender
Posts: 2431
107
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you meant to say that the above code throws a compilation error:

Java compiler wrote:error: missing return statement

You have written a method that has a return statement in an if condition. This is not a problem if all of the possible exits from the method result in either an exception or a return statement. Have a look at the pseudo code below:

Which one do you feel works for your solution ? I would expect your method to behave the same as Math.min() method when both operands are the same.
 
Knute Snortum
Sheriff
Posts: 6551
176
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is even another situation that might work:
 
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The old Sun style guide would have you write return condition ? A : B; instead. That would work because you are returning something for every condition.
 
Dan Red
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I understand now



That works, thank you everyone.
 
Sheriff
Posts: 14614
243
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dan Red wrote:That works, thank you everyone.


Do you mean works, i.e., produces correct results, or "works," i.e., it runs without blowing up?

From what I can tell, least(8, 7) will return 8. I would have thought that the result should be 7.
 
Campbell Ritchie
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's have a look at the old Sun style guide. It says to avoid code likeIt says to write instead,You may want () around an expression containing ?: because ?: has a low precedence.
 
Dan Red
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Dan Red wrote:That works, thank you everyone.


Do you mean works, i.e., produces correct results, or "works," i.e., it runs without blowing up?

From what I can tell, least(8, 7) will return 8. I would have thought that the result should be 7.



lol I just noticed that it doesn't produce correct result, gonna edit.
 
Saloon Keeper
Posts: 10993
243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dan Red wrote:gonna edit


Please don't, it messes up the narrative of this topic. I reverted your edits. Please explain your mistake in a new post.
 
Dan Red
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay.

The mistake was that the program had to point out the least number, and it was pointing out the greatest number.

Instead of

first > second
return  first

should be

first > second
return second
 
Marshal
Posts: 7347
498
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dan Red wrote:first > second
return  first

should be

first > second
return second


My brains working harder than they should in order to understand how you said it "should be".

I'd expect it to be:

One would think not a big difference, well, for me it is.

Also I've added <=, I find it easier to understand that way. I'd expect that majority would find it easier that way, simply because looking to "if" part one could identify in case of equal values what value would be returned without even looking how "else" part is implemented.
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!