Answered step by step
Verified Expert Solution
Question
1 Approved Answer
7. Revise your answer from the previous question so that the code also works correctly for the situation where the target might be the null
7. Revise your answer from the previous question so that the code also works correctly for the situation where the target might be the null reference 8. Consider this code using the ArrayBag from Section 5.2: Integer i new Integer (42); Integer j new Integer(43); ArrayBag bnew ArrayBag) Add two i's and one j: b.add(j) Draw a diagram showing the reference variables i and j with their pointers to Integer objects, and also show b's data array with its pointers after these add statements 9. Suppose that the variable head is a reference to the head node of a linked list of objects. Each node has an instance variable called link (which is a reference to the next node) and another instance variable called data (which is an Object that's stored in the node). Write a few lines of code to count the number of occurrences of a specific non-null target Object on the list. At the end of your code, a variable called answer should indieate how many times the target appears in the array. Use target.equals to test for equality. 10. Revise your answer from the previous question so that the code also works correctly for the situation where the target might be the null reference. 11. Suppose that x is an Object variable. Write a small piece of Java code that will print the message "Leopold is cool" provided that x is actually a reference to an object of the Integer wrapper class. 2. Suppose that i is an Iterator objcet. Write a small picce of Java code that uses i.has Next and i.next to print all the Objects of i to System.out 13. Describe one primary advantage of an external iterator versus an internal iterator. 7. Revise your answer from the previous question so that the code also works correctly for the situation where the target might be the null reference 8. Consider this code using the ArrayBag from Section 5.2: Integer i new Integer (42); Integer j new Integer(43); ArrayBag bnew ArrayBag) Add two i's and one j: b.add(j) Draw a diagram showing the reference variables i and j with their pointers to Integer objects, and also show b's data array with its pointers after these add statements 9. Suppose that the variable head is a reference to the head node of a linked list of objects. Each node has an instance variable called link (which is a reference to the next node) and another instance variable called data (which is an Object that's stored in the node). Write a few lines of code to count the number of occurrences of a specific non-null target Object on the list. At the end of your code, a variable called answer should indieate how many times the target appears in the array. Use target.equals to test for equality. 10. Revise your answer from the previous question so that the code also works correctly for the situation where the target might be the null reference. 11. Suppose that x is an Object variable. Write a small piece of Java code that will print the message "Leopold is cool" provided that x is actually a reference to an object of the Integer wrapper class. 2. Suppose that i is an Iterator objcet. Write a small picce of Java code that uses i.has Next and i.next to print all the Objects of i to System.out 13. Describe one primary advantage of an external iterator versus an internal iterator
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started