Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int...

25
Interfaces Interfaces CSC 171 FALL 2004 LECTURE 14

Transcript of Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int...

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

InterfacesInterfaces

CSC 171 FALL 2004

LECTURE 14

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

Project 1 reviewProject 1 review

public class Rational {

private int numerator, denominator;

public Rational(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; reduce();}

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

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);}

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

public int getNumerator() {

return numerator ;

}

public int getDenominator() {

return denominator ;

}

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

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);

}

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

Rational r1 = new Rational(1,2);

Rational r2 = new Rational(3,4);

Rational r3 = Rational.add(r1,r2);

Rational r4 = Rational.add(r2,r1);

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

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);}

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

Rational r1 = new Rational(1,2);

Rational r2 = new Rational(3,4);

Rational r3 = r1.add(r2);

Rational r4 = r2.add(r1);

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

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);

}

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

public Rational multiply(Rational r) {

int newNum = this.numerator * r.numerator ;

int newDenom = this.denominator * r.denominator;

return new Rational(newNum,newDenom);

}

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

public static boolean equals(Rational r1, Rational r2) {

return (r1.numerator * r2.denominator)

== (r2.numerator * r1.denominator);

}

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

if (Rational.equals(r1,r2)) {

//….

}

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

public boolean equals(Rational r) {

return (this.numerator * r.denominator)

== (r.numerator * this.denominator);

}

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

if (r1.equals(r2)) {

//….

}

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

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

public String toString() { return "(" + numerator + "/" + denominator +")";}

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

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

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

System.out.println(r1);

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

ReadingReading

Read Chapter 9 of Horstmann

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

Reusable SolutionsReusable 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?

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

MethodsMethods

public static void main(String [] args) {

// fill in your code

}

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

MethodsMethods

// the head is the ‘what’

public static void main(String [] args) {

// fill in your code

}

// the body is the ‘how’

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

InterfacesInterfaces

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

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

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

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

public interface interfaceName {

// method signatures

}

public class className implements

interfaceName, interfaceName, … {

}

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

Common InterfaceCommon Interface

public interface Comparable {

public int compareTo (Object o) {

// return “0” if equal

// a negative value if less

// positive value if more

}

}

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

ExampleExample

Comparable for rationals