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. | Okay, Completed. |
2 | If a collection stores 10 objects, what value would be returned from a call to its size() method? | 9 |
3 | Write a method call using get() to return the fifth object stored in a collection called items. | (notes.get(4)); |
4 | What is the index of the last item stored in a collection of 15 objects? | 14 |
5 | Write a method call to add the object held in the variable meeting to a collection called notes. | public void storeNote(String note) { notes.add(note); } |
6 | Write a method call to remove the third object stored in a collection called dates. | public void removeNote (int 2) notes.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? | 5 |
8 | Implement a removeNote() method in your notebook. | Okay, Completed. |
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 listNotes()
|
10 | We know that the first note is stored at index zero in the ArrayList, so could we write the body of listAllNotes() along the following lines? System.out.println(notes.get(0)); System.out.println(notes.get(1)); System.out.println(notes.get(2)); etc. | No. Because if you ask for all the notes, you don’t need to invoke get(int NoteNumber); |
11 | Implement the listNotes() method in your version of the notebook project | Okay, Completed. |
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. | Okay, Completed. |
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. | Okay, Completed. |
14 | Modify showNote() and removeNote() to print out an error message if the note number entered was not valid. | Okay, Completed. |
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 make it much easier to provide the correct index when removing a note. | Okay, Completed. |
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. | Okay, Completed. |
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. | Okay, Completed. |
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. | Okay, Completed. |
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. | Okay, Completed. |
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. | Okay, Completed. |
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. | Okay, Completed. |
22 | Find a further example of casting in the getLot() method of the Auction class. | |
23 | What happens if you try to compile the Auction class without one of the casts? For instance, edit the showLots() method so that the first statement in the body of the while loop reads Lot lot = it.next(); | |
24 | Add a close() method to the Auction class. This should iterate over the collection of lots and print details of all the lots. For lots that have been sold, the details should include the name of the successful bidder, and the value of the winning bid. For lots that have not been sold, print a message that indicates this fact. | |
25 | Add a getUnsold() method to the Auction class with the following signature: public arrayList getUnsold() This method should iterate over the lots field, storing unsold lots in a new arrayList local variable. At the end of the method, return the list of unsold lots. | |
26 | Suppose that the Auction class includes a method that makes it possible to remove a lot from the auction. Assuming that the remaining lots do not have their lotNumber fields changed when a lot is removed, what impact would the ability to remove lots have on the getLot() method. | |
27 | Rewite getLot() so that it does not rely on a lot with a particular number stored at index (number-1) in the collection. You may assume that lots are always stored in increasing order of their lot number. | |
28 | Add a removeLot() method to the Auction class, having the following signature: public Lot removeLot (int number) This method should not assume that a lot with a given number is stored at any particular location within the collection. | |
29 | The Arraylist class is found in the java.util package. That package also includes a class called LinkedList. Try to find out what you can about the LinkedList class, and comare its methods with those of Arraylist. Which methods do they have in common and which are different? | |
30 | Continue working with the club project from exercise 4.19. Define a method in the Club class which returns the number of members that joined in a particular month. public int joinedInMonth(int month) Print an error message if the month parameter is outside the range 1-12. | |
31 | Define a method in the Club class, that removes from the club's collection all members who joined in a given month, and return these "purged" members in a separate collection object. public ArrayList purge(int month, int year) | |
32 | Open the product project and complete the StockManager class through this and the next few exercises. StockManager uses an ArrayList to store product items. Its addProduct()method already adds a product to the collection, but the following methods need completing: delivery(), findProduct(), printProductDetails(), and numberInStock(). Each product sold by the company is represented by an instance of the Product class, which records the product's ID, name, and how many of that product are currently in stock. The Product class defines the increasingQuantity() method to record increases in the stock level of that product. The sellOne() method records that, one item of the product has been sold by reducing the quantity field level by 1. Product has been provided for you, and you should not need to make alterations to it. Start by implementing the printProductDetails() method to ensure that you are able to iterate over the collection of products. Just print out the details of each Product returned by calling its toString() method. | |
33 | Implement the findProduct() method. This should look through the collection for a product whose ID field matches the ID argument of this method. If a matching product is found it should be returned as the method's result. If no matching product is found, return null. | |
34 | Implement the numberInStock() method. This should locate a product in the collection with a matching ID, and return the current quantity of that product as a result. If no product with a matching ID is found, return zero. | |
35 | Implement the delivery() method using a similar approach to that used in numberInStock(). It should find the product with the given ID in the list of products and then call its increaseQuantity() method. | |
36 | Implement a method in Stockmanager to print details of all products with stock levels below a given value. Modify the addProduct() method so that a new product cannot be added to the product list with the same ID as the existing one. Add to Stockmanager a method that finds a product from its name rather than its ID public Product findProduct (String name) In order to do this you need to know that two String objects S1 and S2, can be tested for equality with the boolean expression S1.equals(S2) | |
37 | Explore the weblog-analyzer project by creating a LogAnalyzer object and calling its analyzeData() method. Follow that with a call to its printHourlyCounts() method, which which will print the results of the analysis. Which are the busiest times of day? | |
38 | Write a declaration for an array variable people that could be used to refer to an array of Person objects. | |
39 | Write a declaration for an array variable vacant that could be used to refer to an array of boolean values. | |
40 | Read through the LogAnalyzer class and identify all the places where the hourCounts variable is used. At this stage, do not worry about what all the uses mean as they will be explained in the following sections. Note how often a pair of square brackets is used with the variable. | |
41 | What is wrong with the following array declarations? [] int counts; boolean [5000] occupied Correct them. | |
42 |
Given the following variable declarations: double[] readings; String[] urls; TicketMachine[] machines; write assignments that accomplish the following tasks: a) make the readings variable refer to an array that is able to hold 60 double values; b) make the urls variable refer to an array that is able to hold 90 String objects; c) make the machines variable refer to an array that is able to hold five TicketMachine objects. | |
43 | How many String objects are created by the following declaration? String[] labels = new String[20]; | |
44 | What is wrong with the following array creation? double[] prices = new double(50); Correct it. | |
45 | Check to see what happens if the for loop's condition is incorrectly written using the <= operator in printHourlyCounts(): for (int hour = 0; hour <= hourCounts.length; hour++) | |
46 | Rewrite the body of printHoulyCounts() so that the for loop is replaced by an equivalent while loop. Call the rewritten method to check that it prints the same results as before. | |
47 | Correct all the errors in the printGreater() method, whose intended funtion is to print all the values in the marks array that are greater than the mean. public void printGreater(double marks, double mean) { for (index = 0; index <= marks.length; index++) if (marks[index]>mean) System.out.println(marks[index]); } | |
48 | Rewrite the following method from the NoteBook class in the notebook2 project so that it uses a for loop rather than a while loop: public void listNodes() { int index = 0; while (index <> { System.out.println(notes.get(index)); index++; } } | |
49 |
Complete the numberOfAccesses() method, below, to count the total number of accesses recorded in the log file. Complete it by using a for loop to iterate over hourCounts: public int numberOfAccesses() { int total = 0; ............................ return total; } | |
50 | Add your numberOfAccesses() method to the LogAnalyzer class and check that it gives the correct result. | |
51 | Add a method busiestHour() to LogAnalyzer that returns the busiest hour. | |
52 | Add a method quietestHour() to LogAnalyzer that returns the number of the least busy hour. How is this different from busiestHour()? | |
53 | Which hour is returned by your busiestHour() method if more that one hour has the biggest count? | |
54 | Add a method to LogAnalyzer that finds which two-hour period is the busiest. return the value of the first hour of this period. | |
55 | Save the weblog-analyzer project under a different name, so that you can develop a new version that performs a more extensive analysis of the available data. For instance, it would be useful to know which days tend to be quieter than others - are there any seven-day cyclical patterns, for instance? In order to perform analysis of daily, monthly or yearly data you will need to make some changes to the LogEntry class. This already stores all the values from a single log line, but only the hour and minute values are available via accessors. Add further methods that make the remaining fields available in a similar way. Then add a range of additional analysis methods to the analyzer. | |
56 | If you have completed the previous exercise, you could extend the log file format with additional numerical fields. For instance, servers commonly store a numerical code that indicates whether an access was successful or not: the value 200 stands for a successful access, 403 means that access to the document was forbidden, and 404 means that the document could not be found. Have the analyzer provide information on the number of successful and unsuccessful accesses. This exercise is likely to be very challenging, as it will require you to make changes to every class in the project. | |
57 | In the lab-classes project that we have discussed in previous chapters, the LabClass class includes a students field to maintain a collection of student objects. Read through the LabClass class in order to reinforce some of the concepts we have discussed in this chapter. | |
58 | The LabClass class enforces a limit to the number of students who may be enrolled in a particular tutorial group. In view of this, do you think it would be more appropriate to use a fixed-size array rather than a flexible-size collection for the students field? Give reasons both for and against the alternatives. | |
59 | Java provides another type of loop: the do-while loop. Find out how this loop works and describe it. Write an example of a do-while loop that prints out the numbers from 1 to 10. To find out about this loop, find a description of the java language: for example.. http://java.sun.com/docs/books/tutorial/java/nutsandbolts/ in the section on Control Flow Statements | |
60 | Rewrite the notebook's listNotes() method using a do-while loop. | |
61 | Find out about Java's switch-case statement. What is its purpose? How is it used? Write an example. | |
62 | | |