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

Need someone to answer the rest of my question that somebody else answered partl

ID: 3917885 • Letter: N

Question

Need someone to answer the rest of my question that somebody else answered partly on here. The code they did is posted below along with the question.

Code that came with the problem:

public class Grade{
    private String letter;
    public Grade(String grade){
        letter=grade;
    }
    public String getLetter(){return letter;}
    public double getValue(){
        switch(letter){
            case "A": return 4;
            case "A-": return 3.667;
            case "B+": return 3.333;
            case "B": return 3;
            case "B-": return 2.667;
            case "C+": return 2.333;
            case "C": return 2;
            case "C-": return 1.667;
            case "F": return 0;
        }
        return 0;
    }
}

public class Student implements Comparable{
    private String firstName;
    private String lastName;
    private int pID;
    public Student(String aFirst, String aLast, int apID){
        firstName = aFirst;
        lastName = aLast;
        pID = apID;
    }
    public int getID(){return pID;}
    public String getFirst(){return firstName;}
    public String getLast(){return lastName;}
      public int compareTo(Object s){
        if (lastName.compareTo(((Student)s).getLast())!=0)
          return lastName.compareTo(((Student)s).getLast());
      else if (firstName.compareTo(((Student)s).getFirst())!=0)
          return firstName.compareTo(((Student)s).getFirst());
      else
        return pID-((Student)s).getID();
      }
}

public class Student1 extends Student{
public static final int HASH_MULTIPLIER=29;
public Student1(String aFirst, String aLast, int apID){
       super(aFirst, aLast,apID);
}
public int hashCode(){
      return (int) Math.pow(HASH_MULTIPLIER,2)*getFirst().hashCode()+HASH_MULTIPLIER*getLast().hashCode()+new Integer(getID()).hashCode();
}
}

public class Student2 extends Student{
public static final int HASH_MULTIPLIER=37;
public Student2(String aFirst, String aLast, int apID){
       super(aFirst, aLast,apID);
}
public int hashCode(){
      return (int) Math.pow(HASH_MULTIPLIER,2)*getFirst().hashCode()+HASH_MULTIPLIER*getLast().hashCode()+new Integer(getID()).hashCode();
}
}

The part that the other person answered:

I have implemented all methods of HashGradeBook and TreeGradeBook except csvExport(String fileName, Grade grade). I have included test program also.

/////////////////////////////////////////////Student.java//////////////////////////////////////////////////////////////////////

package com.java.gradeBookAssignment;

public class Student implements Comparable {

private String firstName;

private String lastName;

private int pID;

public Student(String aFirst, String aLast, int apID) {

firstName = aFirst;

lastName = aLast;

pID = apID;

}

public int getID() {

return pID;

}

public String getFirst() {

return firstName;

}

public String getLast() {

return lastName;

}

@Override

public int compareTo(Object s) {

if (lastName.compareTo(((Student) s).getLast()) != 0)

return lastName.compareTo(((Student) s).getLast());

else if (firstName.compareTo(((Student) s).getFirst()) != 0)

return firstName.compareTo(((Student) s).getFirst());

else

return pID - ((Student) s).getID();

}

}

///////////////////////////////////////End Student.java/////////////////////////////////////

/////////////////////////////////////////////////Grade.java///////////////////////////////////////////

package com.java.gradeBookAssignment;

public class Grade {

private String letter;

public Grade(String grade) {

letter = grade;

}

public String getLetter() {

return letter;

}

public double getValue() {

switch (letter) {

case "A":

return 4;

case "A-":

return 3.667;

case "B+":

return 3.333;

case "B":

return 3;

case "B-":

return 2.667;

case "C+":

return 2.333;

case "C":

return 2;

case "C-":

return 1.667;

case "F":

return 0;

}

return 0;

}

}

////////////////////////////////////End Grade.java////////////////////////////////////////////////

/////////////////////////////////////////GradeBooking.java///////////////////////////////////////////////////

package com.java.gradeBookAssignment;

