Computer Science 15-100, Fall 2008
Class Notes: Interfaces
Interfaces
import java.util.*;
class Demo {
public static void main(String[] args) {
// Declare, allocate, and load array with (somewhat) random pairs
Random random = new Random();
Pair[] pairs = new Pair[10];
for (int i=0; i<pairs.length; i++)
pairs[i] = new Pair(i%3, random.nextInt(10));
// And print the unsorted values
System.out.println(Arrays.toString(pairs));
// Now sort them
Arrays.sort(pairs);
// And print the naturally sorted values
System.out.println("Natural ordering (using Comparable):");
System.out.println(Arrays.toString(pairs));
// Now re-sort them using the Comparator
PairSorter pairSorter = new PairSorter();
Arrays.sort(pairs, pairSorter); // sort with the given Comparator!
// And print the unnaturally sorted values
System.out.println("Unnatural ordering (using Comparator):");
System.out.println(Arrays.toString(pairs));
}
}
class PairSorter implements Comparator<Pair> {
public int compare(Pair pair1, Pair pair2) {
// We'll sort by the SUM of the pair's x + y
int sum1 = pair1.getX() + pair1.getY();
int sum2 = pair2.getX() + pair2.getY();
return (sum1 - sum2);
}
}
class Pair implements Comparable {
private int x, y;
public Pair(int x, int y) { this.x = x; this.y = y; }
public String toString() {
// abbreviated for this example
return "(" + x + "," + y + ")";
}
public int getX() { return x; }
public int getY() { return y; }
// Must specify how to compare two Pair instances
public int compareTo(Object object) {
Pair that = (Pair) object;
// Let's sort by x first, and in a tie, then by y
if (this.x != that.x)
return (this.x - that.x);
else
return (this.y - that.y);
}
}
import java.awt.*;
import javax.swing.*;
class Demo {
public static void main(String[] args) {
JFrame frame = new JFrame("ActionListener Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click to beep!");
frame.getContentPane().add(button);
frame.setSize(300,200);
frame.setVisible(true);
}
}
Attempt #2: A button that beeps:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Demo {
public static void main(String[] args) {
JFrame frame = new JFrame("ActionListener Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click to beep!");
frame.getContentPane().add(button);
frame.setSize(300,200);
frame.setVisible(true);
// Now make the button work
ActionListener buttonListener = new ButtonListener();
button.addActionListener(buttonListener);
}
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Toolkit.getDefaultToolkit().beep();
}
}
Attempt #3: A button that beeps and counts the beeps!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Demo {
public static void main(String[] args) {
JFrame frame = new JFrame("ActionListener Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click to beep!");
frame.getContentPane().add(button);
frame.setSize(300,200);
frame.setVisible(true);
// Now make the button work
ActionListener buttonListener = new ButtonListener(button);
button.addActionListener(buttonListener);
}
}
class ButtonListener implements ActionListener {
private JButton button;
private int beepCount;
public ButtonListener(JButton button) {
this.button = button;
this.beepCount = 0;
}
public void actionPerformed(ActionEvent e) {
Toolkit.getDefaultToolkit().beep();
beepCount++;
button.setText(beepCount + " beeps! Click to beep again!");
}
}
class MyCode {
public static void main(String[] args) {
String s = "Go Pens!";
for (char c : s) // will not compile (drats!)
System.out.println(c);
}
}
How we (sort of) get it:
import java.util.*;
class MyCode {
public static void main(String[] args) {
IterableString s = new IterableString("Go Pens!");
for (char c : s)
System.out.println(c);
}
}
class IterableString implements Iterable<Character> {
private String stringValue;
public IterableString(String stringValue) {
this.stringValue = stringValue;
}
public String getStringValue() {
return stringValue;
}
public Iterator<Character> iterator() {
return new StringIterator(this);
}
}
class StringIterator implements Iterator<Character> {
private IterableString iterableString;
private int nextIndex;
public StringIterator(IterableString iterableString) {
this.iterableString = iterableString;
this.nextIndex = 0;
}
public boolean hasNext() {
String stringValue = iterableString.getStringValue();
return ((stringValue != null) &&
(nextIndex < stringValue.length()));
}
public Character next() {
String stringValue = iterableString.getStringValue();
return stringValue.charAt(nextIndex++);
}
public void remove() {
// The "right" way to not support an optional method
throw new UnsupportedOperationException();
}
}
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem