Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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:

1 Interfaces CSC 171 FALL 2004 LECTURE 14

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

17 Reading Read Chapter 9 of Horstmann

18 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?

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

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

21 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

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

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

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

25 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


Ads by Google