public interface GradeBooking {

// gets a file name as its only input parameter, reads its content and stores it

// in either HashMap or TreeMap

public void csvImport(String fileName);

// Transfers the students info from the map data structure back to the CSV file

// specified by its input parameter

public void csvExport(String fileName);

// Transfers the students info from the map data structure back to the CSV file

// specified by its input parameter and grade is updated.

public void csvExport(String fileName, Grade grade);

// checks whether the student with the specication given by the input parameters

// currently

// exists in the Map data structure. If it does, the method changes his/her

// grade to the

// one given by the last input parameter; otherwise, it creates a key for the

// student and maps him/her to the grade given by the last input parameter.

void addGrade(String firstName, String lastName, int pID, String grade);

// returns the grade of the student whose information is given by the input

// parameters. If such student

// doesn't exist, it throws an exception.

Grade findGrade(String firstName, String lastName, int pID);

// Student student from HashMap or TreeMap is it already exists. If not do

// nothing

void removeGrade(String firstName, String lastName, int pID);

// find average grade of all the students in Hashmap or treemap

double findAverage();

}

//////////////////////////////////////End GradeBooking.java//////////////////////////////////////////////////

///////////////////////////////////////////HashGradBook.java///////////////////////////////////////////////////////

package com.java.gradeBookAssignment;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.HashMap;

import java.util.Iterator;

