Jump to content

How to get this generic class to compile

- - - - -

  • Please log in to reply
2 replies to this topic

#1
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
I am having trouble getting this generic class to compile, what am I missing or not doing correct?

javac AnyType.java
expecting class, enum, interface error- but generics are different?
for loop @ i



    public static <AnyType>

    AnyType findMax(AnyType [] arr, Comparator<? super AnyType> cmp)

    {

       int maxIndex = 0;

	

	   for(int i = 1; i < arr.size(); i++){

	      if(cmp.compareTo(arr[i], arr[maxIndex]) > 0){

	         maxIndex = i;

	      }

		  }

	   return arr[maxIndex];

   }


class CaseInsenitiveCompare implements Comparator<String>

{

   public int compare(String lhs, String rhs){

      return lhs.compareToIgnoreCase(rhs);

   }

}


class TestProgram

{

   public static void main(String [] args){

      String [] arr = {"ZEBRA", "alligator", "crocodile"};

	  System.out.println(findMax(arr, new CaseInsensitiveCompare()));

	}

}


Here is the interface

package java.util;

public interface Comparator<AnyType>

{

   int compare(AnyType lhs, AnyType rhs);

}



Thanks

#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
your first part of the code is written outside of the class. You can't write anything outside the class.

#3
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Thanks, didnt mean to post the code, figured out the problem, forgot an import statement (java.util.Comparator).
(This was an example from my cs book that I had to modify)

Here is the finished product after the minor modification with a question below it

import java.util.Comparator;


/**

 * Program: Test<AnyType>

 *

 * Description:

 *      This was initially the example from the book. The class has been modified

 *      to accept Characters to find the max char type. The comments will

 *      show the changes

 * Formula used:

 *      none

 * @author Mark Allen Weis edited by Mike 

 */

// added a class

public class BookMinorMod<AnyType> {

    

    public static <AnyType> AnyType findMax(AnyType[] arr,

            Comparator<? super AnyType> cmp) {

        int maxIndex = 0;


        for (int i = 1; i < arr.length; i++) {

            if (cmp.compare(arr[i], arr[maxIndex]) > 0) {

                maxIndex = i;

            }

        }

        return arr[maxIndex];

    }


    // changed Comparator from String to Character

    static class CaseInsensitiveCompare implements Comparator<Character> {


        public int compare(Character lhs, Character rhs) {

            // made the characters lower case to evaluate correctly (doesn't affect output result; if UpperCase 

            // result will be uppercase and vice versa)

            lhs = Character.toLowerCase(lhs);

            rhs = Character.toLowerCase(rhs);

            return (lhs).compareTo(rhs);

        }

    }


    static class TestProgram {


        public static void main(String[] args) {

            // just put char fields here

            Character[] arr = {'Z', 'a', 'c', 'D'};

            System.out.println(findMax(arr, new CaseInsensitiveCompare()));

        }

    }

}// end of BookMinorMod


Is there another way compare character data ignoring case besides using toUpperCase() or toLowerCase()?
Thanks again.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users