Answered step by step
Verified Expert Solution
Question
1 Approved Answer
PLEASE WRITE CODE AS SOON AS POSSIBLE! IN JAVA LANGUAGE! In this task you will be working with interfaces. 1. Create an interface called Numbers.
PLEASE WRITE CODE AS SOON AS POSSIBLE! IN JAVA LANGUAGE!
In this task you will be working with interfaces. 1. Create an interface called Numbers. 2. Define a method: private double getConditional Sum(DoublePredicate cond, double arr). a. It will find the sum of those elements of the array that holds the condition (i.e., positive) 3. Define a method: default double getPositive Sum(double arr). a. It will return the sum of only positive elements of the array by using getConditionalSum() method. Note: you may use lambda expressions here. 4. Define a method: default double getNegative Sum(double[arr) which will do a similar job as the previous method, but for negative elements. 5. Define a method: default double getSum(double[] arr) which will do a similar job as the previous two methods, but for ALL elements. 6. Make Question class implement Numbers interface. Test all the default methods in main(). 7. Now that you have practiced default methods, let us get back to abstract ones. 8. Declare a method double getAverage(double arr); in the interface. This will dicate you implementing it in the Question4 class. Do it. Return the average of all the elements. 9. Test getAverage() method in main(). 10. Now, try to instantiate Numbers interface using a lambda expression. You can do it, because it is a functional interface. a. Implement it so that it returns the average of elements different than zero. 11. Test getAverage() method in main(). How output looks: Sum: 11.0 PosSum: 26.0 Possum: -15.0 1.0 How main() looks: double elements[] = new double[] {3, 0,5, -1, 2, 4,-8, 5, -6, 7,0 }; Question4 94 = new Question(); System.out.println("Sum:" +94.getSum(elements); System.out.println("Possum:" +44.getPositive Sum(elements)); System.out.println("NegSum: " + 94.getNegative Sum(elements)); System.out.println(94.getAverage(elements)); Numbers lam = //TODO System.out.println(" Sum:" + lam.getSum(elements)); System.out.println("Possum: " + lam.getPositive Sum(elements)); System.out.println("NegSum:" + lam.getNegative Sum(elements)); Sum: 11.0 Pos Sum: 26.0 PosSum: -15.0 1.2222222222222223 System.out.println(lam.getAverage(elements))Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started