tags:

views:

136

answers:

3

This is what I have so far but I don't now what to do next. The question is as follows (sorry the coding is not all appearing in one box): Implement a method

public void search (String searchString) { }

to iterate through the notes ArrayList until it finds a note that contains the searchString. It should then print either the item found or the message "String not found". When testing check for a String that is in the list and for one that isn't.

Code:

import java.util.ArrayList;
import java.util.Iterator;

/**
 * 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 2008.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();
}

/**
 * Show a note.
 * @param noteNumber The number of the note to be shown.
 */
public void showNote(int noteNumber)
{
    if(noteNumber < 0) {
        // This is not a valid note number, so do nothing.
        System.out.println("invalid index given");
    }
    else if(noteNumber < numberOfNotes()) {
        // This is a valid note number, so we can print it.
        System.out.println(notes.get(noteNumber));
    }
    else {
         System.out.println("there are fewer items in the notebook than that");
        // This is not a valid note number, so do nothing.
    }
}

public void removeNote(int noteNumber)
{
    if(noteNumber < 0) {
        // This is not a valid note number, so do nothing.
         System.out.println("invalid index given");
    }
    else if(noteNumber < numberOfNotes()) {
        // This is a valid note number.
        notes.remove(noteNumber);
    }
    else {
        System.out.println("there are fewer items in the notebook than that");
        // This is not a valid note number, so do nothing.
    }
}

public void multiplesOfFive()
{

    int i = 10;
    while(i < 100) 
    {
        System.out.println(i);
        i = i + 5;
    }
}

public int sum(int a, int b)
{

    int index = a;
    int result = 0;
    while(index <= b) 
    {
        result = result + index;
        index = index + 1;
    }
    return result;
}

public int product(int a, int b)
{

    int index = a;
    int result = 1;
    while(index <= b) 
    {
        result = result * index;
        index = index + 1;
    }
    return result;
}

public boolean 
    isPrime (int n)
    {
       if (n<=1)return false;
       if (n==2) return true;
       for (int i = 2;i<=n-1;i++)
       {
         if (n%i==0)return false;
       }
       return true;
     }
}
A: 

Use one of the new for-each style loops to iterate over the List of notes:

for (String string : notes) {
    // This will loop over all the Strings in the notes List.
    // Perform your logic here.
}
Kaleb Brasee
+1  A: 

two ideas to consider:

  1. When you compose your search method, consider utilizing the contains method in the String class as you iterate (see Kaleb Brasee's post).
  2. ensure that you handle the case when a null is passed in as the search param.
akf
A: 

If the list is not in alphabetical order you need to loop through the list comparing each string against the search string. Once you find a match you can break the loop (using a return true (or the string) would be the easiest way) then outside the loop you can place a return false to signify that a match was not found.

Some methods you will need to use:
ArrayList:

  • size() - gives you the size of the list so you know when you have reached the end
  • get( int index ) - returns the item in the list at the specified index

String:

  • equals( String cmp ) - compares 2 strings and returns an int

It would be good to become familiar with the Java API so that you can find methods and their return values.

If the list is in alphabetical order there are more efficient ways to search.

Josh Curren