Question: Question 7 (15 marks) Write a method named frequency that takes as input a list of objects containing a character value and a boolean flag

 Question 7 (15 marks) Write a method named frequency that takesas input a list of objects containing a character value and aboolean flag initially set to false, and prints the frequency of each

Question 7 (15 marks) Write a method named frequency that takes as input a list of objects containing a character value and a boolean flag initially set to false, and prints the frequency of each character of the list. Every time an element is counted, its flag is set to true so that it is not printed or counted a second time. List 1; 1 = new LinkedList(); 1.add(new Tuple('a')); 1.add(new Tuple( 'b')); 1.add(new Tuple('a')); 1.add(new Tuple( 'C')); 1.add(new Tuple( 'b')); 1.add(new Tuple('a')); 1.add(new Tuple('c')); 1.add(new Tuple( 'a')); 1.add(new Tuple('d')); 1.add(new Tuple('d')); 1.add(new Tuple( 'b')); Frequency.frequency (1); Executing the above program produces the following output "a : 4, b: 3,c: 2, d : 2". Below you will find a schematic representation of the list for the execution of the above program. Each set of parentheses contains a character and boolean value. Here t and f are used to represent true and false, respectively. This is the list before the execution of the program. (a,f)->(b,f)->(a,f)->(0,8)->(1,1)>(a,f)->(0,f)->(a,f)->(0,8)->(d,f)->(b,f) The method frequency will first display a : 4. The list will have been transformed as follows. (a,t)->(6,1)>(a,t)->(0,1)>(1,1)>(a,t)->(c,f)->(a,t)->(0,1)>(4,1)>(6,1) Next, the method displays b: 3. The list will have been transformed as follows. (a,t)->(b,t)->(a,t)->(0,8)->(b,t)->(a,t)->(c,f)->(a,t)->(0,8)->(d,f)->(b,t) Next, the method displays c : 2. The list will have been transformed as follows. (a,t)->(b,t)->(a,t)->(c,t)->(b,t)->(a,t)->(c,t)->(a,t)->(d,f)->(d,f)->(b,t) Finally, the method displays d : 2. The list will have been transformed as follows. (a,t)->(b,t)->(a,t)->(c,t)->(b,t)->(a,t)->(c,t)->(a,t)->(d,t)->(d,t)->(b,t) Your implementation must comply with the following directives: You need to use iterators to traverse the list. In fact, the only method of the List that you can use is the method iterator, which returns an iterator on the list. The frequency table should not be saved, just printed. In particular, you cannot use arrays, lists, stacks or queues to store counts. The counts are simply printed. . You will find the source code for the class Tuple and the interface Iterator on page 16. public class Frequency { public static void frequency (List 1) { // End of frequency } // End of Frequency Objects of the class Tuple are used to store a character and a boolean. The value of the boolean is initially false. The method toggle is used to invert the value of visited. public class Tuple { private char c; private boolean visited public Tuple(char c) { this.c = c; visited = false; } public void toggle() { visited = ! visited: } public boolean visited () { return visited: } public char getChar() { return c: } public String toString() { if (visited) { return" (" + c + ", t)"; } else { return" (" + c + ", 1)"; } This question is about the abstract data type List and iterators. The interface List declares a method named iterator, which has a return value of type Iterator. The interface Iterator declares the following methods; public interface Iterator { // Returns true if the iteration has more elements. public abstract boolean hasNext(); // Returns the next element in the iteration. public abstract E next()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!