Sample+Exam+task+3

Hi guys,

Did any one understand task 3 in short answers question? me and few of my friends try to do that task and we still not able to get through that question. can anyone help us with this???

Thnx...

you mean this?

**Question 3 (25 marks) **

The following incomplete WordList class is intended as a collection class for holding a list of words.

class WordList {  private static final int DEFAULTSIZE = 100;

 private int maxWords;  private String[] list;   private int length;  }

Here the constant provides a default value to replace any request for an illegal array size, the array provides a place to store the words, maxWords will record the size of the array, and length will record how many words are currently stored in it.

(a) Write a constructer for WordList that accepts a parameter for the size of the array. The constructor will create an array for the words of the requested size, or of the default size if the size requested is not appropriate. It will also record the size of the array and set the current length appropriately. (5 marks)

so, not sure why there's a maxWords and list.length, and assuming maximum size is 100 and size is not negative:

WordList(int size) { if (size > DEFAULTSIZE) size = DEFAULTSIZE; list = new String[size]; length = 0; } (b) Write a method addWord to add a word to the array. This will accept a String reference and return a Boolean value indicating whether or not the word in the String was able to be added. It should update the state of the collection object as appropriate. Note that a String object is immutable and can be added safely to the collection without needing to be copied. (10 marks)

(c) Write a method findWord to find the first occurrence in the collection of a word beginning with a specified prefix. This method will accept a reference to a String object containing the prefix and return a String reference to the word found, or null if there is no matching word. The String class contains a startsWith instance method, which accepts a String argument and returns a boolean indicating whether the instance string starts with the argument string.  (10 marks)  