Download presentation

Presentation is loading. Please wait.

Published byDianna Brecher Modified over 2 years ago

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

Similar presentations

OK

Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on game theory applications Ppt on high voltage engineering holland Fab ms ppt online Ppt on mobile device management Lungs anatomy and physiology ppt on cells Ppt on dynamic resource allocation in cloud computing Ppt on crash fire tender picture Ppt on micro operations of an airport Ppt on synthesis and degradation of purines and pyrimidines in dna Ppt on instrument landing system manufacturer