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 List Processor. 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) destructiveFunctionApplyer (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 ListProcessor.java 1 import java.util.ArrayList; 2 import java.util.ListIterator; 3 4 5 6 7 8 9 LO 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 POHNM 30 31 32 B3 public class ListProcessor { 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(T n){ this.a = n; } public void setPredicate(Tn){ 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 " < 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 50 61 52 53 64 55 56 67 58 69 707172 73 74 75 76 7 78 79 88 77 80 } 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 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; Function.java 1 @FunctionalInterface INM + 2 3 public interface Function { T apply(T nexT); 4 } Predicate.java 1 @FunctionalInterface HNm ti 2 public interface Predicate { boolean test(T Next); 3 4 5 } Tester.java 1 import java.util.ArrayList; 2 public class Tester { Run | Debug public static void main(String[] args) { System.out.println(x: "Tester: "); //Random list of 10 times ArrayList intlist= new ArrayList (); for(int i = 0; i <10; i++) intlist.add((int)(Math.random()*1000 +1)); 3 4 5 6 7 8 9 10 11 12 13 14 15 16 System.out.println("inList: " + intlist); System.out.println("inList non-destructively filtered to remoe odds: " + ListProcessor.nondestructiveFilter(intlist, a->a%2 ==0)); System.out.println("inList: (should be unchanged): + intlist); System.out.println(x: "inList destructively filtered to remove odds... "); ListProcessor.destructiveFilter(intlist,a->a%2==0); System.out.println("inList: + intlist); System.out.println(x: -"); 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 // strList2 ArrayList strlist = new ArrayList (); for(int i=0; i 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 List Processor. 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) destructiveFunctionApplyer (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 List Processor. 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) destructiveFunctionApplyer (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 ListProcessor.java 1 import java.util.ArrayList; 2 import java.util.ListIterator; 3 4 5 6 7 8 9 LO 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 POHNM 30 31 32 B3 public class ListProcessor { 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(T n){ this.a = n; } public void setPredicate(Tn){ 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 " < ListProcessor.java 1 import java.util.ArrayList; 2 import java.util.ListIterator; 3 4 5 6 7 8 9 LO 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 POHNM 30 31 32 B3 public class ListProcessor { 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(T n){ this.a = n; } public void setPredicate(Tn){ 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 " < 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 50 61 52 53 64 55 56 67 58 69 707172 73 74 75 76 7 78 79 88 77 80 } 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 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; 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 50 61 52 53 64 55 56 67 58 69 707172 73 74 75 76 7 78 79 88 77 80 } 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 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; Function.java 1 @FunctionalInterface INM + 2 3 public interface Function { T apply(T nexT); 4 } Predicate.java 1 @FunctionalInterface HNm ti 2 public interface Predicate { boolean test(T Next); 3 4 5 } Function.java 1 @FunctionalInterface INM + 2 3 public interface Function { T apply(T nexT); 4 } Predicate.java 1 @FunctionalInterface HNm ti 2 public interface Predicate { boolean test(T Next); 3 4 5 } Tester.java 1 import java.util.ArrayList; 2 public class Tester { Run | Debug public static void main(String[] args) { System.out.println(x: "Tester: "); //Random list of 10 times ArrayList intlist= new ArrayList (); for(int i = 0; i <10; i++) intlist.add((int)(Math.random()*1000 +1)); 3 4 5 6 7 8 9 10 11 12 13 14 15 16 System.out.println("inList: " + intlist); System.out.println("inList non-destructively filtered to remoe odds: " + ListProcessor.nondestructiveFilter(intlist, a->a%2 ==0)); System.out.println("inList: (should be unchanged): + intlist); System.out.println(x: "inList destructively filtered to remove odds... "); ListProcessor.destructiveFilter(intlist,a->a%2==0); System.out.println("inList: + intlist); System.out.println(x: -"); Tester.java 1 import java.util.ArrayList; 2 public class Tester { Run | Debug public static void main(String[] args) { System.out.println(x: "Tester: "); //Random list of 10 times ArrayList intlist= new ArrayList (); for(int i = 0; i <10; i++) intlist.add((int)(Math.random()*1000 +1)); 3 4 5 6 7 8 9 10 11 12 13 14 15 16 System.out.println("inList: " + intlist); System.out.println("inList non-destructively filtered to remoe odds: " + ListProcessor.nondestructiveFilter(intlist, a->a%2 ==0)); System.out.println("inList: (should be unchanged): + intlist); System.out.println(x: "inList destructively filtered to remove odds... "); ListProcessor.destructiveFilter(intlist,a->a%2==0); System.out.println("inList: + intlist); System.out.println(x: -"); 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 // strList2 ArrayList strlist = new ArrayList (); for(int i=0; i 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 // strList2 ArrayList strlist = new ArrayList (); for(int i=0; i
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
-
can someone solve this Modern workstations typically have memory systems that incorporate two or three levels of caching. Explain why they are designed like this. [4 marks] In order to investigate...
-
The National Health Statistics Reports dated Oct. 22, 2008, included the following information on the heights (in.) for non-Hispanic white females: a. Calculate and interpret a confidence interval at...
-
According to the News Wire "Price Elasticity of Demand," the average cigarette price rose by 12 percent on April 1, 2009. (a) According to the story, by what percentage might smoking be expected to...
-
Find the reference angle and the exact function value if they exist. tan 0
-
A contract is created to refurbish a luxury yacht: new color schemes, new furniture, new wall and floor coverings, new light fixtures, and window treatmentsthe whole works. Of course, it is not just...
-
Baden Corporation entered into a lease agreement for 10 photocopy machines for its corporate headquarters. The lease agreement qualifies as an operating lease in all terms except there is a...
-
Direct labor-hours Machine-hours Total fixed manufacturing overhead cost Variable manufacturing overhead per machine-hour Variable manufacturing overhead per direct labor-hour Required: 1. Compute...
-
Carrie D'Lake, Reed A. Green, and Doug A. Divot share a passion for golf and decide to go into the golf club manufacturing business together. On January 2, 2015, D'Lake, Green, and Divot form the...
-
Rupert is based in EU and works covering the EU market for her HK based employer. She is invited to a meeting with the top 20 shareholders of a biotechnology company to meet with the management team....
-
Suppose that the future price pt of a certain item is given by the following exponential function. In this function, pt is measured in dollars and t is the number of years from today. -pt30001.019t
-
In a residential setup, there is an underground water tank as well as an overhead one. One pump (let us call it to pump C) is installed near the underground tank to transport CDA-supplied water to...
-
A car of mass 1100 kg is claimed to be capable of reaching a speed of 60 mph from rest in 8.5 seconds. Take 60 mph = 96 kph. 1. Assuming that the rate of acceleration of the car is constant...
-
If company A had revenue of $13.3 billion revenue and profit of $2.246 billion, and company B had revenue of $16.6 billion and profit of $118 million, what are some possible future sales projection...
-
2021, Toucan Play This Game had to remit $1,450,000 over a 9 month period. In 2022, Toucan Play This Game had to remit S5, 000, 000 over a 12r monthr peniod. 3 points save Answer A What kind of...
-
(a) Use a k map. to simplify y=DC(ABD+ DB) +ABC +D %3D
-
Explain five different cases of income exempt from tax with clear examples.
-
In auditing the valuation of inventory, the auditor, Claire Butler, decided to use difference estimation. She decided to select an unrestricted random sample of 80 inventory items from a population...
-
Discuss why many CPA firms have willingly settled lawsuits out of court. What are the implications to the profession?
-
Rene Ritter opened a small grocery and related-products convenience store in 1989 with money she had saved working as an A&P store manager. She named it Ritter Dairy and Fruits. Because of the...
-
a. The yield to maturity on two 10-year maturity bonds currently is 7%. Each bond has a call price of $1,100. One bond has a coupon rate of 6%, the other 8%. Assume for simplicity that bonds are...
-
Suppose we add a new variable equal to current liabilities/current assets to Altmans equation. Would you expect this variable to receive a positive or negative coefficient?
-
On May 30, 2020, Janice Kerr is considering one of the newly issued 10-year AAA corporate bonds shown in the following exhibit. a. Suppose that market interest rates decline by 100 basis points...
Study smarter with the SolutionInn App