Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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[] map(Function function, D[] array) {

R[] result = (R[]) new Object[array.length];

for (int i = 0; i

result[i] = (R) function.apply((R) array[i]);

}

return result;

}

public static void PrintArray(Object[] array) {

for (int i = 0; i

System.out.println(array[i] + " ");

}

System.out.println();

}

public static void main(String[] args) {

Function function = new CalculateTriple();

Double[] doubleArray = { 2.0, 4.0, 6.0, 8.0 };

PrintArray(map(function, doubleArray));

Function function3 = new CalculateSuccessor();

Integer[] integerArray = { 1, 2, 4, 2, 5 };

PrintArray(map(function3, integerArray));

Function function2 = new CalculateLength();

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 static R map(Function function, D] array) This assignment requires you to implement the map function specified; please implement it in a class named edu.psu.cmpsc221.Homework2. The syntax for this generic function is slightly different from the syntax of the other Java generics that we have seen in class (there is an additional pair of type variables that follows the static keyword). This is the syntax Java uses for a static generic method. Please also implement a PrintArray method that takes an array of reference types as a parameter and simply prints the elements of the array to the screen. PrintArray shall be generic. To help clarify what map is doing, a few example uses are provided // Example 1 Function function - new CalculateSuccessorO; Integer integerArray 11, 3, 4, 2, 5J PrintArray (map(function, integerArray)); // map returns 12, 4, 5, 3, 6 // Example 2 Function anotherFunction - new CalculateLengthO; String[] stringArray { "Java", "C++", "Smalltalk" }; PrintArray (map (anotherFunction, stringArray)) // map returns 14, 3, 9 // Example 3 Function tripleFunction-new CalculateTriple(); Double[] doubleArray = { 2.0, 4.0, 5.0, 1.0 }; PrintArray (map (tripleFunction, doubleArray)); // map returns 6.0, 12.0, 15.0, 3.0 Please note that while the solution is actually quite short, this is a challenging problem because Java will likely get in your way and prevent you from implementing "obvious" solutions (recall that Java generics are known to not play nicely with arrays). Lecture has introduced you to everything that you need to solve this problem. While you are always welcome to research any solutions on the Internet (cite your sources!!1!), please note that the submitted solution must respect the signature specified in the assignment My best suggestion to you is to start working on this problem early. Don't wait. No, seriously, get moving! Fire away with any questions that may arise

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning ASP.NET 4.5 Databases

Authors: Sandeep Chanda, Damien Foggon

3rd Edition

1430243805, 978-1430243809

More Books

Students also viewed these Databases questions