CS 161 Lab #6

February 29th

Goals:

This week we'll work with the Notebook class to 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 "normal" kind, modify the way the list is displayed, and write some code that changes the position of notes within the ArrayList.

Let's Get Looping!

  1. Download the NotebookLab project and extract its contents, then open the project in BlueJ.
  2. Take a moment to familiarize yourself with the Java code in the NotebookLab class. It's exactly the same as the code from the book at this point: 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 an instance of Notebook and experiment with its methods to get a feel for how it works.
  3. Before we get to loops, let's first practice a bit with lists. 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 below.

    You can learn more about ways to add items to an ArrayList by reading the online documentation. There's a link to this from the course webpage, but you can also access it directly from within BlueJ: From the Help menu in BlueJ, select the "Java Class Libraries" item. It will open the Java API documentation in a web page. You can click the "ALL CLASSES" link to get a list of all of the built-in Java classes and click on ArrayList, or use the search box in the top right corner to find the ArrayList documentation. Once you see the documentation for ArrayList, scroll down to the Method Summary section, look for a flavor of add that allows you to specify the position of the new item, and use it in your storeUrgentNote method. It should work as illustrated below. (The output would actually appear in the terminal window — I'm showing it mixed in with the commands that generate it.)

    > Notebook book;
    > book = new Notebook();
    > book.storeNote("This is my first note.");
    > book.storeNote("Second note.");
    > book.listNotes()
    This is my first note.
    Second note.
    > book.storeUrgentNote("Study for exam.");
    > book.storeUrgentNote("Finish assignment.");
    > book.listNotes()
    Urgent: Finish assignment.
    Urgent: Study for exam.
    This is my first note.
    Second note.
    
  4. 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 that starts at zero and is incremented each time through the loop), 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. Your numbers should be printed out, but not added to the ArrayList, otherwise they'd be incorrect if items ever got removed. And yes, we did this in class, but try to recreate it here without peeking at that code — it's good practice.
    > Notebook book;
    > book = new Notebook();
    > book.storeNote("one");
    > book.storeNote("two");
    > book.storeNote("three");
    > book.listNotes();
    0) one
    1) two
    2) three
    > book.removeNote(0);
    > book.listNotes();
    0) two
    1) three
    
  5. 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.) Ideally, you should number them correctly no matter where you find them. (For example, in the output below if note number three had also been urgent, it would show up with a 3) in front of it even though we didn't print note #2.)
    > Notebook book;
    > 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()
    0) Urgent: This is urgent too.
    1) Urgent: This is apparently urgent.
    2) This is my first note.
    3) Second note.
    > book.listUrgentNotes()
    0) Urgent: This is urgent too.
    1) Urgent: This is apparently urgent.
    
  6. 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, but that's ok:
    > Notebook book;
    > 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();
    0) Urgent: Remember to study for exam!
    1) this is a non-urgent note
    2) this note is also not urgent
    > book.demoteNote(0);
    > book.listNotes();
    0) this is a non-urgent note
    1) this note is also not urgent
    2) Urgent: Remember to study for exam!
    
  7. Write a method called printFirstFew that takes an int as its input — the number of notes to be printed — and only prints the specified number of list items, starting at the top. If the user passes in a value that's larger than the number of items in the list, just print them all without complaining.

    When implementing this, think about how we printed out only some of the list items in the examples from class. (Look at listLongNotes, for example, or showNotesContaining.) You'll want an if statement inside a for-each loop, where the if statement's condition makes use of a counter variable (one that gets incremented each time we go through the loop). You could also use a return statement within a for-each loop to leave the method once the required number of items has been printed.

    > Notebook book;
    > book = new Notebook();
    > book.storeNote("Note 1");
    > book.storeNote("Note 2");
    > book.storeNote("Note 3");
    > book.storeNote("Note 4");
    > book.storeNote("Note 5");
    > book.printFirstFew(3);
    Note 1
    Note 2
    Note 3
    > book.printFirstFew(10);
    Note 1
    Note 2
    Note 3
    Note 4
    Note 5
    

Extras

If time permits, consider trying one of the following exercises:


Brad Richards, 2024