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

2. Create a Test class to test the following functionality in its main method. a

ID: 3657884 • Letter: 2

Question

2. Create a Test class to test the following functionality in its main method. a. Declare and initialize an empty ArrayList of Student objects named studentList. b. Declare and initialize an empty HashMap named studentMap. The keys will be the names of the students and the entries in the map will be the corresponding student objects. c. Use the BufferedReader class to read the data.txt file. The contents of the file are shown below. Create the data.txt file in HW5_lastName. d. Read the contents of the text file one line at a time using a loop. The program should work for any number of input lines. In this loop, 1. Invoke the processInputData method for each line read. This method returns the corresponding Student object. 2. Add this Student object to the studentList. 3. Insert this Student object into the studentMap using the student

Explanation / Answer

Test.java ************ import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; public class Test { public static void main(String args[]) { Test test = new Test(); List studentList = new ArrayList(); Map studentMap = new HashMap(); try { BufferedReader bufferedReader = new BufferedReader(new FileReader( "data.txt")); String line = null; Student student = new Student(); while ((line = bufferedReader.readLine()) != null) { student = test.processInputData(line); studentList.add(student); studentMap.put(student.getName(), student); } test.displayList(studentList); test.displayMap(studentMap); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * This method is used to display studentList using iterator * * @param studentList */ public void displayList(List studentList) { System.out.println("Display studentList using iterator"); System.out.println("**********************************"); Iterator iter = studentList.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } System.out.println(" "); } /** * This method is used to display studentMap using iterator * * @param studentMap */ public void displayMap(Map studentMap) { System.out.println("Display studentMap using iterator"); System.out.println("*********************************"); Set keySet = studentMap.keySet(); Iterator iterator = keySet.iterator(); while (iterator.hasNext()) { System.out.println(studentMap.get(iterator.next())); } } public Student processInputData(String line) { Student stud = new Student(); String[] splitString = line.split(","); stud.setName(splitString[0]); String[] marks = new String[6]; for (int k = 1; k
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote