Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*** Write a Java programm*** This assignment will have you working with Collecti

ID: 3722293 • Letter: #

Question

*** Write a Java programm*** This assignment will have you working with Collections and reading in another text file. This time you will identify the people form the text file that fit in the following criteria: Those who took CS210, those who took CS211, those who took CS210 or CS211, those who took CS210 and CS211, those who took CS210 but not CS211, and finally those who took CS210 but not CS210. The text file consists of a list of students, theirs names, and the classes they took.

---------------student.txt-----------

cs211 John
cs211 William
cs211 Susan
cs211 Jack
cs211 Jennifer
cs211 Sophia
cs211 Emma
cs211 Olivia
cs211 Eve
cs211 Tom
cs211 Lily
cs210 John
cs210 William
cs210 Susan
cs210 Jack
cs210 Jennifer
cs210 Sophia
cs210 Emma
cs210 Olivia
cs210 Isabella
cs210 Zoe

-------------------------------------------------------------------------------------------

Output lists to screen.

those who took CS210

those who took CS211,

those who took CS210 or CS211,

those who took CS210 and CS211,

those who took CS210 but not CS211,

those who took CS211 but not CS210.

Explanation / Answer

Explanation:

Please find the below StudentCourse class which is reading student.txt input file and displaying the information as required, Please see the outputs.

Please revert back in case anything needs to be changed.

Program:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class StudentCourse {
public static void main(String[] args) {
HashMap<String, ArrayList<String>> studentCouraseMap = readInputFile("student.txt");
processStudentCourseMap(studentCouraseMap);
}
public static void processStudentCourseMap(HashMap<String, ArrayList<String>> studentCouraseMap) {
ArrayList<String> studentListCS211 = new ArrayList<String>();
ArrayList<String> studentListCS210 = new ArrayList<String>();
for ( String course : studentCouraseMap.keySet() ) {
if (course.equalsIgnoreCase("cs211")) {
studentListCS211 = studentCouraseMap.get(course);
} else {
studentListCS210 = studentCouraseMap.get(course);
}
}
displayOnlyForCS210(studentListCS210);
displayOnlyForCS211(studentListCS211);
displayForCS2111OrCS211(studentListCS211, studentListCS210);
displayForCS2111AndCS211(studentListCS211, studentListCS210);
displayInformationOnlyForCS210(studentListCS211, studentListCS210);
displayInformationOnlyForCS211(studentListCS211, studentListCS210);
}
public static void displayOnlyForCS210(ArrayList<String> studentListCS210) {
System.out.println("Students who took CS210:");
for (int i=0;i<studentListCS210.size();i++) {
System.out.println(studentListCS210.get(i));
}
}
public static void displayOnlyForCS211(ArrayList<String> studentListCS211) {
System.out.println(" Students who took CS211:");
for (int i=0;i<studentListCS211.size();i++) {
System.out.println(studentListCS211.get(i));
}
}
public static void displayForCS2111OrCS211(ArrayList<String> studentListCS211,ArrayList<String> studentListCS210) {
Set<String> set = new TreeSet<String>();
set.addAll(studentListCS210);
set.addAll(studentListCS211);
ArrayList<String> studentListEitherCourse = new ArrayList<String>(set);
System.out.println(" Students who took CS210 or CS211:");
for (int i=0;i<studentListEitherCourse.size();i++) {
System.out.println(studentListEitherCourse.get(i));
}
}
public static void displayForCS2111AndCS211(ArrayList<String> studentListCS211,ArrayList<String> studentListCS210) {
System.out.println(" Students who took CS210 and CS211:");
for (int i=0;i<studentListCS210.size();i++) {
if (studentListCS211.contains(studentListCS210.get(i))) {
System.out.println(studentListCS210.get(i));
}
}
}
public static void displayInformationOnlyForCS210(ArrayList<String> studentListCS211,ArrayList<String> studentListCS210) {
System.out.println(" Students who took CS210 but not CS211:");
for (int i=0;i<studentListCS210.size();i++) {
if (!studentListCS211.contains(studentListCS210.get(i))) {
System.out.println(studentListCS210.get(i));
}
}
}
public static void displayInformationOnlyForCS211(ArrayList<String> studentListCS211,ArrayList<String> studentListCS210) {
System.out.println(" Students who took CS211 but not CS210:");
for (int i=0;i<studentListCS211.size();i++) {
if (!studentListCS210.contains(studentListCS211.get(i))) {
System.out.println(studentListCS211.get(i));
}
}
}
public static HashMap<String, ArrayList<String>> readInputFile(String inputFileName) {
Scanner sacnnerReader = null;
try {
File file = new File(inputFileName);
if (file.exists()) {
HashMap<String, ArrayList<String>> studentCouraseMap = new HashMap<String, ArrayList<String>>();
sacnnerReader = new Scanner(file);
while (sacnnerReader.hasNextLine()) {
String line = sacnnerReader.nextLine();
String lineArry[] = line.split(" ");
String course = lineArry[0];
String name = lineArry[1];
if (studentCouraseMap.get(course) != null) {
ArrayList<String> studentList = studentCouraseMap.get(course);
studentList.add(name);
studentCouraseMap.put(course, studentList);
} else {
ArrayList<String> studentList = new ArrayList<String>();
studentList.add(name);
studentCouraseMap.put(course, studentList);
}
}
return studentCouraseMap;
} else {
System.out.println("Input File not found");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sacnnerReader != null) {
try {
sacnnerReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return null;
}
}

Output:

Students who took CS210:
John
William
Susan
Jack
Jennifer
Sophia
Emma
Olivia
Isabella
Zoe

Students who took CS211:
John
William
Susan
Jack
Jennifer
Sophia
Emma
Olivia
Eve
Tom
Lily

Students who took CS210 or CS211:
Emma
Eve
Isabella
Jack
Jennifer
John
Lily
Olivia
Sophia
Susan
Tom
William
Zoe

Students who took CS210 and CS211:
John
William
Susan
Jack
Jennifer
Sophia
Emma
Olivia

Students who took CS210 but not CS211:
Isabella
Zoe

Students who took CS211 but not CS210:
Eve
Tom
Lily