Results 1 to 7 of 7

Thread: interface help

  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default interface help

    We just learned about using interfaces, and I am trying to use a very simplistic model of it. Here is my code:

    TestRationalNumber:
    Java Code:
    RationalNumber javaInterfaceExample = new RationalNumber();
    		javaInterfaceExample.sayHello();
    RationalNumber:
    Java Code:
    public class RationalNumber implements Comparable {public void sayHello() {
    		System.out.println("Hellos, Visitor!");
    	}}
    Comparable:
    Java Code:
    interface Comparable {
    	
    	public void sayHello();
    	
    }
    I guess I am having issues calling on the sayHello() method from the RationalNumber class. Eclipse is telling me that i need to put in more ints. I am calling on the constructor, right? So somehow I need to modify the code to call on the right method...

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: interface help

    Don't call your interface Comparable as there already is a core interface called that which can lead to some major problems. Instead call it something else, perhaps Hello or something like that.

    Next, when creating code that others will read, don't be afraid to use white space to help make your code more readable. For instance your RationalNumber class is very difficult to read due to your trying to cram statements in as few lines as possible. Don't over-use white space either, but rather check the examples you see and try to emulate them.

    Luck!

  3. #3
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: interface help

    the only way I see it working, is if i take my constructor out of the RationalNumber class...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: interface help

    Quote Originally Posted by droidus View Post
    the only way I see it working, is if i take my constructor out of the RationalNumber class...
    Try re-writing it and renaming the interface. If it still doesn't work, please post the new code, but please post readable code.

  5. #5
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: interface help

    Java Code:
    public class RationalNumber implements Print {
    	private int numerator, denominator;
    
    	public RationalNumber(int numer, int denom) {
    		if (denom == 0)
    			denom = 1;
    		if (denom < 0) {
    			numer = numer * -1;
    			denom = denom * -1;
    		}
    		numerator = numer;
    		denominator = denom;
    		reduce();
    	}
    public void sayHello() {
    		System.out.println("Hellos, Visitor!");
    	}
    }
    Java Code:
    public class TestRationalNumber {
              public static void main(String[] args) {
              RationalNumber javaInterfaceExample = new RationalNumber();
    	javaInterfaceExample.sayHello();
            }
    }


    Java Code:
    interface Print {
    	
    	public void sayHello();
    	
    }
    It seems to work fine without the constructor...

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: interface help

    Quote Originally Posted by droidus View Post
    It seems to work fine without the constructor...
    Of course. If you give your class that constructor, you eliminate the default parameterless constructor. So if you try to use a parameterless constructor, you'll of course get an error. This has absolutely nothing to do with interfaces.

  7. #7
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: interface help

    ok, so what do I have to do? I am not quite sure what you are saying.

    so i would have to do something like this for it to pick it up, since it has no parameters, right?

    public RationalNumber() {
    }
    and then later on...
    public void sayHello() {
    System.out.println("Hellos, Visitor!");
    }
    Last edited by droidus; 10-08-2011 at 03:19 AM.

Similar Threads

  1. How do I use an Interface for this?
    By aaronfsimons in forum New To Java
    Replies: 14
    Last Post: 06-22-2009, 02:19 PM
  2. Interface?
    By makpandian in forum New To Java
    Replies: 5
    Last Post: 03-26-2009, 10:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •