Write a method countDuplicates that returns the number of duplicates in a sorted list. The list...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Write a method countDuplicates that returns the number of duplicates in a sorted list. The list will be in sorted order, so all of the duplicates will be grouped together. For example, if a variable list stores the sequence of values below, the call of list.countDuplicates () should return 7 because there are 2 duplicates of 1, 1 duplicate of 3, 1 duplicate of 15, 2 duplicates of 23 and 1 duplicate of 40: [1, 1, 1, 3, 3, 6, 9, 15, 15, 23, 23, 23, 40, 401 Remember that you may assume that the list is in sorted order, so any duplicates would occur consecutively. Assume that you are adding this method to the LinkedIntList class as defined below: public class LinkedIntList { private ListNode front; // null for an empty list } Type your solution here: 1/*public int countDuplicates() { int sum= 0; ListNode current = front; 2 3 4 5 6 LLLLLLL 7 8 T06X 9 10 11 12 13 14 15 16 17 } 18 */| if (front == null) { return 0; } while (current.next != null) { if (current. data == current.next.data) { sum++; } current current.next; } return sum; This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem. E 4 Indent OBJP4 Exercise 16.6: hasTwoConsecutive Language/Type: Java implementing Linked Lists LinkedIntList Related Links: LinkedIntList.java Author: Whitaker Brand (on 2016/09/08) Write a method has TwoConsecutive that returns whether or not a list of integers has two adjacent numbers that are consecutive integers (true if such a pair exists and false otherwise). For example, if a variable list stores the following sequence of values, then the call list.hasTwoConsecutive() should return true because the list contains the adjacent numbers (7, 8) which are a pair of consecutive numbers: [1, 18, 2, 7, 8, 39, 18, 40] If the list had stored the following sequence of values, then the method should return false: [1, 18, 17, 2, 7, 39, 18, 40, 8] This sequence contains some pairs of numbers that could represent consecutive integers (e.g., 1 and 2, 7 and 8, 39 and 40), but those pairs of numbers are not adjacent in the sequence. The list also has a pair of adjacent numbers (18, 17) that are not in the right order to be considered consecutive. You may not make any assumptions about how many elements are in the list. Assume that you are adding this method to the LinkedIntList class as defined below: public class LinkedIntList { private ListNode front; // null for an empty list } Type your solution here: 1/*public boolean has Two Consecutive() { if (front == null) return false; 2 34567 8 9 10 11 12 13 14 15 16 17 } 18 */ ListNode prev = front; ListNode current = prev.next; while (current != null) { } if (prev.data + 1 ==current.data) return true; prev = current; current= prev.next; return false; This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem. Submit E 4 Indent Sound F/X Highlighting O BJP4 Exercise 16.7: deleteBack Language/Type: Related Links: Author: Write a method deleteBack that deletes the last value (the value at the back of the list) and returns the deleted value. If the list is empty, your method should throw a NoSuch ElementException. Assume that you are adding this method to the LinkedIntList class as defined below: public class LinkedIntList { private ListNode front; // null for an empty list } Type your solution here: 1/*public int deleteBack() { if (front == null) 2 3 4 Java implementing Linked Lists LinkedIntList LinkedIntList.java Marty Stepp (on 2016/09/08) 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 } 22 */ } throw new NoSuch ElementException(); int data; if(front.next == null) { data front.data; front= null; return data; ListNode n = front; while(n.next.next!= null) n = n.next; data = n.next.data; n.next = null; return data; This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem. You passed 6 of 6 tests. Submit Indent Sound F/X Highlighting 4 O BJP4 Exercise 16.8: switchPairs Language/Type: * Java implementing Linked Lists LinkedIntList LinkedIntList.java Robert Baxter (on 2016/09/08) Related Links: Author: Write a method switchPairs that switches the order of elements in a linked list of integers in a pairwise fashion. Your method should switch the order of the first two values, then switch the order of the next two, switch the order of the next two, and so on. For example, if the list initially stores these values: [3, 7, 4, 9, 8, 12] Your method should switch the first pair (3, 7), the second pair (4, 9), the third pair (8, 12), etc. to yield this list: [7, 3, 9, 4, 12, 8] If there are an odd number of values, the final element is not moved. For example, if the list had been: [3, 7, 4, 9, 8, 12, 2] It would again switch pairs of values, but the final value (2) would not be moved, yielding this list: [7, 3, 9, 4, 12, 8, 2] Assume that we are adding this method to the LinkedIntList class as shown below. You may not call any other methods of the class to solve this problem, you may not construct any new nodes, and you may not use any auxiliary data structures to solve this problem (such as an array, ArrayList, Queue, String, etc.). You also may not change any data fields of the nodes. You must solve this problem by rearranging the links of the list. public class LinkedIntList { private ListNode front; 2 3 } public class ListNode { public int data; public ListNode next; } Type your solution here: 1/*public void switchPairs () { ListNode current = front; 7 8 9 10} 11 */ while (current != null && current.next != null) { int temp = current.data; current.data = current.next.data; } current.next. data = temp; current current.next.next; This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem. You passed 7 of 7 tests. Submit 4 Indent Sound F/X Highlighting E BJP4 Exercise 16.9: stutter Language/Type: Related Links: Author: Java implementing Linked Lists LinkedIntList stutter LinkedIntList.java Whitaker Brand (on 2016/09/08) Write a method stutter that doubles the size of a list by replacing every integer in the list with two of that integer. For example, suppose a variable list stores the following sequence of integers: [1, 8, 19, 4, 17] After a call of list.stutter(), it should store the following sequence of integers: [1, 1, 8, 8, 19, 19, 4, 4, 17, 17] Assume that you are adding this method to the LinkedIntList class as defined below: public class LinkedIntList { private ListNode front; // null for an empty list } Type your solution here: 1/*public void stutter() { 4 7 8} 9 */ ListNode current = front; while (current != null) { current.next = new ListNode (current.data, current.next); current = current.next.next; } This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem. You passed 5 of 5 tests. Submit E 4 Indent Sound F/X Highlighting Write a method countDuplicates that returns the number of duplicates in a sorted list. The list will be in sorted order, so all of the duplicates will be grouped together. For example, if a variable list stores the sequence of values below, the call of list.countDuplicates () should return 7 because there are 2 duplicates of 1, 1 duplicate of 3, 1 duplicate of 15, 2 duplicates of 23 and 1 duplicate of 40: [1, 1, 1, 3, 3, 6, 9, 15, 15, 23, 23, 23, 40, 401 Remember that you may assume that the list is in sorted order, so any duplicates would occur consecutively. Assume that you are adding this method to the LinkedIntList class as defined below: public class LinkedIntList { private ListNode front; // null for an empty list } Type your solution here: 1/*public int countDuplicates() { int sum= 0; ListNode current = front; 2 3 4 5 6 LLLLLLL 7 8 T06X 9 10 11 12 13 14 15 16 17 } 18 */| if (front == null) { return 0; } while (current.next != null) { if (current. data == current.next.data) { sum++; } current current.next; } return sum; This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem. E 4 Indent OBJP4 Exercise 16.6: hasTwoConsecutive Language/Type: Java implementing Linked Lists LinkedIntList Related Links: LinkedIntList.java Author: Whitaker Brand (on 2016/09/08) Write a method has TwoConsecutive that returns whether or not a list of integers has two adjacent numbers that are consecutive integers (true if such a pair exists and false otherwise). For example, if a variable list stores the following sequence of values, then the call list.hasTwoConsecutive() should return true because the list contains the adjacent numbers (7, 8) which are a pair of consecutive numbers: [1, 18, 2, 7, 8, 39, 18, 40] If the list had stored the following sequence of values, then the method should return false: [1, 18, 17, 2, 7, 39, 18, 40, 8] This sequence contains some pairs of numbers that could represent consecutive integers (e.g., 1 and 2, 7 and 8, 39 and 40), but those pairs of numbers are not adjacent in the sequence. The list also has a pair of adjacent numbers (18, 17) that are not in the right order to be considered consecutive. You may not make any assumptions about how many elements are in the list. Assume that you are adding this method to the LinkedIntList class as defined below: public class LinkedIntList { private ListNode front; // null for an empty list } Type your solution here: 1/*public boolean has Two Consecutive() { if (front == null) return false; 2 34567 8 9 10 11 12 13 14 15 16 17 } 18 */ ListNode prev = front; ListNode current = prev.next; while (current != null) { } if (prev.data + 1 ==current.data) return true; prev = current; current= prev.next; return false; This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem. Submit E 4 Indent Sound F/X Highlighting O BJP4 Exercise 16.7: deleteBack Language/Type: Related Links: Author: Write a method deleteBack that deletes the last value (the value at the back of the list) and returns the deleted value. If the list is empty, your method should throw a NoSuch ElementException. Assume that you are adding this method to the LinkedIntList class as defined below: public class LinkedIntList { private ListNode front; // null for an empty list } Type your solution here: 1/*public int deleteBack() { if (front == null) 2 3 4 Java implementing Linked Lists LinkedIntList LinkedIntList.java Marty Stepp (on 2016/09/08) 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 } 22 */ } throw new NoSuch ElementException(); int data; if(front.next == null) { data front.data; front= null; return data; ListNode n = front; while(n.next.next!= null) n = n.next; data = n.next.data; n.next = null; return data; This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem. You passed 6 of 6 tests. Submit Indent Sound F/X Highlighting 4 O BJP4 Exercise 16.8: switchPairs Language/Type: * Java implementing Linked Lists LinkedIntList LinkedIntList.java Robert Baxter (on 2016/09/08) Related Links: Author: Write a method switchPairs that switches the order of elements in a linked list of integers in a pairwise fashion. Your method should switch the order of the first two values, then switch the order of the next two, switch the order of the next two, and so on. For example, if the list initially stores these values: [3, 7, 4, 9, 8, 12] Your method should switch the first pair (3, 7), the second pair (4, 9), the third pair (8, 12), etc. to yield this list: [7, 3, 9, 4, 12, 8] If there are an odd number of values, the final element is not moved. For example, if the list had been: [3, 7, 4, 9, 8, 12, 2] It would again switch pairs of values, but the final value (2) would not be moved, yielding this list: [7, 3, 9, 4, 12, 8, 2] Assume that we are adding this method to the LinkedIntList class as shown below. You may not call any other methods of the class to solve this problem, you may not construct any new nodes, and you may not use any auxiliary data structures to solve this problem (such as an array, ArrayList, Queue, String, etc.). You also may not change any data fields of the nodes. You must solve this problem by rearranging the links of the list. public class LinkedIntList { private ListNode front; 2 3 } public class ListNode { public int data; public ListNode next; } Type your solution here: 1/*public void switchPairs () { ListNode current = front; 7 8 9 10} 11 */ while (current != null && current.next != null) { int temp = current.data; current.data = current.next.data; } current.next. data = temp; current current.next.next; This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem. You passed 7 of 7 tests. Submit 4 Indent Sound F/X Highlighting E BJP4 Exercise 16.9: stutter Language/Type: Related Links: Author: Java implementing Linked Lists LinkedIntList stutter LinkedIntList.java Whitaker Brand (on 2016/09/08) Write a method stutter that doubles the size of a list by replacing every integer in the list with two of that integer. For example, suppose a variable list stores the following sequence of integers: [1, 8, 19, 4, 17] After a call of list.stutter(), it should store the following sequence of integers: [1, 1, 8, 8, 19, 19, 4, 4, 17, 17] Assume that you are adding this method to the LinkedIntList class as defined below: public class LinkedIntList { private ListNode front; // null for an empty list } Type your solution here: 1/*public void stutter() { 4 7 8} 9 */ ListNode current = front; while (current != null) { current.next = new ListNode (current.data, current.next); current = current.next.next; } This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem. You passed 5 of 5 tests. Submit E 4 Indent Sound F/X Highlighting
Expert Answer:
Answer rating: 100% (QA)
Heres the implementation of the countDuplicates method public int countDup... View the full answer
Related Book For
Building Java Programs A Back To Basics Approach
ISBN: 9780135471944
5th Edition
Authors: Stuart Reges, Marty Stepp
Posted Date:
Students also viewed these programming questions
-
Select a public for-profit corporation of your choice to write a 34 page (not counting the cover page, abstract or reference list) report in which you: Summarize the GAAP versus IFRS existing and...
-
"Fortran, Algol and Lisp invented most programming language concepts 50 years ago; adding the concept of object-orientation suffices to explain all programming languages to date". To what extent is...
-
Steve is married filing jointly with his wife Nancy and operates a business as a sole proprietor. His business is a small tax and accounting business where he has two employees who are paid a total...
-
The following are the Ledger Balance (in thousands) extracted from the books of Vaishnavi Bank Ltd as on March 31, 2016. The bank's Profit and Loss Account for the year ended and Balance Sheet as at...
-
The phase line in Exercise 11. From the given phase-line diagram, sketch a possible graph of the rate of change of x as a function of x. start here x=2 x=4 8
-
The pressure in a natural gas pipeline is measured by the manometer shown in Fig. P1 - 69E with one of the arms open to the atmosphere where the local atmospheric pressure is 14.2 psia. Determine the...
-
Which of the following would strengthen organizational control over large-scale data processing center? a. requiring the user departments to specify the general control standards necessary for...
-
On January 1, 2016, Bacco Company had a balance of $72,350 in its Delivery Equipment account. During 2016, Bacco purchased delivery equipment that cost $22,100. The balance in the Delivery Equipment...
-
7:42 -5 1 -5 .III LTE 1 5 Find the following information pertaining to the polynomial, f(x), graphed above. (a) The zeros of f(x) are x = (separate by commas) and the zero x = has multiplicity, the...
-
Harrison Printing has projected its sales for the first eight months of 2014 as follows: Harrison collects 20 percent of its sales in the month of the sale, 50 percent in the month following the...
-
Regarding Astrology being a science, what is atleast one threat to internal or external validity or reliability? Why is this threat problematic and how could it be remedied? Consider which validity...
-
How can the internal auditor provide management with summary reports on the effectiveness of its control systems?
-
What steps could the internal auditor take to save time when assigned to audit an off-site location?
-
To avoid bias in sample selection, a. Every item examined should exceed a given value. b. Every item must have an equal chance of selection. c. Use cluster sampling. d. Always start at the first page...
-
When parties must arbitrate their disputes?
-
Describe the potential source of comparisons between work "programmed" and work "accomplished."
-
Cost Definitions Picante, Inc., provided the following information for the last calendar year: Beginning inventory: Direct materials $28,000 Work in process 46,800 Ending inventory: Direct materials...
-
You are maintaining a subsidiary ledger account for Police-Training Expenditures for 2013. The following columns are used: Inventory purchases are initially recorded as expenditures. Record the...
-
Write a toString method for a binary tree of integers. The method should return "empty" for an empty tree. For a leaf node, it should return the data in the node as a string. For a branch node, it...
-
Write a recursive method called dedup that takes a string as a parameter and that returns a new string obtained by replacing every sequence of repeated adjacent letters with just one of that letter....
-
What is the difference between the public and private keywords? What items should be declared private?
-
Guarantee Oil Companys internal land department incurred costs of $150,000 in acquiring leases. Of the 1 million acres of prospects, only 450,000 acres were leased. a. How much, if any, of the...
-
Given the following data for Float Energy: a. Give the entries to record the abandonment of both Lease A and Lease B. b. Give the entries assuming instead that both Lease A and Lease B were proved,...
-
Gusher Oil Corporation obtained shooting rights only for \($10,000\) on 5,000 acres owned by Mr. Q and shooting rights coupled with an option to lease for \($12,000\) on 4,000 acres owned by Mr. S....
Study smarter with the SolutionInn App