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
-
"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...
-
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...
-
Saving Energy Inc. is a company that wants to buy coal deposits but does not want the financing for the purchase to be reported on its financial statements. The company therefore establishes a trust...
-
A collar that can slide on a vertical rod is subjected to the three forces shown. Determine (a) the required value of if the resultant of the three forces is to be horizontal, (b) the corresponding...
-
What is FGP?
-
A manufacturing company produces steel housings for electrical equipment. The main component part of the housing is a steel trough that is made out of a 14- gauge steel coil. It is produced using a...
-
What can you do to increase the performance of an embedded system?
-
In what way might a policy to pay no dividends affect a hotel corporations market price of shares? If the policy were to pay out all net income in dividends, how might this affect the company's...
-
You have calculated a need for $4.4 million in resources (salaries, supplies, etc.) for next year in order to run your population health outreach program. Finance has calculated independently that...
-
Jackson & Jackson is a globally famous pharmaceutical company with a common stock market capitalization of $500 billion. It has 500 million shares outstanding. 1. What is its share price? 2. Jackson...
-
What is financial leverage? What is the impact of financial leverage on wealth creation? What is the relationship between financial leverage and risk?
-
Given two bond portfolios with the same duration, but different convexities, portfolio managers must choose the one with higher convexity. Do you agree? Why?
-
Draw a flowchart for a program that calculates speed using the following formula. The program must ask the user to input distance travelled in kilometers and time travelled in hours. Then, it will...
-
Explain the evolution of Communication Technology chronological over the past decade; discuss how it has imparted our society POSITIVELY and NEGATIVELY over the last decade.
-
Q1) What is the a3 Value Q2) What is the a7 Value Q3) What is the a4 Value Q4) What is the b3 Value Q5) What is the b2 Value Q6) What is the sign of 2nd constraint? A pastry chef at a bakery wants to...
-
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?
-
In testing the claim that the mean IQ score of statistics students is greater than 100, the alternative hypothesis is expressed as > 100. Decide whether the statement makes sense (or is clearly...
-
A significance level of 0.05 indicates that the probability of making a type I error is 0.05.
-
H a : 2.55, n = 100, x = 2.58, = 0.29. Find the value of the standard score, z, and determine whether the alternative hypothesis is supported at a 0.05 significance level.
Words And Power Computers Language And US Cold War Values 1st Edition - ISBN: 303070372X - Free Book
Study smarter with the SolutionInn App