Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

byUnits

Language/Type: Java Collections Sets and Maps
Author: Marty Stepp (on 2021/04/02)

Write a method called byUnits that takes two maps as parameters and that returns a third map. The first parameter will be a map whose keys are course numbers and whose values are the units for that course, as in the following:

        {CHEM237=4, CHEM241=3, CSE142=4, CSE143=5, CSE190=1, DANCE102=3,
         EE215=4, ENGL101=5, ENGL115=2, HIST101=3, MATH124=5, MATH125=5,
         MKTG301=4, PHIL100=5, PHYS121=4, PSYCH101=5}

The second parameter will be a map whose keys are student numbers (a string because of possible leading zeros) and whose values are sets of course numbers that the student with that ID is enrolled in, as in:

        {0615694=[CHEM237, CHEM241, DANCE102, ENGL101], 1009195=[CSE143,
         EE215], 1021012=[MATH124, MKTG301, PSYCH101], 1035859=[CSE143, EE215,
         PHYS121], 1437611=[CSE142, ENGL115, HIST101, MATH125],
         1556919=[CHEM237, CHEM241, CSE142, PHIL100], 2199600=[CSE142, CSE190,
         MATH125, PHYS121], 2381328=[CSE142, CSE190, DANCE102, ENGL101],
         2440382=[CSE142, PSYCH101]}

The method computes the total units each student is taking. For example, the student 1556919 is taking CHEM237, CHEM241, CSE142, and PHIL100. The courses add up to 16 units (4 + 3 + 4 + 5). The map returned by the method should indicate for each number of units, the set of student IDs for students taking that number of units. Thus, there would be an entry for 16 that would include in its set the student ID 1556919. Assuming the two maps above are stored in variables called units and enrollments, respectively, then the call byUnits(units, enrollments) should return the following map:

        {9=[1009195, 2440382], 13=[1035859, 2381328], 14=[1021012, 1437611,
         2199600], 15=[0615694], 16=[1556919]}

As in this example, the keys of the map returned by your method should appear in increasing numerical order. Your method should construct the new map and each of the sets contained in the map and can construct iterators but should otherwise not construct any other structured objects (no string, set, list, etc.). It should also not modify the two parameters and it should be reasonably efficient.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.