Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Collections / Reference Semantics - Debugging Download starter code: File Debugging.zip One of the TAs has been programming in Python for too long, and forgot
Collections Reference Semantics Debugging Download starter code: File Debugging.zip One of the TAs has been programming in Python for too long, and forgot how to code in Java! They wrote a solution to the following problem, but accidentally included some bugs: Write a method called deepCopy that takes as a parameter map whose keys are strings and whose values are lists of integers and that creates and returns a new map that is a copy of the map parameter. For example, given a variable called map that stores the following information: cse cse cse The call deepCopy map should return a new map whose structure and content are identical to map. Any later modifications to map or the lists in map following this call should not be reflected in the copy. The map you construct should store keys in alphabetical order. Your method should not modify the contents of the map passed as a parameter. There are bugs in the following program. Find and fix them all! import java.util.; public class Debugging public static void mainString args Map testMap new TreeMap; int arr new int; int arr new int; int arr new int; List c arrToListarr; List c arrToListarr; List c arrToListarr; testMap.putcse c; testMap.putcse c; testMap.putcse c; Map deepCopyMap deepCopytestMap; System.out.print; for String key : deepCopyMap.keySet System.out.printkey deepCopyMap.getkey; System.out.println; public static List arrToListint arr List l new ArrayList; for int num : arr laddnum; return l; Produces and returns a "deep copy" of the parameter map, which has the same structure and values as the parameter, but with all internal data structures and values copied. After calling this method, modifying the parameter or return value should NOT affect the other. Parameters: inputMap the map to duplicate Returns: A deep copy of the parameter map. public static Map deepCopyMap inputMap Map deepCopy new HashMap; for String key : inputMap.keySet List inputList inputMap.getkey; deepCopy.putinputList key; return deepCopy;
Collections
Reference Semantics
Debugging
Download starter code:
File
Debugging.zip
One of the TAs has been programming in Python for too long, and forgot how to code in Java! They wrote a solution to the following problem, but accidentally included some bugs:
Write a method called deepCopy that takes as a parameter map whose keys are strings and whose values are lists of integers and that creates and returns a new map that is a copy of the map parameter. For example, given a variable called map that stores the following information:
cse
cse
cse
The call deepCopy
map
should return a new map whose structure and content are identical to map. Any later modifications to map or the lists in map following this call should not be reflected in the copy. The map you construct should store keys in alphabetical order. Your method should not modify the contents of the map passed as a parameter.
There are
bugs in the following program. Find and fix them all! import java.util.;
public class Debugging
public static void mainString args
Map testMap new TreeMap;
int arr new int;
int arr new int;
int arr new int;
List c arrToListarr;
List c arrToListarr;
List c arrToListarr;
testMap.putcse c;
testMap.putcse c;
testMap.putcse c;
Map deepCopyMap deepCopytestMap;
System.out.print;
for String key : deepCopyMap.keySet
System.out.printkey deepCopyMap.getkey;
System.out.println;
public static List arrToListint arr
List l new ArrayList;
for int num : arr
laddnum;
return l;
Produces and returns a "deep copy" of the parameter map, which has the same
structure and values as the parameter, but with all internal data structures
and values copied. After calling this method, modifying the parameter or
return value should NOT affect the other.
Parameters:
inputMap the map to duplicate
Returns:
A deep copy of the parameter map.
public static Map deepCopyMap inputMap
Map deepCopy new HashMap;
for String key : inputMap.keySet
List inputList inputMap.getkey;
deepCopy.putinputList key;
return deepCopy;
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