













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














A List of Candidate Objects
Self-Check
Self-Check
Self-Check
Make the assertions fail
Add More Functionality
public void testRecordOnePlayAndCanPlay() { // Times played begins at 0 when constructed Song song1 = new Song("We Can Work it Out", 135); assertTrue(song1.canPlayToday()); song1.recordOnePlay(); song1.recordOnePlay(); song1.recordOnePlay(); song1.recordOnePlay(); assertTrue(song1.canPlayToday()); song1.recordOnePlay(); assertFalse(song1.canPlayToday()); }
Refactoring
Finishing Song
Self-Check
import java.applet.Applet; import java.applet.AudioClip; import java.net.MalformedURLException; import java.net.URL; public class SongPlayer { private String baseFolder; private AudioClip audioClip; public SongPlayer(String baseFolder) { this.baseFolder = baseFolder; } public void playSong(Song theSong) { URL url = null; try { url = new URL(baseFolder + theSong.getAudioFileName()); audioClip = Applet.newAudioClip(url); } catch (MalformedURLException e) { System.out.println("bad url " + url); } audioClip.play(); }
// Play one Song stored in this folder's midifiles folder import javax.swing.JOptionPane; public class PlayOneMidiSong { // All songs must be in a folder named midifiles and that // folder must be in the same folder as the Java class files // (or you won't hear the song play) public static final String baseFolder = "file:" + System.getProperty("user.dir") + "/midifiles/"; public static void main(String[] args) { SongPlayer theSongPlayer = new SongPlayer(baseFolder); // Posssible formats: au, wav, aif, mid. // With extra installation, we could play mp3 files! Song theSong = new Song("Beatles", "Help", 132, "Help.mid"); theSongPlayer.playSong(theSong); JOptionPane.showMessageDialog(null, "Stop playing: " + theSong.toString()); // The SongPlayer starts a new thread that will not be shut down // without an explicit call to terminate this program. Without this // static method call, you will have a program running every time // you run this program (songs playing over the others). System.exit(0); } }
SongCollection
public void testNeighborsWrapping() { GameOfLife society = new GameOfLife(10, 16); society.growCellAt(3, 0); society.growCellAt(3, 15); assertEquals(1, society.neighborCount(3, 15)); assertEquals(1, society.neighborCount(3, 0)); assertEquals(2, society.neighborCount(2, 0)); assertEquals(2, society.neighborCount(4, 0)); assertEquals(2, society.neighborCount(2, 15)); }
// Write the constructor so it takes two integer arguments to represent // the number of rows and columns in the game of life. The constructor // creates a society with no cells but space to store rows*cols cells. public GameOfLife (int rows, int cols) // Return the number of usable rows indexed from 0..numberOfRows()- 1 public int numberOfRows () // Return the number of columns indexed from 0..numberOfColumns()- 1 public int numberOfColumns () // Place a new cell in the society. // Precondition: row and col are in range. public void growCellAt (int row, int col) // Return one big string of cells to represent the current state of // the society of cells (see output below where '.' represents an // empty space and 'O' is a live cell. There is no need to // test toString. Simply use it to visually inspect if needed. public String toString ()
GameOfLife society = new GameOfLife(4, 14); society.growCellAt(1, 2); society.growCellAt(2, 2); society.growCellAt(3, 2); System.out.println(society.toString());
// Return true if there is a cell at the given row and column. // Return false if there is none at the specified location. public boolean cellAt (int row, int col) // Count the neighbors around the given location. Use wraparound. // A cell in row 0 has neighbors in the last row if a cell is in the // same column, or the column to the left or right. In this example, // .....O..O // O........ // O.......O // O.......O // ....O.O.. // cell 0,5 has two neighbors in the last row, cell 2,8 has four // neighbors, cell 2,0 has four neighbors, cell 1,0 has three // neighbors. The cell at 4,8 has three neighbors, and the cell // at 3,8 has 3 neighbors. public int neighborCount (int row, int col) // Update the state to represent the next society. public void update ()