Results 1 to 7 of 7
Thread: interface help
- 10-08-2011, 03:12 AM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 259
- Rep Power
- 11
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();
Java Code:public class RationalNumber implements Comparable {public void sayHello() { System.out.println("Hellos, Visitor!"); }}
Java Code:interface Comparable { public void sayHello(); }
-
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!
- 10-08-2011, 03:54 AM #3
Senior Member
- Join Date
- Feb 2011
- Posts
- 259
- Rep Power
- 11
Re: interface help
the only way I see it working, is if i take my constructor out of the RationalNumber class...
-
Re: interface help
- 10-08-2011, 04:07 AM #5
Senior Member
- Join Date
- Feb 2011
- Posts
- 259
- Rep Power
- 11
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(); }
-
Re: interface help
- 10-08-2011, 04:12 AM #7
Senior Member
- Join Date
- Feb 2011
- Posts
- 259
- Rep Power
- 11
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 04:19 AM.
Similar Threads
-
How do I use an Interface for this?
By aaronfsimons in forum New To JavaReplies: 14Last Post: 06-22-2009, 03:19 PM -
Interface?
By makpandian in forum New To JavaReplies: 5Last Post: 03-26-2009, 11:59 AM
Bookmarks