aspose file tools*
The moose likes Beginning Java and the fly likes Also Need Help Verifying this method works Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Also Need Help Verifying this method works" Watch "Also Need Help Verifying this method works" New topic
Author

Also Need Help Verifying this method works

William Koch
Ranch Hand

Joined: Sep 26, 2012
Posts: 76
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

Joined: May 22, 2012
Posts: 867
    
    5
> Can anyone tell me why?

Because it is broken.

A hint: what does yield for the array (2,1)?
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
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

Joined: Oct 13, 2005
Posts: 39388
    
  28
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

Joined: Sep 26, 2012
Posts: 76
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

Joined: Feb 26, 2001
Posts: 4680
    
    7

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
William Koch
Ranch Hand

Joined: Sep 26, 2012
Posts: 76
@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

Joined: Feb 26, 2001
Posts: 4680
    
    7

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

Joined: Feb 26, 2001
Posts: 4680
    
    7

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 39388
    
  28
Now it does!
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4680
    
    7

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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Joined: Sep 26, 2012
Posts: 76
The assignment assumed that there were at least 2 Integer elements in the list.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Winston Gutkowski,
Your post was moved to a new topic.
Split from http://www.coderanch.com/forums/posts/list/40/593650
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Also Need Help Verifying this method works