Answered step by step
Verified Expert Solution
Link Copied!

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
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

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

Database Processing Fundamentals Design

Authors: Marion Donnie Dutton Don F. Seaman

14th Edition Globel Edition

ISBN: 1292107634, 978-1292107639

More Books

Students also viewed these Databases questions

Question

Write 2/11 as an infinite geometric series.

Answered: 1 week ago