teaching machines

CS 245 Lab 2 – Interfaces and Interfaces

January 30, 2014 by . Filed under cs245, labs, spring 2014.

First, if you have checkpoints left over from last lab, get them inspected during the first 15 minutes of this lab.

Work in pairs. Where possible, work with someone that you did not work with last week. The exchange of new ideas and perspectives is not an opportunity you want to rob yourself of.

Synopsis

In this lab, you will continue to explore the use of interfaces so that you can write code that is highly reusable. Code written in terms of a supertype can be applied to many subtypes.

Checkpoint 1

Person A types.

Check out the documentation for java.io.FileFilter or java.io.FilenameFilter. See how these types are used by the list* methods of java.io.File.

Also checkout the documentation for Arrays.toString and File.length. These may come in handy.

Write a method listLarge that accepts a String path to a directory as a parameter. It prints to the console all files in the directory that are larger than some threshold number of bytes.

Write a method named listImages that accepts a String path to a directory as a parameter. It prints to the console all files in the directory that are JPEGs, PNGs, BMPs, or GIFs.

Optional: Kudos and high-fives if you also print out matching files in all subdirectories. It shouldn’t take more than a couple extra lines of code.

Checkpoint 2

Person B types.

Pop up a JFrame that shows a TicTacToe board, a 3×3 grid of JButtons. The JFrame‘s default LayoutManager—the 5-cell BorderLayout—is not going to work here. Instead, use a GridLayout.

When a button is clicked, set the button’s text to the current player’s symbol. Turns alternate between X and O.

Optional: check for win/draw states.