Hi All , I have a java lab i need to do and I need help with this. I need to imp
ID: 3876313 • Letter: H
Question
Hi All , I have a java lab i need to do and I need help with this. I need to implement a method to Build a list of the names of students currently enrolled in a number of units strictly greater than the unitThreshold....
________________________________________________________
}
_________________________________________________
/* * This file should remain unchanged. */ class Course { private final String name; private final int numUnits; public Course(final String name, final int numUnits) { this.name = name; this.numUnits = numUnits; } public String getName() { return name; } public int getNumUnits() { return numUnits; }}
_________________________________________________
import java.util.LinkedList; import java.util.List; import java.util.Map; class ExampleMap { public static List<String> highEnrollmentStudents( Map<String, List<Course>> courseListsByStudentName, int unitThreshold) { List<String> overEnrolledStudents = new LinkedList<>(); /* Build a list of the names of students currently enrolled in a number of units strictly greater than the unitThreshold. */ return overEnrolledStudents; } }Explanation / Answer
The code is given below
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
class Course
{
private final String name;
private final int numUnits;
public Course(final String name, final int numUnits)
{
this.name = name;
this.numUnits = numUnits;
}
public String getName()
{
return name;
}
public int getNumUnits()
{
return numUnits;
}
}
class ExampleMap
{
public static List<String> highEnrollmentStudents(
Map<String, List<Course>> courseListsByStudentName, int unitThreshold)
{
List<String> overEnrolledStudents = new LinkedList<>();
List<String> s=new LinkedList<String>(courseListsByStudentName.keySet());
Iterator<String> i=s.iterator();
while(i.hasNext())
{
int units=0;
String name=(String)i.next();
List<Course> l=courseListsByStudentName.get(name);
Iterator<Course> listItr=l.iterator();
while(listItr.hasNext())
{
units+=listItr.next().getNumUnits();
}
if(units>unitThreshold)
overEnrolledStudents.add(name);
}
return overEnrolledStudents;
}
}