public class HashGradeBook implements GradeBooking {

// HashMap that stores STudent and Grade

private HashMap<Student, Grade> gradeHashMap = new HashMap<>();

// gets a file name as its only input parameter, reads its content and stores it

// in a map data structure

@Override

public void csvImport(String fileName) {

BufferedReader br = null;

try {

br = new BufferedReader(new FileReader(fileName));

String line = "";

String demlimiter = ",";

// Read all the lines from the file and create Student and Grade objects and add

// them to the hashMap

while ((line = br.readLine()) != null) {

if (!(line.isEmpty())) {

String str[] = line.split(demlimiter);

String firstName = str[0].toString().replace(""", "").trim();

String lastName = str[1].toString().replace(""", "").trim();

int pid = Integer.parseInt(str[2].toString().replace(""", "").trim());

Student student = new Student(firstName, lastName, pid);

String grade = str[3].toString().replace(""", "").trim();

Grade g = new Grade(grade);

gradeHashMap.put(student, g);

}

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

br.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

@Override

public void csvExport(String fileName) {

BufferedWriter bw = null;

FileWriter fw = null;

try {

String line = " ";

fw = new FileWriter(fileName);

bw = new BufferedWriter(fw);

for (HashMap.Entry<Student, Grade> entry : gradeHashMap.entrySet()) {

Grade gr = entry.getValue();

Student stu = entry.getKey();

line = stu.getFirst() + ", " + stu.getLast() + ", " + stu.getID() + ", " + gr.getLetter() + " ";

bw.write(line);

}

System.out.println("Done writting");

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (bw != null)

bw.close();

if (fw != null)

fw.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

@Override

public void csvExport(String fileName, Grade grade) {

// TODO Auto-generated method stub

}

// checks whether the student with the specication given by the input parameters

// currently

// exists in the Map data structure. If it does, the method changes his/her

// grade to the

// one given by the last input parameter; otherwise, it creates a key for the

// student and maps him/her to the grade given by the last input parameter.

@Override

public void addGrade(String firstName, String lastName, int pID, String grade) {

boolean isFound = false;

Student student = new Student(firstName, lastName, pID);

for (HashMap.Entry<Student, Grade> entry : gradeHashMap.entrySet()) {

Student stu = entry.getKey();

int res = stu.compareTo(student);

if (res == 0) {

entry.setValue(new Grade(grade));

isFound = true;

break;

}

}

if (!isFound) {

gradeHashMap.put(new Student(firstName, lastName, pID), new Grade(grade));

}

}

@Override

// Looks in the hashMap for the student object if found returns the

// corresponding grade otherwise exception is thrown

public Grade findGrade(String firstName, String lastName, int pID) {

Student student = new Student(firstName, lastName, pID);

Grade g = null;

for (HashMap.Entry<Student, Grade> entry : gradeHashMap.entrySet()) {

Student stu = entry.getKey();

int res = stu.compareTo(student);

if (res == 0) {

g = entry.getValue();

return g;

}

}

if (g == null) {

throw new IllegalArgumentException("No student found with the given parameters");

}

return g;

}

// removes the student whose information is given by the input parameters from

// the map data

// structure. If such student doesn't exist, the method does nothing.

@Override

public void removeGrade(String firstName, String lastName, int pID) {

Student student = new Student(firstName, lastName, pID);

Iterator<Student> iterator = gradeHashMap.keySet().iterator();

while (iterator.hasNext()) {

Student stu = iterator.next();

int res = student.compareTo(stu);

if (res == 0) {

iterator.remove();

System.out.println("Successfully removed object");

}

}

}

// calculates and returns the average of all grades stored in the

// map data structure. The method throws an exception if the map is empty.

@Override

public double findAverage() {

double result = 0.0;

if (gradeHashMap.size() == 0) {

throw new IllegalStateException("HashMap empty");

}

for (HashMap.Entry<Student, Grade> entry : gradeHashMap.entrySet()) {

Grade gr = entry.getValue();

result += gr.getValue();

}

return result;

}

}

//////////////////////////////////////////////End HashGradeBook.java///////////////////////////////////////////////

////////////////////////////////////////////////////TreeGradeBook.java////////////////////////////////////////////////

package com.java.gradeBookAssignment;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Iterator;

import java.util.Map.Entry;

import java.util.TreeMap;

public class TreeGradeBook implements GradeBooking {

private TreeMap<Student, Grade> gradeTreeMap = new TreeMap<>();

@Override

public void csvImport(String fileName) {

BufferedReader br = null;

try {

br = new BufferedReader(new FileReader(fileName));

String line = "";

String demlimiter = ",";

// Read all the lines from the file and create Student and Grade objects and add

// them to the hashMap

while ((line = br.readLine()) != null) {

if (!(line.isEmpty())) {

String str[] = line.split(demlimiter);

String firstName = str[0].toString().replace(""", "").trim();

String lastName = str[1].toString().replace(""", "").trim();

int pid = Integer.parseInt(str[2].toString().replace(""", "").trim());

Student student = new Student(firstName, lastName, pid);

String grade = str[3].toString().replace(""", "").trim();

Grade g = new Grade(grade);

gradeTreeMap.put(student, g);

}

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

br.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

@Override

public void csvExport(String fileName) {

BufferedWriter bw = null;

FileWriter fw = null;

try {

String line = " ";

fw = new FileWriter(fileName);

bw = new BufferedWriter(fw);

for (Entry<Student, Grade> entry : gradeTreeMap.entrySet()) {

Student stu = entry.getKey();

Grade gr = entry.getValue();

line = stu.getFirst() + ", " + stu.getLast() + ", " + stu.getID() + ", " + gr.getLetter() + " ";

bw.write(line);

}

System.out.println("Done writting");

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (bw != null)

bw.close();

if (fw != null)

fw.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

@Override

public void csvExport(String fileName, Grade grade) {

// TODO Auto-generated method stub

}

@Override

public void addGrade(String firstName, String lastName, int pID, String grade) {

boolean isFound = false;

Student student = new Student(firstName, lastName, pID);

// Create a Iterator to KeySet of HashMap

for (Entry<Student, Grade> entry : gradeTreeMap.entrySet()) {

Student stu = entry.getKey();

// Check if student already exists of not.

int res = stu.compareTo(student);

if (res == 0) {

// if exists then update grade

entry.setValue(new Grade(grade));

isFound = true;

break;

}

}

if (!isFound) {

// if student key not found then adde new value to map

gradeTreeMap.put(new Student(firstName, lastName, pID), new Grade(grade));

}

}

@Override

public Grade findGrade(String firstName, String lastName, int pID) {

Student student = new Student(firstName, lastName, pID);

Grade g = null;

for (Entry<Student, Grade> entry : gradeTreeMap.entrySet()) {

Student key = entry.getKey();

int res = key.compareTo(student);

if (res == 0) {

g = entry.getValue();

return g;

}

}

if (g == null) {

throw new IllegalArgumentException("No student found with the given parameters");

}

return null;

}

@Override

public void removeGrade(String firstName, String lastName, int pID) {

Student student = new Student(firstName, lastName, pID);

// Create a Iterator to KeySet of HashMap

Iterator<Student> it = gradeTreeMap.keySet().iterator();

// Iterate over all the elements

while (it.hasNext()) {

Student stu = it.next();

// Check if Value associated with Key is ODD

int res = stu.compareTo(student);

if (res == 0) {

// Remove the element

it.remove();

}

}

}

@Override

public double findAverage() {

double result = 0.0;

if (gradeTreeMap.size() == 0) {

throw new IllegalStateException("TreeMap empty");

}

for (Entry<Student, Grade> entry : gradeTreeMap.entrySet()) {

Grade gr = entry.getValue();

result += gr.getValue();

}

return result;

}

}

////////////////////////////////////////////////////End TreeGradeBook.java////////////////////////////////////////////////

////////////////////////////////////////////Driver.java//////////////////////////////////////////////////

package com.java.gradeBookAssignment;

//Program to test both of the classes

public class Driver {

public static void main(String args[]) {

HashGradeBook book1 = new HashGradeBook();

//TODO: Change file Location

book1.csvImport(

"src\com\java\gradeBookAssignment\input.csv");

Grade gr = book1.findGrade("Joe", "Miles", 98765);

System.out.println("STudent found with grade= " + gr.getLetter());

book1.addGrade("Adam", "Eve", 22222, "A+");

gr = book1.findGrade("Adam", "Eve", 22222);

System.out.println("New Student should be added afer this found with grade= " + gr.getLetter());

book1.addGrade("Adam", "Eve", 22222, "B+");

gr = book1.findGrade("Adam", "Eve", 22222);

System.out.println("Same object but with different grade now= " + gr.getLetter());

book1.removeGrade("Adam", "Eve", 22222);

try {

gr = book1.findGrade("Adam", "Eve", 22222);

System.out.println("Expected exception.");

} catch (IllegalArgumentException ex) {

System.out.println("Expected exception. continue");

}

book1.addGrade("Adam", "Eve", 22222, "A+");

gr = book1.findGrade("Adam", "Eve", 22222);

System.out.println("Added again Student should be added afer this found with grade= " + gr.getLetter());

double avg = book1.findAverage();

System.out.println("Average garde = " + avg);

//TODO: Change file Location

book1.csvExport(

"src\com\java\gradeBookAssignment\output.csv");

TreeGradeBook book2 = new TreeGradeBook();

//TODO: Change file Location

book2.csvImport(

"src\com\java\gradeBookAssignment\input.csv");

gr = book2.findGrade("Joe", "Miles", 98765);

System.out.println("Student found with grade= " + gr.getLetter());

avg = book2.findAverage();

System.out.println("TreeMap average grade= " + avg);

book2.addGrade("Adam", "Eve", 22222, "A+");

gr = book2.findGrade("Adam", "Eve", 22222);

System.out.println("New Student should be added afer this found with grade= " + gr.getLetter());

book2.removeGrade("Adam", "Eve", 22222);

try {

gr = book2.findGrade("Adam", "Eve", 22222);

System.out.println("Expected exception.");

} catch (IllegalArgumentException ex) {

System.out.println("Expected exception. continue");

}

//TODO: Change file Location

book2.csvExport(

"src\com\java\gradeBookAssignment\output.csv");

}

}

////////////////////////////////////////////ENd Driver.java/////////////////////////////////////////

Explanation / Answer

/*
Note that the only changes made are in the TreeGradeBook.java and the HashGradeBook.java files, which have been provided below.
Rest of the files remain the same as in the previous answer.
Please ensure the "input.csv" file is correctly filled with the records (formatted as shown in the questions's screenshot), and is at the correct location (change the file path in the Driver.java if needed) for the program to work
*/

////////////////////////////////////////////TreeGradeBook.java/////////////////////////////////////////
package com.java.gradeBookAssignment;
import java.io.BufferedReader;
import java.io.*;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.TreeMap;

public class TreeGradeBook implements GradeBooking {
private TreeMap<Student, Grade> gradeTreeMap = new TreeMap<>();
@Override
public void csvImport(String fileName) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(fileName));
String line = "";
String demlimiter = ",";
// Read all the lines from the file and create Student and Grade objects and add
// them to the hashMap
while ((line = br.readLine()) != null) {
if (!(line.isEmpty())) {
String str[] = line.split(demlimiter);
String firstName = str[0].toString().replace(""", "").trim();
String lastName = str[1].toString().replace(""", "").trim();
int pid = Integer.parseInt(str[2].toString().replace(""", "").trim());
Student student = new Student(firstName, lastName, pid);
String grade = str[3].toString().replace(""", "").trim();
Grade g = new Grade(grade);
gradeTreeMap.put(student, g);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void csvExport(String fileName) {
BufferedWriter bw = null;
FileWriter fw = null;
try {
String line = " ";
fw = new FileWriter(fileName);
bw = new BufferedWriter(fw);
for (Entry<Student, Grade> entry : gradeTreeMap.entrySet()) {
Student stu = entry.getKey();
Grade gr = entry.getValue();
line = stu.getFirst() + ", " + stu.getLast() + ", " + stu.getID() + ", " + gr.getLetter() + " ";
bw.write(line);
}
System.out.println("Done writting");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
@Override
public void csvExport(String fileName, Grade grade) {
BufferedWriter bw = null;
FileWriter fw = null;
try {
String line = " ";
fw = new FileWriter(fileName);
bw = new BufferedWriter(fw);
for (Entry<Student, Grade> entry : gradeTreeMap.entrySet()) {
Student stu = entry.getKey();
Grade gr = entry.getValue();
if(gr.getLetter().equals((grade.getLetter()))==false)
continue;
line = stu.getFirst() + ", " + stu.getLast() + ", " + stu.getID() + ", " + gr.getLetter() + " ";
bw.write(line);
}
System.out.println("Done writting");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
@Override
public void addGrade(String firstName, String lastName, int pID, String grade) {
boolean isFound = false;
Student student = new Student(firstName, lastName, pID);
// Create a Iterator to KeySet of HashMap
for (Entry<Student, Grade> entry : gradeTreeMap.entrySet()) {
Student stu = entry.getKey();
// Check if student already exists of not.
int res = stu.compareTo(student);
if (res == 0) {
// if exists then update grade
entry.setValue(new Grade(grade));
isFound = true;
break;
}
}
if (!isFound) {
// if student key not found then adde new value to map
gradeTreeMap.put(new Student(firstName, lastName, pID), new Grade(grade));
}
}
@Override
public Grade findGrade(String firstName, String lastName, int pID) {
Student student = new Student(firstName, lastName, pID);
Grade g = null;
for (Entry<Student, Grade> entry : gradeTreeMap.entrySet()) {
Student key = entry.getKey();
int res = key.compareTo(student);
if (res == 0) {
g = entry.getValue();
return g;
}
}
if (g == null) {
throw new IllegalArgumentException("No student found with the given parameters");
}
return null;
}
@Override
public void removeGrade(String firstName, String lastName, int pID) {
Student student = new Student(firstName, lastName, pID);
// Create a Iterator to KeySet of HashMap
Iterator<Student> it = gradeTreeMap.keySet().iterator();
// Iterate over all the elements
while (it.hasNext()) {
Student stu = it.next();
// Check if Value associated with Key is ODD
int res = stu.compareTo(student);
if (res == 0) {
// Remove the element
it.remove();
}
}
}
@Override
public double findAverage() {
double result = 0.0;
if (gradeTreeMap.size() == 0) {
throw new IllegalStateException("TreeMap empty");
}
for (Entry<Student, Grade> entry : gradeTreeMap.entrySet()) {
Grade gr = entry.getValue();
result += gr.getValue();
}
return result;
}
}
////////////////////////////////////////////End TreeGradeBook.java/////////////////////////////////////////
////////////////////////////////////////////HashGradeBook.java/////////////////////////////////////////
package com.java.gradeBookAssignment;
//package com.java.gradeBookAssignment;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
public class HashGradeBook implements GradeBooking {
// HashMap that stores STudent and Grade
private HashMap<Student, Grade> gradeHashMap = new HashMap<>();
// gets a file name as its only input parameter, reads its content and stores it
// in a map data structure
@Override
public void csvImport(String fileName) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(fileName));
String line = "";
String demlimiter = ",";
// Read all the lines from the file and create Student and Grade objects and add
// them to the hashMap
while ((line = br.readLine()) != null) {
if (!(line.isEmpty())) {
String str[] = line.split(demlimiter);
String firstName = str[0].toString().replace(""", "").trim();
String lastName = str[1].toString().replace(""", "").trim();
int pid = Integer.parseInt(str[2].toString().replace(""", "").trim());
Student student = new Student(firstName, lastName, pid);
String grade = str[3].toString().replace(""", "").trim();
Grade g = new Grade(grade);
gradeHashMap.put(student, g);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void csvExport(String fileName) {
BufferedWriter bw = null;
FileWriter fw = null;
try {
String line = " ";
fw = new FileWriter(fileName);
bw = new BufferedWriter(fw);
for (HashMap.Entry<Student, Grade> entry : gradeHashMap.entrySet()) {
Grade gr = entry.getValue();
Student stu = entry.getKey();
line = stu.getFirst() + ", " + stu.getLast() + ", " + stu.getID() + ", " + gr.getLetter() + " ";
bw.write(line);
}
System.out.println("Done writting");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
@Override
public void csvExport(String fileName, Grade grade) {
BufferedWriter bw = null;
FileWriter fw = null;
try {
String line = " ";
fw = new FileWriter(fileName);
bw = new BufferedWriter(fw);
for (HashMap.Entry<Student, Grade> entry : gradeHashMap.entrySet()) {
Grade gr = entry.getValue();
if(gr.getLetter().equals(grade.getLetter())==false)
continue;
Student stu = entry.getKey();
line = stu.getFirst() + ", " + stu.getLast() + ", " + stu.getID() + ", " + gr.getLetter() + " ";
bw.write(line);
}
System.out.println("Done writting");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
// checks whether the student with the specication given by the input parameters
// currently
// exists in the Map data structure. If it does, the method changes his/her
// grade to the
// one given by the last input parameter; otherwise, it creates a key for the
// student and maps him/her to the grade given by the last input parameter.
@Override
public void addGrade(String firstName, String lastName, int pID, String grade) {
boolean isFound = false;
Student student = new Student(firstName, lastName, pID);
for (HashMap.Entry<Student, Grade> entry : gradeHashMap.entrySet()) {
Student stu = entry.getKey();
int res = stu.compareTo(student);
if (res == 0) {
entry.setValue(new Grade(grade));
isFound = true;
break;
}
}
if (!isFound) {
gradeHashMap.put(new Student(firstName, lastName, pID), new Grade(grade));
}
}
@Override
// Looks in the hashMap for the student object if found returns the
// corresponding grade otherwise exception is thrown
public Grade findGrade(String firstName, String lastName, int pID) {
Student student = new Student(firstName, lastName, pID);
Grade g = null;
for (HashMap.Entry<Student, Grade> entry : gradeHashMap.entrySet()) {
Student stu = entry.getKey();
int res = stu.compareTo(student);
if (res == 0) {
g = entry.getValue();
return g;
}
}
if (g == null) {
throw new IllegalArgumentException("No student found with the given parameters");
}
return g;
}
// removes the student whose information is given by the input parameters from
// the map data
// structure. If such student doesn't exist, the method does nothing.
@Override
public void removeGrade(String firstName, String lastName, int pID) {
Student student = new Student(firstName, lastName, pID);
Iterator<Student> iterator = gradeHashMap.keySet().iterator();
while (iterator.hasNext()) {
Student stu = iterator.next();
int res = student.compareTo(stu);
if (res == 0) {
iterator.remove();
System.out.println("Successfully removed object");
}
}
}
// calculates and returns the average of all grades stored in the
// map data structure. The method throws an exception if the map is empty.
@Override
public double findAverage() {
double result = 0.0;
if (gradeHashMap.size() == 0) {
throw new IllegalStateException("HashMap empty");
}
for (HashMap.Entry<Student, Grade> entry : gradeHashMap.entrySet()) {
Grade gr = entry.getValue();
result += gr.getValue();
}
return result;
}
}
////////////////////////////////////////////End HashGradeBook.java/////////////////////////////////////////

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