Question
Anyone know how to go about doing this? My code is below however I have an issue where I get this error: Exception in thread
Anyone know how to go about doing this? My code is below however I have an issue where I get this error:
"Exception in thread "main" java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.Integer; ([Ljava.lang.Object; and [Ljava.lang.Integer; are in module java.base of loader 'bootstrap') at Homework2.main(Homework2.java:53)"
It has to do with the fact that I am trying to cast an Object[] Array to an array of type R in my printArray statement so I need help using generics to get around this.
Code (the highlighted portion is wrong):
package edu.psu.cmpsc221;
import java.util.function.Function;
public class Homework2 {
public static class CalculateLength implements Function
public String apply(String t) {
return t.length()+"";
}
}
public static class CalculateTriple implements Function
public Double apply(Double t) {
return t * 3;
}
}
public static class CalculateSuccessor implements Function
public Integer apply(Integer parameter) {
return parameter + 1;
}
}
@SuppressWarnings("unchecked")
public static
R[] result = (R[]) new Object[array.length];
for (int i = 0; i
result[i] = (R) function.apply((R) array[i]);
}
return result;
}
public static
for (int i = 0; i
System.out.println(array[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
Function
Double[] doubleArray = { 2.0, 4.0, 6.0, 8.0 };
PrintArray(map(function, doubleArray));
Function
Integer[] integerArray = { 1, 2, 4, 2, 5 };
PrintArray(map(function3, integerArray));
Function
String[] stringArray = { "Java", "C++", "Smalltalk" };
PrintArray(map(function2, stringArray));
}
}
While Java does not support higher-order functions, we recently had an introduction to emulating them using the edu.psu. cmpsc221.Function interface. Functional programming languages have a concept called map that takes a function and an array as parameters, and it returns the array produced by ap- plying the function to every element of the original array. Given that our recent introduction to the edu.psu.cmpsc221.Function interface used static members, our map function wil also be static and will have the following signature: public staticStep 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