public interface Customer { void eat(); boolean pay(); }
public class Cat implements Customer { public void eat() { ... } public boolean pay() { ... } }
public void serve(Customer c) // Can be a Cat or another implementing class { c.eat(); if (!c.pay()) doDishes(c); }
Which objects can be passed to the pay
method in the preceding slide?
Click all that apply
Customer
Cat
Person
Object
Which service does this stand mixer provide to any attachment that conforms to the “interface” for plugging in attachments?
Measurable
Interfacepublic interface Measurable { double getMeasure(); }
public static double average(Measurable[] objects) // Can be a BankAccount, Country, etc. { double sum = 0; for (Measurable obj : objects) sum = sum + obj.getMeasure(); if (objects.length > 0) return sum / objects.length; else return 0; }
In Eclipse, make a project out of the ch10/section_1
directory of the Book Code. Add the class below, and add the print statement to the main
method of the tester.
public class Word implements Measurable { private String text; public Word(String text) { this.text = text; } public double getMeasure() { return text.length(); } } ... System.out.println(Data.average(new Word[] { new Word("Hello"), new Word("World!") }));
How should the tester be completed?
System.out.println("Expected: 5");
System.out.println("Expected: 5.5");
System.out.println("Expected: 6");
System.out.println("Expected: Hello, World!");
Measurable
Measurable
Measurable obj = new BankAccount();
double m = obj.getMeasure(); // method depends on class of obj
public static Measurable larger(Measurable obj1, Measurable obj2) { if (obj1.getMeasure() > obj2.getMeasure()) return obj1; else return obj2; }
Measurable
Country uruguay = new Country("Uruguay", 176220); Country thailand = new Country("Thailand", 513120); Country max = larger(uruguay, thailand); // ERROR
Measurable
isn't so useful:
Measurable max = larger(uruguay, thailand); System.out.println(max.getName()); // ERROR
Country max = (Country) larger(uruguay, thailand);
public interface Sequence { int next(); }
public void process(Sequence seq, int valuesToProcess) { for (int i = 1; i <= valuesToProcess; i++) { int value = seq.next(); int lastDigit = value % 10; counters[lastDigit]++; } }
public class SquareSequence implements Sequence { private int n; public int next() { n++; return n * n; } }
public class RandomSequence implements Sequence { private Random generator = new Random(); public int next() { return generator.nextInt(); } }
Make an Eclipse project from the ch10/worked_example_1
directory in the Book Code
What are the last digits of the sequence of the cubes? (13, 23, 33, etc.?)
Implement the sequence class and modify the SequenceDemo
program.
What result do you get?
public class Mystery implements Sequence { private Sequence sequence; public Mystery(Sequence sequence) { this.sequence = sequence; } public int next() { return sequence.next() % 10; } }
Which of the following is true?
Mystery
object and calls next
on it, then an infinite recursion occurs, i.e. the next
method keeps calling itselfprocess
method can be rewritten as
public void process(Sequence seq, int valuesToProcess) { Sequence digits = new Mystery(seq); for (int i = 1; i <= valuesToProcess; i++) { counters[digits.next()]++; } }
Sequence seq = new Mystery(new SquareSequence()); for (int i = 1; i <= 5; i++) System.out.print(seq.next());
prints 1491625