Generics The goal: Use two functional interfaces to implement helpful methods that take and change lists...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Generics The goal: Use two functional interfaces to implement helpful methods that take and change lists of any element type. All classes must follow all good design and coding practices, including good design of classes, code indenting, appropriate comments, and readable and meaningful output. First, create these two functional interfaces: @FunctionalInterface public interface Predicate { boolean test (T t); } @FunctionalInterface public interface Function { T apply (T t); } Now, create a class ListProcessor. It is a helper class that acts as a container for four static methods: destructiveFilter (ArrayList a, Predicate p) nondestructive Filter (ArrayList a, Predicate p) nondestructive FunctionApplyer (ArrayList a, Function f) destructive FunctionApplyer (ArrayList a, Function f) All four of these static methods take an ArrayList of and either a Predicate or a Function , and all four of these methods should return an ArrayList of integers. The nondestructive methods shouldn't modify the list they're given; they should create a new ArrayList with the correct elements and return that. The destructive methods, on the other hand, should actually change the ArrayList they're given, and then return them. The filter methods should apply the Predicate they're given to each element in the list, and whether the Predicate returns true or false determines whether that element will be in the list that's returned. The functionApplyer methods should apply the Function they're given to each element in the list, and put each result at the same index in the list. What your Tester.java main method should do: Create ArrayLists of at least two different type parameters one of which is Integer, fill them, and test each of the four helper methods on them. 1. Test and print the input and output of destructiveFilter and nondestructive Filter on test lists for a particular Predicate. Use a lambda expression for the Predicate! 2. Do the above again, but with a different Predicate. Again, use a lambda expression for the Predicate! 3. Test and print the input and output of destructive FunctionApplyer and nondestructive FunctionApplyer on test lists for a particular Function. Use a lambda expression for the Function! 4. Do the above again, but with a different Function. And again, use a lambda expression for the Function. Example output is shown below: Tester: intlist: [393, 974, 79, 38, 793, 96, 318, 514, 962, 479] intlist non-destructively filtered to remove odds: [974, 38, 96, 318, 514, 962] intlist (should be unchanged): [393, 974, 79, 38, 793, 96, 318, 514, 962, 479] intlist destructively filtered to remove odds... intList: [974, 38, 96, 318, 514, 962] [974, 38, 96, 318, 514, 962] strlist2: [California, Utah, New York, Idaho, Nevada, Wyoming] strlist2 non-destructively filtered to remove elements containing 'a': [New York, Wyoming] strlist2 (should be unchanged): [California, Utah, New York, Idaho, Nevada, Wyoming] strlist destructively filtered to remove elements containing 'a'... strlist2: [New York, Wyoming] intList4: [5, 6, 7, 8, 9] intlist4 non-destructively made all values decremented: [4, 5, 6, 7, 8] intList4 (should be unchanged): [5, 6, 7, 8, 9] intlist4 destructively made all values decremented... intList4: [4, 5, 6, 7, 8] strlist: [cat, dog, house, zebra, Constantinople] strlist non-destructively made all values truncated by one char: [ca, do, hous, zebr, Constantinopl] strlist (should be unchanged): [cat, dog, house, zebra, Constantinople] strlist destructively made all values truncated by one char... strlist: [ca, do, hous, zebr, Constantinopll import java.util.ArrayList; import java.util.ListIterator; public class List Processor { public T a; public T p; public T f; public ListProcessor (T ArrayList, T Predicate, T Function) { ArrayList = a; Predicate = p; Function = f; } public void setArrayList (Tn){ this.a = n; } public void setPredicate (T n) { this.p = n; } public void setFunction (Tn){ this. f = n; } public T getArrayList() { return a; } public T getPredicate() { return p; } public T getFunction () { return f; } public String toString(){ return " < >"; public static ArrayList //Iterator ListIterator iterator = a.listIterator(); while(iterator.hasNext()) } // if item doesn't pass test, remove it) if (!p.test(iterator.next())) iterator.remove(); return a; public static ArrayList nondestructiveFilter(ArrayList a, Predicate p) { ArrayList result = new ArrayList (); //Iterator ListIterator iterator = a.listIterator(); while(iterator.hasNext()) { destructiveFilter(ArrayList a, Predicate p) { T item iterator.next(); // if item pass if (p.test(item)) result.add(item); } return result; public static ArrayList nondestructiveFunctionApplyer (ArrayList a, Function f) { ArrayList result = new ArrayList (); //iterator ListIterator iterator = a.listIterator(); while(iterator.hasNext()){ // add result of function to the result list result.add(f.apply(iterator.next())); } return result; public static ArrayList destructiveFunctionApplyer (ArrayList a, Function f) { // iterator ListIterator iterator = a.listIterator(); while(iterator.hasNext()){ //set the current item to the result of the function iterator.set(f.apply(iterator.next())); } return a; @FunctionalInterface public interface Function { T apply (T nexT); } @FunctionalInterface public interface Predicate { boolean test (T Next); } import java.util.ArrayList; public class Tester { public static void main(String[] args) { System.out.println("Tester: "); //Random list of 10 times ArrayList intlist= new ArrayList (); for (int i = 0; i <10; i++) intlist.add((int) (Math.random()*1000 +1)); System.out.println("inList: " + intlist); System.out.println("inList non-destructively filtered to remoe odds: a->a%2 ==0)); ListProcessor.nondestructiveFilter(intlist, System.out.println("inList: (should be unchanged): " + intlist); System.out.println("inlist destructively filtered to remove odds... "); ListProcessor.destructiveFilter(intlist,a->a%2==0); System.out.println("inlist: " + intlist); System.out.println(". -------"); // strList2 ArrayList strlist = new ArrayList (); for(int i=0; i Tester: inList: [303, 598, 859, 631, 169, 769, 692, 387, 93, 63] inList non-destructively filtered to remoe odds: [598, 692] inList: (should be unchanged): [303, 598, 859, 631, 169, 769, 692, 387, 93, 63] inList destructively filtered to remove odds... inList: [598, 692] strList2: [California, Utah, New York, Idaho, Nevada, Wyoming] strList2 non-destructively filtered to remove elements containing 'a': [New York, Wyoming] strList2 (should be unchanged): [New York, Wyoming] strList destructively filtered to remove elements containig 'a'... strList2: [New York, Wyoming] Generics The goal: Use two functional interfaces to implement helpful methods that take and change lists of any element type. All classes must follow all good design and coding practices, including good design of classes, code indenting, appropriate comments, and readable and meaningful output. First, create these two functional interfaces: @FunctionalInterface public interface Predicate { boolean test (T t); } @FunctionalInterface public interface Function { T apply (T t); } Now, create a class ListProcessor. It is a helper class that acts as a container for four static methods: destructiveFilter (ArrayList a, Predicate p) nondestructive Filter (ArrayList a, Predicate p) nondestructive FunctionApplyer (ArrayList a, Function f) destructive FunctionApplyer (ArrayList a, Function f) All four of these static methods take an ArrayList of and either a Predicate or a Function , and all four of these methods should return an ArrayList of integers. The nondestructive methods shouldn't modify the list they're given; they should create a new ArrayList with the correct elements and return that. The destructive methods, on the other hand, should actually change the ArrayList they're given, and then return them. The filter methods should apply the Predicate they're given to each element in the list, and whether the Predicate returns true or false determines whether that element will be in the list that's returned. The functionApplyer methods should apply the Function they're given to each element in the list, and put each result at the same index in the list. Generics The goal: Use two functional interfaces to implement helpful methods that take and change lists of any element type. All classes must follow all good design and coding practices, including good design of classes, code indenting, appropriate comments, and readable and meaningful output. First, create these two functional interfaces: @FunctionalInterface public interface Predicate { boolean test (T t); } @FunctionalInterface public interface Function { T apply (T t); } Now, create a class ListProcessor. It is a helper class that acts as a container for four static methods: destructiveFilter (ArrayList a, Predicate p) nondestructive Filter (ArrayList a, Predicate p) nondestructive FunctionApplyer (ArrayList a, Function f) destructive FunctionApplyer (ArrayList a, Function f) All four of these static methods take an ArrayList of and either a Predicate or a Function , and all four of these methods should return an ArrayList of integers. The nondestructive methods shouldn't modify the list they're given; they should create a new ArrayList with the correct elements and return that. The destructive methods, on the other hand, should actually change the ArrayList they're given, and then return them. The filter methods should apply the Predicate they're given to each element in the list, and whether the Predicate returns true or false determines whether that element will be in the list that's returned. The functionApplyer methods should apply the Function they're given to each element in the list, and put each result at the same index in the list. What your Tester.java main method should do: Create ArrayLists of at least two different type parameters one of which is Integer, fill them, and test each of the four helper methods on them. 1. Test and print the input and output of destructiveFilter and nondestructive Filter on test lists for a particular Predicate. Use a lambda expression for the Predicate! 2. Do the above again, but with a different Predicate. Again, use a lambda expression for the Predicate! 3. Test and print the input and output of destructive FunctionApplyer and nondestructive FunctionApplyer on test lists for a particular Function. Use a lambda expression for the Function! 4. Do the above again, but with a different Function. And again, use a lambda expression for the Function. What your Tester.java main method should do: Create ArrayLists of at least two different type parameters one of which is Integer, fill them, and test each of the four helper methods on them. 1. Test and print the input and output of destructiveFilter and nondestructive Filter on test lists for a particular Predicate. Use a lambda expression for the Predicate! 2. Do the above again, but with a different Predicate. Again, use a lambda expression for the Predicate! 3. Test and print the input and output of destructive FunctionApplyer and nondestructive FunctionApplyer on test lists for a particular Function. Use a lambda expression for the Function! 4. Do the above again, but with a different Function. And again, use a lambda expression for the Function. Example output is shown below: Tester: intlist: [393, 974, 79, 38, 793, 96, 318, 514, 962, 479] intlist non-destructively filtered to remove odds: [974, 38, 96, 318, 514, 962] intlist (should be unchanged): [393, 974, 79, 38, 793, 96, 318, 514, 962, 479] intlist destructively filtered to remove odds... intList: [974, 38, 96, 318, 514, 962] [974, 38, 96, 318, 514, 962] strlist2: [California, Utah, New York, Idaho, Nevada, Wyoming] strlist2 non-destructively filtered to remove elements containing 'a': [New York, Wyoming] strlist2 (should be unchanged): [California, Utah, New York, Idaho, Nevada, Wyoming] strlist destructively filtered to remove elements containing 'a'... strlist2: [New York, Wyoming] intList4: [5, 6, 7, 8, 9] intlist4 non-destructively made all values decremented: [4, 5, 6, 7, 8] intList4 (should be unchanged): [5, 6, 7, 8, 9] intlist4 destructively made all values decremented... intList4: [4, 5, 6, 7, 8] strlist: [cat, dog, house, zebra, Constantinople] strlist non-destructively made all values truncated by one char: [ca, do, hous, zebr, Constantinopl] strlist (should be unchanged): [cat, dog, house, zebra, Constantinople] strlist destructively made all values truncated by one char... strlist: [ca, do, hous, zebr, Constantinopll Example output is shown below: Tester: intlist: [393, 974, 79, 38, 793, 96, 318, 514, 962, 479] intlist non-destructively filtered to remove odds: [974, 38, 96, 318, 514, 962] intlist (should be unchanged): [393, 974, 79, 38, 793, 96, 318, 514, 962, 479] intlist destructively filtered to remove odds... intList: [974, 38, 96, 318, 514, 962] [974, 38, 96, 318, 514, 962] strlist2: [California, Utah, New York, Idaho, Nevada, Wyoming] strlist2 non-destructively filtered to remove elements containing 'a': [New York, Wyoming] strlist2 (should be unchanged): [California, Utah, New York, Idaho, Nevada, Wyoming] strlist destructively filtered to remove elements containing 'a'... strlist2: [New York, Wyoming] intList4: [5, 6, 7, 8, 9] intlist4 non-destructively made all values decremented: [4, 5, 6, 7, 8] intList4 (should be unchanged): [5, 6, 7, 8, 9] intlist4 destructively made all values decremented... intList4: [4, 5, 6, 7, 8] strlist: [cat, dog, house, zebra, Constantinople] strlist non-destructively made all values truncated by one char: [ca, do, hous, zebr, Constantinopl] strlist (should be unchanged): [cat, dog, house, zebra, Constantinople] strlist destructively made all values truncated by one char... strlist: [ca, do, hous, zebr, Constantinopll import java.util.ArrayList; import java.util.ListIterator; public class List Processor { public T a; public T p; public T f; public ListProcessor (T ArrayList, T Predicate, T Function) { ArrayList = a; Predicate = p; Function = f; } public void setArrayList (Tn){ this.a = n; } public void setPredicate (T n) { this.p = n; } public void setFunction (Tn){ this. f = n; } public T getArrayList() { return a; } public T getPredicate() { return p; } public T getFunction () { return f; } public String toString(){ return " < >"; import java.util.ArrayList; import java.util.ListIterator; public class List Processor { public T a; public T p; public T f; public ListProcessor (T ArrayList, T Predicate, T Function) { ArrayList = a; Predicate = p; Function = f; } public void setArrayList (Tn){ this.a = n; } public void setPredicate (T n) { this.p = n; } public void setFunction (Tn){ this. f = n; } public T getArrayList() { return a; } public T getPredicate() { return p; } public T getFunction () { return f; } public String toString(){ return " < >"; public static ArrayList //Iterator ListIterator iterator = a.listIterator(); while(iterator.hasNext()) } // if item doesn't pass test, remove it) if (!p.test(iterator.next())) iterator.remove(); return a; public static ArrayList nondestructiveFilter(ArrayList a, Predicate p) { ArrayList result = new ArrayList (); //Iterator ListIterator iterator = a.listIterator(); while(iterator.hasNext()) { destructiveFilter(ArrayList a, Predicate p) { T item iterator.next(); // if item pass if (p.test(item)) result.add(item); } return result; public static ArrayList nondestructiveFunctionApplyer (ArrayList a, Function f) { ArrayList result = new ArrayList (); //iterator ListIterator iterator = a.listIterator(); while(iterator.hasNext()){ // add result of function to the result list result.add(f.apply(iterator.next())); } return result; public static ArrayList destructiveFunctionApplyer (ArrayList a, Function f) { // iterator ListIterator iterator = a.listIterator(); while(iterator.hasNext()){ //set the current item to the result of the function iterator.set(f.apply(iterator.next())); } return a; public static ArrayList //Iterator ListIterator iterator = a.listIterator(); while(iterator.hasNext()) } // if item doesn't pass test, remove it) if (!p.test(iterator.next())) iterator.remove(); return a; public static ArrayList nondestructiveFilter(ArrayList a, Predicate p) { ArrayList result = new ArrayList (); //Iterator ListIterator iterator = a.listIterator(); while(iterator.hasNext()) { destructiveFilter(ArrayList a, Predicate p) { T item iterator.next(); // if item pass if (p.test(item)) result.add(item); } return result; public static ArrayList nondestructiveFunctionApplyer (ArrayList a, Function f) { ArrayList result = new ArrayList (); //iterator ListIterator iterator = a.listIterator(); while(iterator.hasNext()){ // add result of function to the result list result.add(f.apply(iterator.next())); } return result; public static ArrayList destructiveFunctionApplyer (ArrayList a, Function f) { // iterator ListIterator iterator = a.listIterator(); while(iterator.hasNext()){ //set the current item to the result of the function iterator.set(f.apply(iterator.next())); } return a; @FunctionalInterface public interface Function { T apply (T nexT); } @FunctionalInterface public interface Predicate { boolean test (T Next); } @FunctionalInterface public interface Function { T apply (T nexT); } @FunctionalInterface public interface Predicate { boolean test (T Next); } import java.util.ArrayList; public class Tester { public static void main(String[] args) { System.out.println("Tester: "); //Random list of 10 times ArrayList intlist= new ArrayList (); for (int i = 0; i <10; i++) intlist.add((int) (Math.random()*1000 +1)); System.out.println("inList: " + intlist); System.out.println("inList non-destructively filtered to remoe odds: a->a%2 ==0)); ListProcessor.nondestructiveFilter(intlist, System.out.println("inList: (should be unchanged): " + intlist); System.out.println("inlist destructively filtered to remove odds... "); ListProcessor.destructiveFilter(intlist,a->a%2==0); System.out.println("inlist: " + intlist); System.out.println(". -------"); // strList2 ArrayList strlist = new ArrayList (); for(int i=0; i import java.util.ArrayList; public class Tester { public static void main(String[] args) { System.out.println("Tester: "); //Random list of 10 times ArrayList intlist= new ArrayList (); for (int i = 0; i <10; i++) intlist.add((int) (Math.random()*1000 +1)); System.out.println("inList: " + intlist); System.out.println("inList non-destructively filtered to remoe odds: a->a%2 ==0)); ListProcessor.nondestructiveFilter(intlist, System.out.println("inList: (should be unchanged): " + intlist); System.out.println("inlist destructively filtered to remove odds... "); ListProcessor.destructiveFilter(intlist,a->a%2==0); System.out.println("inlist: " + intlist); System.out.println(". -------"); // strList2 ArrayList strlist = new ArrayList (); for(int i=0; i Tester: inList: [303, 598, 859, 631, 169, 769, 692, 387, 93, 63] inList non-destructively filtered to remoe odds: [598, 692] inList: (should be unchanged): [303, 598, 859, 631, 169, 769, 692, 387, 93, 63] inList destructively filtered to remove odds... inList: [598, 692] strList2: [California, Utah, New York, Idaho, Nevada, Wyoming] strList2 non-destructively filtered to remove elements containing 'a': [New York, Wyoming] strList2 (should be unchanged): [New York, Wyoming] strList destructively filtered to remove elements containig 'a'... strList2: [New York, Wyoming] Tester: inList: [303, 598, 859, 631, 169, 769, 692, 387, 93, 63] inList non-destructively filtered to remoe odds: [598, 692] inList: (should be unchanged): [303, 598, 859, 631, 169, 769, 692, 387, 93, 63] inList destructively filtered to remove odds... inList: [598, 692] strList2: [California, Utah, New York, Idaho, Nevada, Wyoming] strList2 non-destructively filtered to remove elements containing 'a': [New York, Wyoming] strList2 (should be unchanged): [New York, Wyoming] strList destructively filtered to remove elements containig 'a'... strList2: [New York, Wyoming]
Expert Answer:
Related Book For
Auditing and Assurance services an integrated approach
ISBN: 978-0132575959
14th Edition
Authors: Alvin a. arens, Randal j. elder, Mark s. Beasley
Posted Date:
Students also viewed these programming questions
-
Solve the exercises below according to the instructions provided for each one. Exercise 1 (12 points) Using the normal distribution table, answer: What is the probability that a random variable x is:...
-
How It Worked Out at AdventHealth In the? end, Bob went with Option? #3, \"all hands on? deck.\" He directed his team to use their special expertise and technology to aid AdventHealth in the battle...
-
Let A, B be sets. Define: (a) the Cartesian product (A B) (b) the set of relations R between A and B (c) the identity relation A on the set A [3 marks] Suppose S, T are relations between A and B, and...
-
Return to Better Mousetraps in Problem 18. Suppose the firm can cut its requirements for working capital in half by using better inventory control systems. By how much will this increase project NPV?...
-
Let v and w be elements of an inner product space. Prove that ||v + w||2 = ||v||2 + ||w||2 if and only if v, w are orthogonal. Explain why this formula can be viewed as the generalization of the...
-
Example: It is proposed to construct a self-financing road tunnel. The criterion is that the toll paid by the vehicles over the life of the project must be sufficient to cover both the initial...
-
Which of the following is an authorization control? a. fingerprint reader c. encryption b. compatibility test d. log analysis
-
Frank and Maureen Fantazzi invested $6,000 in a savings account paying 4% annual interest when their daughter, Angela, was born. They also deposited $1,000 on each of her birthdays until she was 18...
-
Why in this case WT/DS546 the Report contains an addendum? What is the Interim review in paragraph 27 of the Addendum? Why in Paragraph 29 of the Addendum, the Panel mention Interim and final Reports...
-
Reba Dixon is a fifth-grade school teacher who earned a salary of $38,000 in 2020. She is 45 years old and has been divorced for four years. She receives $1,200 of alimony payments each month from...
-
Patrick is the owner of a Night club where young people go out at night to party. Drug dealers go to the club where the dealers find many eager customers to buy their illegal and dangerous drugs....
-
Johnson Inc.s non - strategic investment portfolio at December 3 1 , 2 0 1 9 , consisted of the following: Debt and Equity Investments * Cost Fair Value 1 0 , 0 0 0 Xavier Corp. common shares $ 1 6 3...
-
Jaguar Plastics Company has been operating for three years. At December 31 of last year, the accounting records reflected the following: Cash $27,000 Accounts payable $18,000 Investments (short-term)...
-
Many times, performance management systems are linked to compensation. List three reasons why they should be linked and three reasons why they shouldn't be linked. As an employee, which do you...
-
I think the public can contribute to creating performance-based budgets, but how much they can participate depends on the government's structure and rules. Citizen involvement can greatly impact...
-
The following unadjusted trial balance is prepared at fiscal year - end for Nelson Company. Nelson Company uses a perpetual inventory system. It categorizes the following accounts as selling...
-
What is your definition and perception of the public image each of the following organizations seeks to portray: Mercedes Benz. Pizza Pizza. Canadian Tire. Red Cross. Toronto Raptors. Salvation Army....
-
What is master production scheduling and how is it done?
-
List five types of authorizations in the payroll and personnel cycle and state the type of misstatement that is likely to occur when each authorization is lacking.
-
Multiple Choice Questions The following questions deal with management assertions. Choose the best response. a. An auditor reviews aged accounts receivable to assess likelihood of collection to...
-
Lajod Company has an internal audit department consisting of a manager and three staff auditors. The manager of internal audits, in turn, reports to the corporate controller. Copies of audit reports...
-
On April 14, 2006, The Wall Street Journal carried an article headlined Research in Motion Skeptics Pipe Up. The article concerned Research in Motion Ltd. (RIM), the manufacturer of the BlackBerry....
-
Discuss what is meant by the tone at the top and why it is important. Further, discuss whether you feel it is ethical to be a whistle-blower.
-
Consider the following independent situations: 1. An international health club sells lifetime memberships costing \($1,500\) which allow the purchaser unlimited use of any of the clubs 300 facilities...
SAT Math 2 Subject Test Prep SAT Math Level 2 Study Guide 1st Edition - ISBN: 1628454725 - Free Book
Study smarter with the SolutionInn App