Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: problem with interface

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default problem with interface

    i seem to have a syntax error or am not calling my interface correctly. can someone help please?


    file one ...

    ================================================== =====
    import java.util.Scanner;
     
    public class AnagramFinderApp {
     
    	private static final AnagramFinder True = null;
     
    	/**
    	* Determines if two strings are anagrams. Whitespace is ignored, but all other characters
    	*/
     
         public static void main(String[] args) {
     
        //	 boolean match = false;
     
             Scanner sc = new Scanner(System.in);
     
             System.out.print("Enter first string:");
             String s1 = sc.nextLine();  
     
             System.out.print("Enter second string:");
             String s2 = sc.nextLine(); 
     
      //       AnagramFinder anagramFinder = new AnagramFinder();
             AnagramFinder match = areAnagrams(s1, s2);
     
             if (match == True)        		 
            	 System.out.println("The strings are the same");
             else
        		 System.out.println("The strings are different");
         }
     
    }

    file two ...

    ================================================== =====
    	 public interface AnagramFinder{
     
    	 /**
    	 * Determines if two strings are anagrams. Whitespace is ignored, but all other characters
    	 * (including punctuation) count.
    	 * @param s1 first string to compare
    	 * @param s2 second string to compare
    	 * @return true if s1 and s2 are anagrams, false otherwise
    	 */
    		boolean areAnagrams(String s1, String s2);
     
    	 }

    file tree ...

    ================================================== =====
    import java.util.Arrays;
     
    class areAnagramsx implements AnagramFinder{
     
    	public boolean areAnagrams(String s1, String s2){
     
    	 String ss1 = (s1.replaceAll(" ", "")); // remove blank spaces
    	 String ss2 = (s2.replaceAll(" ", ""));
     
    	 System.out.println(ss1); // print strings
    	 System.out.println(ss2);
     
         char[] arr1 = s1.toCharArray(); // convert string to a character array
         char[] arr2 = s2.toCharArray();
     
         Arrays.sort(arr1); // sort arrays
         Arrays.sort(arr2);
     
         boolean a = Arrays.equals(arr1, arr2);  
     
    	 return a;
       }
     
    }
    Last edited by helloworld922; May 22nd, 2011 at 08:11 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: problem with interface

    Is there an error message? Please post it.
    Otherwise please explain what is/is not happening

    Please put your code in code tags to preserver the formatting. See the # icon above the text input box.

  3. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: problem with interface

    Looks like you've got very confused...

    To use the interface, you need to create an instance of a class that implements it (e.g. areAnagramsx) and then call a method on that object, for example:
    AnagramFinder finder = new areAnagramsx();  // create object that implements the interface
    boolean areAnagrams = finder.areAnagrams(s1, s2); // call method on object
    The boolean result can be used directly, there's no need to compare it with a 'true' value because it already is a true or false value, e.g.:
    boolean areAnagrams = finder.areAnagrams(s1, s2);
    if (areAnagrams) {
       // do stuff
    }
    Also, please use the Java conventions and start all class names with uppercase letters. Use meaningful names - if you can't think of a meaningful name for the implementation class of AnagramFinder, just call it AnagramFinderImpl.

  4. The Following User Says Thank You to dlorde For This Useful Post:

    buddy101 (May 23rd, 2011)

  5. #4
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem with interface

    dlorde,
    i wrote a full page reply, but my computer ate it. Would you be able to break down the setup for the interface?


    // create object that implements the interface
    AnagramFinder finder = new areAnagramsx();

    AnagramFinder = create a new “type” AnagramFinder
    finder = new object being created
    New = new key word
    areAnagramsx() = class that implements the interface


    // call method on object
    boolean areAnagrams = finder.areAnagrams(s1, s2

    Boolean = keyword for the return value of the interface
    areAnagrams = return value from the interface
    finder.areAnagrams(s1, s2) = call the areAnagrams method with the finder
    object

    Is this about right?

    Thanks, Buddy

  6. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: problem with interface

    That's pretty much it, although to be more precise:

    AnagramFinder finder = declare a reference variable called 'finder', of type AnagramFinder
    finder = new areAnagramsx(); = create a new object of type areAnagramsx and assign it to finder (set finder to point to it)

    In Java all variables for objects are references (generally known as 'pointers' in other languages). They must be declared to have a particular type, e.g. AnagramFinder, or areAnagramsx, or Object. This means they can reference ('refer to' or 'point to') any object of that type - including subclasses of that type - so an AnagramFinder variable can point to an areAnagramsx object (and an Object variable can point to objects of any class, because all classes are subclasses of Object). Bear in mind that the variable type determines what you can do with it; for example, an AnagramFinder variable can point to an areAnagramsx object, but you can only use it to access the AnagramFinder methods of the object. Similarly, an Object variable can point to an areAnagramsx object, but can only be used to access the Object methods it has.

    If you don't assign an object to them, reference variables are null because they don't point to an object, and the only thing you can do with them when they are null is to check if they are null or point them to an object of a compatible type - anything else will cause a NullPointerException (one of the few examples in Java that explicitly describes reference variables as pointers!).

    This means that you can only ever access objects (call methods on them, etc) indirectly, via these reference variables, and more than one variable can reference the same object. When you pass a variable to a method, the method receives a local copy of that variable that points to the same object.

    boolean areAnagrams = finder.areAnagrams(s1, s2)

    boolean = keyword declaring the type of variable areAnagrams (to match the type of the return value of the interface method).

    Note that, for various reasons, Java has a few primitive types as well as object types. Primitive types aren't based on classes and methods, they're just 'built in' to the language. Their keywords start with a lowercase letter to make this clear: int, long, boolean, float, etc. However, because objects are often required in code, each primitive type has been given a corresponding wrapper class: Integer, Long, Boolean, Float, etc. Recent versions of Java provide an automatic mechanism called 'autoboxing', that converts between a primitive type and its wrapper class whenever necessary, so you can use them interchangeably without any hassle.
    Last edited by dlorde; May 25th, 2011 at 07:53 AM.

Similar Threads

  1. Interface and Extends problem
    By DannyGT in forum Object Oriented Programming
    Replies: 1
    Last Post: September 1st, 2011, 01:01 PM
  2. Interface
    By kaasi in forum Member Introductions
    Replies: 4
    Last Post: April 21st, 2011, 10:45 AM
  3. interface
    By nasi in forum Object Oriented Programming
    Replies: 5
    Last Post: September 2nd, 2010, 10:36 PM
  4. Interface problem please help!!
    By joff1403 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2010, 11:09 AM
  5. Problem while implementing a basic user interface menu
    By Rastabot in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 3rd, 2009, 04:38 PM