import java.util.ArrayList; /* On the exam you will have a copy of the following program, * with examples of ArrayList and array declarations, if-statements, * and while, for, and for-each loops. */ /* A class to maintain an arbitrarily long list of notes. * @author David J. Barnes and Michael Kolling. */ public class Notebook { // Storage for an arbitrary number of notes. private ArrayList notes; private String[] notes2; // what an array declaration would look like /* Perform any initialization that is required for the notebook. */ public Notebook() { notes = new ArrayList(); notes2 = new String[20]; // fixed size array; not used below } /* Store a new note into the notebook. */ public void storeNote(String note) { notes.add(note); } /* @return The number of notes currently in the notebook. */ public int numberOfNotes() { return notes.size(); } /* Remove a note from the notebook if it exists, given its number. */ public void removeNote(int noteNumber) { if(noteNumber < 0) { // This is not a valid note number, so do nothing. } else if(noteNumber < numberOfNotes()) { // This is a valid note number. notes.remove(noteNumber); } else { // This is not a valid note number, so do nothing. } } /* List all notes in the notebook; while loop version */ public void listNotes() { int index = 0; while(index < notes.size()) { System.out.println(notes.get(index)); index++; // or index += 1 } } /* List all notes in the notebook; for loop version */ public void listNotes2() { int index ; for(index = 0; index < notes.size(); index++) { System.out.println(notes.get(index)); } } /* List all notes in the notebook; for-each loop version */ public void listNotes3() { for ( String note : notes ) { System.out.println(note); } } }