Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Notebook Lab ArrayLists Objectives: For this lab we\'ll get practice with collec

ID: 3810454 • Letter: N

Question

Notebook Lab

ArrayLists

Objectives:

For this lab we'll get practice with collections and loops. You'll extend the book's code so that it deals with high-priority notes as well as the current flavor, modify the way the list is displayed, and write some code that changes the position of notes within the ArrayList.

Directions:

Download the Notebook.java file blackboard into your working directory. Open the Notebook class in jGrasp and compile it.

Take a moment to familiarize yourself with the Java code in the NotebookLab class. The collection of notes is represented by an ArrayList of Strings (created in the constructor), and there are methods that add Strings to the list, remove Strings, and a loop that displays all of the Strings in the collection.

Create a new class called NotebookTester. In the main method, create an instance of Notebook and experiment with its methods to get a feel for how it works. See example below.

The storeNote method always adds new items at the end of the list. Users may prefer to add new items to the beginning of the list instead — especially if the notes are urgent. Add a new method called storeUrgentNote that works just like storeNote, but adds the new note to the front of the collection. It should prepend "Urgent: " to the front of the note string before adding it, to make clear that this is a high-priority item, as illustrated in the partial tester statements below.

Positions within an ArrayList are zero based. That is, the first item is at position zero, the next item is at position 1, etc. The removeNote method assumes users will pass in the appropriate zero-based number when specifying which note to remove, but users may not be familiar with this convention. Let's make it easier on the user and show item numbers when displaying the list. Change the code in the listNotes method so that it maintains a counter (an integer variable), and displays this number along with the notes as shown below. Note: You should not alter the basic loop. Instead, add extra statements to the existing loop to implement the numbers.

Write a listUrgentNotes method that only displays the urgent messages in the list. In other words, it only displays the notes that have "Urgent: " at the beginning. (Read up on startsWith in the String class for more information on how to test for this.)

7.   Write a method called demoteNote that takes a note number, just like removeNote does. Instead of removing the note from the list, demoteNote should move it to the end of the list. There's no ArrayList method for performing exactly this operation, but you can accomplish it by doing a remove and an add. To keep things simple, you may assume that the user passes in a valid, zero-based note number. Note that if you demote an urgent note, the results may look a little weird. Remove the "Urgent: " prefix from demoted notes, if it's present. You can use startsWith to determine if the prefix is present, and substring from the String class to extract everything but the prefix.

Write a swapNotes method that takes a note number like demoteNote does, but exchanges the specified note with its successor. In the example below, the note at position 1 is being swapped with the note "below" it:

You need to write a NotebookTester that tests each method in the Notebook class.

Brad Richards, 2009

Explanation / Answer

*******************

NoteBook.java

package WordNode;

import java.util.ArrayList;
import java.util.Collections;

/**
* A class to maintain an arbitrarily long list of notes.
* Notes are numbered for external reference by a human user.
* In this version, note numbers start at 0.
*
* @author David J. Barnes and Michael Kolling.
* @version 2006.03.30
*/
public class Notebook
{
   // Storage for an arbitrary number of notes.
   private ArrayList<String> notes;

   /**
   * Perform any initialization that is required for the
   * notebook.
   */
   public Notebook()
   {
       notes = new ArrayList<String>();
   }

   /**
   * Store a new note into the notebook.
   * @param note The note to be stored.
   */
   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.
   * @param noteNumber The number of the note to be removed.
   */

   public void demoteNote(int noteNumber){

       if(noteNumber < 0){

           //Do not do anything
       }else if(noteNumber < numberOfNotes()){

           if(notes.contains("Urgent"))
               notes.remove(noteNumber);
       }else {}


   }

   public void listUrgentNotes(){

       if(notes.contains("Urgent:")){
           System.out.println("Display only the list which starts with Urgent");

       }


   }

   public void swapNotes(int noteNumber){

       if(noteNumber<0){
           //DO NOT DO ANYTHING
       }
       else if(noteNumber < numberOfNotes()){
           if(notes.equals(1)){

               System.out.println("Swap the position");  

               //Collections.swap(arg0, arg1, arg2);
           }
       }

   }

   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.
   */
   int count=0;
   public void listNotes()
   {
       for(String note : notes) {
           System.out.println(note);
       }
       count++;
   }

   public void storeUrgentNote(String string) {
       // TODO Auto-generated method stub

       notes.add(string);

   }
}

***************

NoteBookTester.java

***********

package WordNode;

public class NotebookTester {

   static Notebook book;

   public NotebookTester() {
       // TODO Auto-generated constructor stub
   }

   public static void main(String[] args) {
       // TODO Auto-generated method stub

       book= new Notebook();

       book.storeNote("This is my first note");
       book.storeNote("Second note.");


       book.listNotes();


       nextOperation();


       secondOperation();

       thirdOperation();

       fourthOperation();

       // listUrgentNotes();


   }

   private static void fourthOperation() {
       // TODO Auto-generated method stub

       book = new Notebook();
       book.storeNote("one");
       book.storeNote("two");
       book.storeNote("three");
       book.listNotes();  

       book.swapNotes(1);
       book.listNotes();

   }

   /*private static void listUrgentNotes() {
       // TODO Auto-generated method stub
       book= new Notebook();

       book.storeNote("This is my first note.");
       book.storeNote("Second note.");
       book.storeUrgentNote("This is apparently urgent.");
       book.storeUrgentNote("This is urgent too.");
       book.listNotes();


   }*/

   private static void thirdOperation() {
       // TODO Auto-generated method stub


       book = new Notebook();
       book.storeNote("this is a non-urgent note");
       book.storeNote("this note is also not urgent");
       book.storeUrgentNote("Remember to study for exam!");
       book.listNotes();

       book.demoteNote(0);
       book.listNotes();
   }

   private static void secondOperation() {
       // TODO Auto-generated method stub

       book = new Notebook();
       book.storeNote("This is my first note.");
       book.storeNote("Second note.");
       book.storeUrgentNote("This is apparently urgent.");
       book.storeUrgentNote("This is urgent too.");
       book.listNotes();

       book.listUrgentNotes();


   }

   private static void nextOperation() {
       // TODO Auto-generated method stub


       Notebook book1= new Notebook();

       book1.storeNote("one");
       book1.storeNote("two");
       book1.storeNote("three");
       book1.removeNote(0);

       book1.listNotes();

   }

}

********

Output:

This is my first note
Second note.
two
three
This is my first note.
Second note.
This is apparently urgent.
This is urgent too.
this is a non-urgent note
this note is also not urgent
Remember to study for exam!
this is a non-urgent note
this note is also not urgent
Remember to study for exam!
one
two
three
one
two
three

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote