1
Open the notebook1 project in BlueJ and create a Notebookobject.
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.
Done
2
If a collection stores 10 objects, what value would be returned from a call to its size() method?
10.
3
Write a method call using get() to return the fifth object stored in a collection called items.
items.get(5); we have to put semi- column after item.gets()
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 meetingto a collection called notes.
notes.add(meeting);
6
Write a method call to remove the third object stored in a collection called dates.
dates.remove(3);
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.
Done. Had some problem.
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?
We should use voids, and we dont need any parameters for this.
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.
Only if the number of notes is constant or remains the same all the time and that is not the case here.
11
Implement the listNotes() method in your version of the notebook project
Done
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.
Done
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.
Done
14
Modify showNote() and removeNote() to print out an error message if the note number entered was not valid.
Done it.
15
Modify the listNodes() method so that it prints the value of theindex 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.
Done
16
Within a single execution of the listNodes() method, the notescollection 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.
same results, but I am confused why??
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.
Done
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.
Statements are not like loops. They check through the conditions one and move on. In loops, you get stuck inside a circle, and you need something to brake out from it, unlike in statements.
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 storeMembership objects in a collection.
Within Club, define a field for an Arraylist. Use an appropriateimport 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.
(return members.size();)
21
Membership of a club is represented by an instance of theMembership 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 followingsignature.
public void join(Membership member)
Complete the join() method
When you wish to add a new Membership object to the Clubobject 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 theMembership 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.
21 | Membership of a club is represented by an instance of theMembership 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 followingsignature. public void join(Membership member) Complete the join() method When you wish to add a new Membership object to the Clubobject 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 theMembership 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. The join method simply calls the add method of ArrayList. It is memberList.add(member); . |
22 | Find a further example of casting in the getLot() method of the Auction class. | In the getLot method, there is the following line: Lot selectedLot=(Lot) lots.get(lotnumber-1). This helps us by getting Lot from the array.list |
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(); | It gives an error. |
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. | I am not sure. |
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. | I am confused. How to iterate? |
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. | It does not affect because a lot of numbers will not be increased by one. |
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. | Having problem |
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. | Confused . I need to ask these questions to you in class. |
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? | ArrayList is based on Linked List. They both have the same methods but inArrayLists you can add objects to the middle of the array, while in LinkedLists, you can only do that at the starting or the end. |
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. | Array List is causing me problems. |
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 anArrayList 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 Productclass 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 itstoString() 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 itsincreaseQuantity() 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 expressionS1.equals(S2) | |
37 | Explore the weblog-analyzer project by creating a LogAnalyzerobject 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? | Done. The busiest hour of the day is the twelfth houe |
38 | Write a declaration for an array variable people that could be used to refer to an array of Person objects. | private Person[ ] people; |
39 | Write a declaration for an array variable vacant that could be used to refer to an array of boolean values. | private boolean[ ] vacant; |
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. | analyzeHourlyData(). printHourlyCounts(). |
41 | What is wrong with the following array declarations? [] int counts; boolean [5000] occupied Correct them. | int[] counts; The int is only specified when the array is declared. |
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. | a.) new double[60]; b.)new String[90]; c.)new TicketMachine[5]; |
43 | How many String objects are created by the following declaration? String[] labels = new String[20]; | This makes space for string objects. It makes space for 20 String objects. |
44 | What is wrong with the following array creation? double[] prices = new double(50); Correct it. | new double[50] In array, we do not use circle brackets. |
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++) | It should give an 0. Dont know why. |
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. | Done. It will simply look like this: System.out.println("Hr: Count"); int hour = 0; while(hour < hourCounts.length) { System.out.println(hour + ": " + hourCounts[hour]); hour++; } |
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]); } | We just need to change the parameter double[ ]. We need the square brackets. 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 < notes.size() { System.out.println(notes.get(index)); index++; } } | public void listNotes() { for(int i=0; i<notes.size(); i++) { System.out.println(notes.get(i)); } } |
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. | Done but Coding gives error. public int busiestHour() { int result = 0; int currentHighest = 0; for(int i=0; i<hourCounts.length; i++) { if(hourCounts[i] > currentHighest) { result = i } } return result; } |
52 | Add a method quietestHour() to LogAnalyzer that returns the number of the least busy hour. How is this different from busiestHour()? | Done. Here is the code for it: public int quietestHour() { int result = 0; int currentLowest = hourCounts[0]; for(int i=0; i<hourCounts.length; i++) { if(hourCounts[i] < currentLowest) { result = i; } } return result; } |
53 | Which hour is returned by your busiestHour() method if more that one hour has the biggest count? | The earlier hour. |
No comments:
Post a Comment