Question: 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
1
2
1
"
=
[
4
2
,
1
7
,
4
2
,
4
2
]
,
"cse
1
2
2
"
=
[
1
0
,
1
2
,
1
4
]
,
"cse
1
2
3
"
=
[
1
0
0
,
9
9
,
9
8
,
-
9
7
]
}
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
5
bugs in the following program. Find and fix them all! import java.util.*;
public class Debugging {
public static void main(String[] args){
Map> testMap = new TreeMap<>();
int[] arr1= new int[]{42,17,42,42};
int[] arr2= new int[]{10,12,14};
int[] arr3= new int[]{100,99,98,-97};
List c121= arrToList(arr1);
List c122= arrToList(arr2);
List c123= arrToList(arr3);
testMap.put("cse121", c121);
testMap.put("cse122", c122);
testMap.put("cse123", c123);
Map> deepCopyMap = deepCopy(testMap);
System.out.print("{");
for (String key : deepCopyMap.keySet()){
System.out.print(key +"="+ deepCopyMap.get(key)+"");
}
System.out.println("}");
}
public static List arrToList(int[] arr){
List l = new ArrayList<>();
for (int num : arr){
l.add(num);
}
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> deepCopy(Map> inputMap){
Map> deepCopy = new HashMap<>();
for (String key : inputMap.keySet()){
List inputList = inputMap.get(key);
deepCopy.put(inputList, key);
}
return deepCopy;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!