StudentGrades.java import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.Map; import java.util.Scanner; import java.util.Set; import java.util.TreeMap;
Question:
StudentGrades.java
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class StudentGrades
{
public static void main(String[] args)
{
//-----------Start below here. To do: approximate lines of code = 1
// Create a map called students with key of type String (student id) and value of type Student
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
try
{
File studentData = new File("students.txt");
Scanner in = new Scanner(studentData);
Scanner inputLine;
while (in.hasNextLine())
{
String line = in.nextLine();
inputLine = new Scanner(line);
String name = inputLine.next();
String id = inputLine.next();
Student student = new Student(name,id);
while (inputLine.hasNext())
{
String course = inputLine.next();
String grade = inputLine.next();
student.addCourseAndGrade(course, grade);
}
//-----------Start below here. To do: approximate lines of code = 1
// Add the student to the students map
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
}
catch (IOException exception)
{
System.out.println("Error processing file: " + exception);
System.exit(0);
}
//-----------Start below here. To do: approximate lines of code = 3
// Print all the info for all students in the map
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
//-----------Start below here. To do: approximate lines of code = 8
// Update the course grade of a student with given id
//ID: "DD1234" CPS209 update grade to B+
//ID: "JJ2345" CPS209 update grade to A-
//ID: "HH2123" CPS209 update grade to B+
// Print all the info for all students in the map
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
System.out.println("Expected:Joe DD1234 CPS209 B- CPS109 A-");
System.out.println("Adam HH2123 CPS209 B CPS109 D+");
System.out.println("James JJ2345 CPS209 B+ CPS109 C+");
System.out.println("Miriam MM3456 CPS209 A+ CPS109 A+");
System.out.println("Joe DD1234 CPS209 B+ CPS109 A-");
System.out.println("Adam HH2123 CPS209 B+ CPS109 D+");
System.out.println("James JJ2345 CPS209 A- CPS109 C+");
System.out.println("Miriam MM3456 CPS209 A+ CPS109 A+");
}
}
students.txt
Joe DD1234 CPS209 B- CPS109 A-
James JJ2345 CPS209 B+ CPS109 C+
Adam HH2123 CPS209 B CPS109 D+
Miriam MM3456 CPS209 A+ CPS109 A+
Student.java
import java.util.ArrayList;
/**
A student has a name and a list of courses with grades
*/
public class Student
{
private String name ; // the name of the student
private String id;
private ArrayList courses = new ArrayList();
private ArrayList grades = new ArrayList();
public Student(String name, String id)
{
this.name = name ;
this.id = id;
}
/**
Adds another course and grade to the courses and grades array lists
*/
public void addCourseAndGrade(String course, String letterGrade)
{
courses.add(course);
grades.add(letterGrade);
}
public void updateGrade(String course, String newGrade)
{
for (int i = 0; i < courses.size(); i++)
{
if (courses.get(i).equals(course))
{
grades.set(i,newGrade);
return;
}
}
}
public String getName()
{
return name ;
}
public String getId()
{
return id ;
}
public String toString()
{
String courseAndGrade = "";
for (int i = 0; i < courses.size(); i++)
{
courseAndGrade += courses.get(i) + " " + grades.get(i) + " ";
}
courseAndGrade.trim();
return name + " " + id + " " + courseAndGrade;
}
}
P.S Please make sure your code is formatted well and follow instructions exactly and post output screenshot so we know it is as expected!
Cost Accounting A Managerial Emphasis
ISBN: 978-0136126638
13th Edition
Authors: Charles T. Horngren, Srikant M.Dater, George Foster, Madhav