/** A solution to the TresAmigos problem. * @see http://www.radford.edu/itec120/2009spring-ibarland/Homeworks/hw05.html * @author Ian Barland * @version 2008.Oct.16-b * * This version creates two helper functions to help make our main task easier: * 'minString' and 'maxString'. * These make it easy to find the first name (minimum out of three), * and the last name (maximum of three). * Then, in a totally non-intuitive way, it also determinines the * middle name using *only* min and max (!). * * While the code is short, it is *not* obviously-correct; * therefore it is *not* as good as (say) the previous v2 solution. * The only reason I have faith in the code is because the test cases * were extremely thorough, and it passes those test cases. */ public class TresAmigos_v3 { private String first,second,third; public TresAmigos_v3( String ami1, String ami2, String ami3 ) { this.first = minString3(ami1,ami2,ami3 ); this.third = maxString3(ami1,ami2,ami3 ); this.second = maxString3( maxString(minString(ami1,ami2), minString(ami1,ami3)), maxString(minString(ami2,ami1), minString(ami2,ami3)), maxString(minString(ami3,ami1), minString(ami3,ami2)) ); } public String getFirst() { return this.first; } public String getSecond() { return this.second; } public String getThird() { return this.third; } private String minString( String s1, String s2 ) { if (s1.compareTo(s2) < 0) { return s1; } else { return s2; } } private String maxString( String s1, String s2 ) { if (s1.compareTo(s2) > 0) { return s1; } else { return s2; } } private String maxString3( String s1, String s2, String s3 ) { return maxString(s1, maxString(s2,s3) ); } private String minString3( String s1, String s2, String s3 ) { return minString(s1, minString(s2,s3) ); } }