I need help coding the following:
In your NotebookTest driver program, use a For loop to move a note up for a specified number of moves (which you will store in a variable). Set the value of this variable to 2 and test your For loop. Use the method you created in step D.
Make a corresponding For loop to move a note down a specified number of times.
Code for Notebook Class:
public class Notebook {
private ArrayList < String > notes;
public Notebook() {
this.notes = new ArrayList < String > ();
}
public ArrayList < String > getNotes() {
return notes;
}
//adds a note to list and won't allow duplicate
public void addNote(String note) {
if (!notes.contains(note)) {
notes.add(note);
}
}
//this was the original addNote Method
/*public void addNote(String note) {
this.notes.add(note);
}*/
//removes a note
public void deleteNote(String note) {
this.notes.remove(note);
}
//returns the size of list
public int numberOfNotes() {
return notes.size();
}
// gets the position of a note based on its value
public int getNoteNumber(String note) {
return notes.indexOf(note);
}
//returns text in a position
public String getNote(int note) {
return notes.get(note);
}
//updates text of a given position
public void setNote(int note, String nt) {
notes.set(note, nt);
}
//moves note up one in list
public void moveNoteUp(String note) {
Collections.swap(notes, notes.indexOf(note), notes.indexOf(note) - 1);
}
//moves note down one in list
public void moveNoteDown(String note) {
Collections.swap(notes, notes.indexOf(note), notes.indexOf(note) + 1);
}
}
Code for NotebookTest
public class NotebookTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Notebook ntb = new Notebook();
System.out.println("Notebook has " + ntb.numberOfNotes());
ntb.addNote("joe");
ntb.addNote("ryan");
ntb.addNote("bill");
ntb.addNote("jill");
ntb.addNote("beth");
System.out.println("Notebook has " + ntb.numberOfNotes());
ntb.deleteNote("ryan");
System.out.println("Notebook has " + ntb.numberOfNotes());
System.out.println("Bill is at position " + ntb.getNoteNumber("bill") + " in the Arraylist");
System.out.println(ntb.getNote(0) + " is in position 0 of the Arraylist");
ntb.setNote(0, "phil");
System.out.println("I changed the text of position 0 to " + ntb.getNote(0));
ntb.addNote("bill");
System.out.println("Notebook has " + ntb.numberOfNotes());
ntb.addNote("jeff");
System.out.println("Notebook has " + ntb.numberOfNotes());
System.out.println(ntb.getNotes());
ntb.moveNoteUp("jeff");
System.out.println(ntb.getNotes());
ntb.moveNoteDown("jill");
System.out.println(ntb.getNotes());
}
}