Barnes and Kolling Chapter Four Questions
Barnes and Kolling Chapter Four Questions
1 | Open the notebook1 project in BlueJ and create a Notebook object. Store a few notes into it - they are simple strings - then check that the number of notes returned by numberOfNotes() matches the number that you stored. When you use the showNote() method, you will need to use a parameter value of zero to print the first note, one to print the second note and so on. | I made two notes (reminders) as shown in the terminal window, and the int number of notes matches the amount I had created: |
2 | If a collection stores 10 objects, what value would be returned from a call to its size() method? | It would return 10. |
3 | Write a method call using get() to return the fifth object stored in a collection called items. | public ArrayList getNote(int n) { if(0>n){ } else return notes[n]; } |
4 | What is the index of the last item stored in a collection of 15 objects? | Its index would be 14. |
5 | Write a method call to add the object held in the variable meeting to a collection called notes. | public void storeNote(String meeting) { notes.add(meeting); } |
6 | Write a method call to remove the third object stored in a collection called dates. | { dates.remove(2); } |
7 | Suppose that an object is stored at index 6 in a collection. What will be its index immediately after the objects at index 0 and index 9 are removed? | After removing: Index 0, Index 6 will become Index 5. Index 9, Index 6 will stay Index 6. |
8 | Implement a removeNote() method in your notebook. | Here is my method's code: /** * Remove a note. */ public void removeNote(int noteNumber) { if(noteNumber<0) { } else if(noteNumber<notes.size()) notes.remove(noteNumber); else{ } } |
9 | What might the header of a listAllNotes() method look like? What sort of return type should it have? Does it need to take any parameters? | public void listAllNotes() This method needs a void return type, since we would print the notes out, and requires no parameters, as its returning ALL notes. |
10 | We know that the first note is stored at index zero in the ArrayList, so could we write the body of listAllNodes() along the following lines? System.out.println(notes.get(0)); System.out.println(notes.get(1)); System.out.println(notes.get(2)); etc. | No we could not. This is because we can't predetermine the amount of notes to be created, and therefore we will need to set a parameter. |
11 | Implement the listNotes() method in your version of the notebook project | Here's what it looks like: |
12 | Create a Notebook and store a few notes in it. Use the listNotes() method to print them out to check that the method works as it should. | Skip two notes, those are from before, and you'll see all of my notes printed: |
13 | If you wish you could use the debugger to help yourself understand how the statements in the body of the while loop are repeated. Set a breakpoint just before the loop, and step through the method until the loop's condition evaluates to false. | At the end of the method after the evaluation was false: |
14 | Modify showNote() and removeNote() to print out an error message if the note number entered was not valid. | There are the two error messages that I created in the terminal window: |
15 | Modify the listNodes() method so that it prints the value of the index local variable in front of each note. For instance: 0: Buy some bread 1: Recharge phone 2: 11:30 Meeting with Annabel This makes it much easier to provide the correct index when removing a note. | The result: |
16 | Within a single execution of the listNodes() method, the notes collection is asked repeadly how many notes it is currently storing. This is done every time the loop condition is checked. Does the value returned by size() vary from one check to the next? If you think the answer is "No", then rewrite the listNodes() method so that the size of the notes collection is determined only once and is stored in a local variable in the loops condition rather than the call to size(). Check that this version gives the same results. | It worked, although in different circumstances size can change, if you make it do so. However, in this case it doesn't, and it worked. |
17 | Change your notebook so that the notes are numbered starting from 1, rather than zero. Remember that the arrayList object will still be using indices starting from zero, but you can present the notes numbered from 1 in your listing. Make sure you modify showNote() and removeNote() appropriately. | Modified: |
18 | Some people refer to if statements as if loops. From what we have seen of the operation of a while loop, explain why it is not appropriate to refer to if statements as loops. | While is stating that during the time that the condition is met, do this loop, but if is asking whether the condition is being met, and if so, then do the loop.. |
19 | Use the club project to complete the following exercises. Your task is to complete the Club class, an outline of which has been provided in the project. The Club class is intended to store Membership objects in a collection. Within Club, define a field for an Arraylist. Use an appropriate import statement for this field. Make sure that all the files in the project compile before moving on to the next exercise. | Done: |
20 | Complete the numberOfMembers() method to return the current size of the collection. Until you have a method to add objects to the collection this will always return zero, of course, but it will be ready for further testing later. | Done: |
21 | Membership of a club is represented by an instance of the Membership class. A complete version of Membership is already provided for you in the club project, and it should not need any modification. An instance contains details of a person's name, and the month and year in which they joined the club. All membership details are filled out when an instance is created. A new Membership object is added to a Club object's collection via the Club object's join() method which has the following signature. public void join(Membership member) Complete the join() method When you wish to add a new Membership object to the Club object from the object bench, there are two ways you can do this. Either create a new Membership object on the object bench, call the join() method on the Club object, and click on the Membership object to supply the parameter; ..or call the join() method on the Club object and type into the constructor's parameter dialog box: new Membership("member's name ",month, year) Each time you add one, use the numberOfMembers() method to check both that the join() method is adding to the collection, and that the numberOfMembers() method is giving the correct result. | Done, this is the script I ended up with including all the steps, and working: import java.util.ArrayList; /** * Store details of club memberships. * * @author (your name) * @version (a version number or a date) */ public class Club { private ArrayList members; /** * Constructor for objects of class Club */ public Club() { members = new ArrayList(); } /** * Add a new member to the club's list of members. * @param member The member object to be added. */ public void join(Membership member) { members.add(member); } /** * @return The number of members (Membership objects) in * the club. */ public int numberOfMembers() { return members.size(); } } |