Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.

Presentation on theme: "Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int."— Presentation transcript:

Interfaces CSC 171 FALL 2004 LECTURE 14

Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; reduce(); }

private void reduce() { int divisor = gcd((numerator < 0)?-1*numerator:numerator, (denominator < 0)?-1*denominator:denominator); if (divisor != 0) { numerator /= divisor; denominator /= divisor; } private int gcd(int a, int b) { if (b == 0) return a; else return gcd(b,a%b); }

public int getNumerator() { return numerator ; } public int getDenominator() { return denominator ; }

public static Rational add(Rational r1, Rational r2) { int newNum = r1.numerator * r2.denominator + r1.denominator * r2.numerator ; int newDenom = r1.denominator * r2.denominator; return new Rational(newNum,newDenom); }

Rational r1 = new Rational(1,2); Rational r2 = new Rational(3,4); Rational r3 = Rational.add(r1,r2); Rational r4 = Rational.add(r2,r1);

public Rational add(Rational r) { int newNum = this.numerator * r.denominator + r.denominator * this.numerator ; int newDenom = this.denominator * r.denominator; return new Rational(newNum,newDenom); }

Rational r1 = new Rational(1,2); Rational r2 = new Rational(3,4); Rational r3 = r1.add(r2); Rational r4 = r2.add(r1);

public static Rational multiply(Rational r1, Rational r2) { int newNum = r1.numerator * r2.numerator ; int newDenom = r1.denominator * r2.denominator; return new Rational(newNum,newDenom); }

public Rational multiply(Rational r) { int newNum = this.numerator * r.numerator ; int newDenom = this.denominator * r.denominator; return new Rational(newNum,newDenom); }

public static boolean equals(Rational r1, Rational r2) { return (r1.numerator * r2.denominator) == (r2.numerator * r1.denominator); }

if (Rational.equals(r1,r2)) { //…. }

public boolean equals(Rational r) { return (this.numerator * r.denominator) == (r.numerator * this.denominator); }

if (r1.equals(r2)) { //…. }

public static String toString(Rational r) { return "(" + r.numerator + "/" + r.denominator +")"; } public String toString() { return "(" + numerator + "/" + denominator +")"; }

System.out.println(Rational.toString(r1)); System.out.println(r1.toString()); System.out.println(r1);

Reusable Solutions Sometimes, we know what we want something to do, but we don’t know exactly how it will do it. Wouldn’t it be nice if we could specify “what”, and leave the “how” until later?

Methods public static void main(String [] args) { // fill in your code }

Methods // the head is the ‘what’ public static void main(String [] args) { // fill in your code } // the body is the ‘how’

Interfaces A java Interface declares a set of methods and their signatures. Since the body of the methods are not written, you can’t make objects out of interfaces. However, you can make new classes using interfaces, then make objects out of your new classes

To realize an interface, a class must supply all the methods that the interface requires

public interface interfaceName { // method signatures } public class className implements interfaceName, interfaceName, … { }

Common Interface public interface Comparable { public int compareTo (Object o) { // return “0” if equal // a negative value if less // positive value if more }

Example Comparable for rationals

Download ppt "Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int."

Similar presentations