• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Also Need Help Verifying this method works

 
William Koch
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not passing the JUnit test cases for some reason. Can anyone tell me why? The JUnit test cases code is below as well. This might actually help with my other post too.




JUnit stuff....




 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Can anyone tell me why?

Because it is broken.

A hint: what does yield for the array (2,1)?
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your method:



A hint: if the largest element comes first, then the "if" condition holds at its first execution, but never again.
 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
If you can’t solve Ivan Jozsef Balazs’s hint, try writing a few numbers and the values of high1 and high2 after each iteration of the loop.
 
William Koch
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a bunch. I saw the point you all were making but I had to think a few minutes in order to figure out how to fix the problem. I decided to just take into account the different possibilities. Here is my solution :


Thanks again!
 
Junilu Lacar
Bartender
Pie
Posts: 7297
44
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the list had fewer than 2 elements in it, would it still be valid to say there is a second biggest value? It looks like your solution would return Integer.MIN_VALUE even though it isn't an element of the list.
 
Winston Gutkowski
Bartender
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Koch wrote:Here is my solution :...

Seems like you have a redundant check in there. How do you think you might shorten it?

Winston
 
William Koch
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Winston

I am actually not sure. I am not sure. It was an assignment for school so now that I have it working I don't want to mess with it too much currently but I would imagine it has to do with eliminating the first of second else if statement. What would you suggest? I just had a ton of trouble getting it to work yesterday so I did not want to change anything once I got it working but I would love to become more efficient!

@Junilu

Here are the test cases that we were given

{1,2}, // 1
{4,3}, // 3
{6,6}, // 6
{1,2,3,4,5}, // 4
{5,4,3,2,1}, // 4
{5,5,4,4,4}, // 5 (not 4!)
{1,5,5,5,1}, // 5
{1,5,3,-13}, // 3
{-9,-9,-9,0,8,-9}, // 0
{-9,8,-9,0,-9,-9}, // 0
{-900,-50,-15,-32,1000,-22}, // -15
{-999999999,-999999992,-999999998,-999999990}, // -999999992
{Integer.MIN_VALUE,Integer.MAX_VALUE,Integer.MIN_VALUE}
 
Junilu Lacar
Bartender
Pie
Posts: 7297
44
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Seems like you have a redundant check in there. How do you think you might shorten it?

I don't see a redundant check. Did you mean two conditionals that result in the same action? The two are not redundant though.
 
Junilu Lacar
Bartender
Pie
Posts: 7297
44
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Koch wrote:It was an assignment for school so now that I have it working...
Does it work correctly if you have a list like {5} or an empty list? For the 1-element list or the empty list, is it correct to return Integer.MIN_VALUE for the second biggest?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:If the list had fewer than 2 elements in it, would it still be valid to say there is a second biggest value? It looks like your solution would return Integer.MIN_VALUE even though it isn't an element of the list.


In the absence of specific instructions on how to handle that situation, I would say yes. As Paul Clapham pointed out in a similar thread a few months back, Integer.MIN_VALUE is the identity element for the max() function on ints.
 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now it does!
 
Junilu Lacar
Bartender
Pie
Posts: 7297
44
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides asking for specifics, I would probably choose to throw an IllegalArgumentException instead with a message that the argument should have at least two elements -- it's a step further back from violating the Principle of Least Surprise than returning a value that's not actually a part of the list.

@Campbell - JavaDocs are a poor substitute
 
Winston Gutkowski
Bartender
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:I don't see a redundant check. Did you mean two conditionals that result in the same action? The two are not redundant though.

Actually, they are. If you are already "rolling" biggest and second when you find a "new" biggest, then the check for
if (num == biggest)
is redundant, since it will always be satisfied by
if (num > second)
since either both will be equal (in which case you don't care about a 3rd number == biggest), or biggest will be > second; in which case a new number == biggest must be > second.

Winston
 
William Koch
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The assignment assumed that there were at least 2 Integer elements in the list.
 
Winston Gutkowski
Bartender
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski,
Your post was moved to a new topic.
Split from http://www.coderanch.com/forums/posts/list/40/593650
 
Winston Gutkowski
Bartender
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Koch wrote:The assignment assumed that there were at least 2 Integer elements in the list.

@William: I've split off the rest of this discussion into a separate thread, since we kind of rambled off into never-never land.

You can now carry on unencumbered.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic