as homework assignment, need restructure code in textbook. goal to, using findmax method , function object technique, find "findmax" of character , string array findmax return char last in alphabet , string, return string starts last letter in alphabet ignoring case sensitivity. question if have done correctly (i have doubts)? don't want errors or warnings. don't want copy , paste answers. want better understanding of how can this. here code:
import java.util.comparator; public class lab1q6 { public static <anytype extends comparable<? super anytype>> anytype findmax( anytype [ ] arr, comparator<? super anytype> cmp ){ int maxindex = 0; for(int i=1; < arr.length; i++) if( cmp.compare( arr[ ], arr[ maxindex ] ) > 0 ) maxindex = i; return arr[ maxindex ]; } public static class caseinsensitivecompare<anytype extends comparable<anytype>> implements comparator<anytype> { public int compare( string lhs, string rhs ) { return lhs.compareto( rhs ); } @override public int compare(anytype o1, anytype o2) { return o1.compareto( o2 ); } } @suppresswarnings({ "unchecked", "rawtypes" }) public static void main(string[] args) { string [] array = {"owl", "zebra", "gator", "ant", "reindeer"}; character [] arr = {'c', 'z', 'a', 'r', 's'}; system.out.println( findmax(arr, new caseinsensitivecompare())); system.out.println( findmax(array, new caseinsensitivecompare())); } }
Comments
Post a Comment