CS 245 Lecture 3 – Interfaces
Agenda
- bounding advance/retreat in ImageReel
- modeling the Three Little Pigs
- simplifying with interfaces
TODO
- Read chapter 6 in Core Java, stopping before Inner Classes.
- 1/4 sheet.
Three Little Pigs
- How would you model the wolf and houses in this story? Write down classes and method signatures. Recall: The nouns of the problem domain often become the classes. Verbs often become the methods. Direct and indirect objects often become the method parameters.
- You learn of a fourth brother who built a house made out of butter. What needs to change in your model?
- You learn of a fifth brother who built a house made out of goretex. What needs to change in your model?
- You learn of a sixth brother…
Analogy
- A conductor doesn’t know the details of every instrument in the orchestra. The conductor says louder, faster, more punctuated—letting the players figure out the best way to comply. By having a common but generalized language, the conductor can focus on high-level management and the players can focus on their instrument. When the fooboophone gets invented, the conductor doesn’t need to go back to conductor school—provided the new fooboophone players speak the agreed-upon language.
Code
ImageReel.java
package lecture0910;
import java.io.File;
import java.util.Arrays;
public class ImageReel {
private int currentIndex;
private File[] images;
public ImageReel(String pathToDirectory) {
File dir = new File(pathToDirectory);
images = dir.listFiles();
Arrays.sort(images);
currentIndex = 0;
}
public File getCurrent() {
return images[currentIndex];
}
public void advance() {
++currentIndex;
if (currentIndex > images.length - 1) {
currentIndex = 0;
}
}
public void retreat() {
--currentIndex;
if (currentIndex < 0) {
currentIndex = images.length - 1;
}
}
}
ImageReelTest.java
package lecture0910;
import java.io.File;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class ImageReelTest {
private ImageReel reel;
@Before
public void setup() {
reel = new ImageReel("/home/johnch/Desktop/album");
}
@Test
public void testCtor() {
File actual = reel.getCurrent();
Assert.assertEquals("reel not focused correctly", "a.jpg", actual.getName());
}
@Test
public void testAdvance() {
reel.advance();
File actual = reel.getCurrent();
Assert.assertEquals("advance fails", "b.jpg", actual.getName());
}
@Test
public void testRetreat() {
reel.advance();
reel.advance();
reel.advance();
reel.retreat();
File actual = reel.getCurrent();
Assert.assertEquals("retreat fails", "c.jpg", actual.getName());
}
@Test
public void testRetreatEarly() {
reel.retreat();
File actual = reel.getCurrent();
Assert.assertEquals("retreat fails", "e.jpg", actual.getName());
}
@Test
public void testAdvanceAdvanceAdvanceAdvance() {
reel.advance();
reel.advance();
reel.advance();
reel.advance();
reel.advance();
File actual = reel.getCurrent();
Assert.assertEquals("advance fails", "a.jpg", actual.getName());
}
}
Eaushow.java
package lecture0910;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Eaushow {
public static void main(String[] args) {
JFrame frame = new JFrame("Eaushow");
ImageReel reel = new ImageReel("/home/johnch/Desktop/album");
JLabel imageLabel = new JLabel("INSERT PIC HERE");
imageLabel.setHorizontalAlignment(JLabel.CENTER);
frame.add(imageLabel, BorderLayout.CENTER);
JButton previousButton = new JButton("<");
frame.add(previousButton, BorderLayout.WEST);
Retreater retreater = new Retreater(reel, imageLabel);
previousButton.addActionListener(retreater);
JButton nextButton = new JButton(">");
frame.add(nextButton, BorderLayout.EAST);
Advancer advancer = new Advancer(reel, imageLabel);
nextButton.addActionListener(advancer);
frame.setSize(700, 700);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Retreater.java
package lecture0910;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class Retreater implements ActionListener {
private ImageReel reel;
private JLabel label;
public Retreater(ImageReel reel,
JLabel label) {
this.reel = reel;
this.label = label;
}
public void actionPerformed(ActionEvent e) {
reel.retreat();
label.setText(reel.getCurrent().getName());
try {
BufferedImage image = ImageIO.read(reel.getCurrent());
label.setIcon(new ImageIcon(image));
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
Advancer.java
package lecture0910;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class Advancer implements ActionListener {
private ImageReel reel;
private JLabel label;
public Advancer(ImageReel reel,
JLabel label) {
this.reel = reel;
this.label = label;
}
public void actionPerformed(ActionEvent e) {
reel.advance();
label.setText(reel.getCurrent().getName());
try {
BufferedImage image = ImageIO.read(reel.getCurrent());
label.setIcon(new ImageIcon(image));
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
Haiku
They make bomb droppers
And garage door openers
It saves overhead
And garage door openers
It saves overhead