CSC526 Final Exam Practice Questions 1. Java Collection a method called recordGr
ID: 3820626 • Letter: C
Question
CSC526 Final Exam Practice Questions 1. Java Collection a method called recordGrade that takes as parameters a map of student grades, a student id, a numerical grade, and a course, and that records the grade in the map. The student id is used as a key for the overall map with each id associated with a map of student grades. The map of student grades uses the course as a key and associates each course with a numerical grade (stored in an object of type Double) For example, suppose that a variable called grades stores a map with information for two students each with grades in two courses: {1212121 fegr221 3.0, engl111 2.5), 4444444 (egr326 3.6, engl131 3.8)) The following calls indicate a new grade for student 4444444 (3.2 in phys121) and a grade for a new student (3.5 in math126 for student 1234567). recordGrade(grades, "4444444", 3.2, "phys121"); recordGrade (grades, "1234567", 3.5, "math126"): After the call, the grades map would store: (1212121 tegr221 3.0, engl 111 2.5), 1234567 {math126 3.5), 4444444 fegr326 3.6, engl 131 3.8, phys1 3.2) Notice that the map now includes grade information for student 1234567 and a third grade for student 4444444. A student may take a course more than once, in which case you should store the highest grade the student has gotten for that course. For example, the ca recordGrade(grades, "1212121", 2.8, "engl111"); would change the map to: (1212121 (egr221 3.0, engl111 2.8), 1234567 {math 126 3.5), 4444444 fegr326 3.6, engl 131 3.8, phys121 3.2)) If the call instead had been with a grade of 2 in engl111, the map would have been unchanged because that grade is lower than the grade currently in the map. The overall map has keys that are ordered by student ID and each student's map of grades should be ordered by the course number. Your method should construct a map for each student not already in the overall map and you may construct iterators, but you are not allowed to construct other structured objects (no string, set, list, etc.)Explanation / Answer
Question 1
----------------
package chegg.collection;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class StudentGradeMgmt {
public static Map<Integer, Student> grades = new LinkedHashMap<Integer, Student>();
public static void main(String[] args) {
StudentGradeMgmt.grades = new HashMap<Integer, Student>();
String[] course = new String[] { "egr221", "egr111" };
double[] gradesArray = new double[] { 3.0, 3.6 };
Student student1 = new Student(123456, course, gradesArray);
String[] course1 = new String[] { "egr131" };
double[] grades1 = new double[] { 3.0 };
Student student3 = new Student(444444, course1, grades1);
StudentGradeMgmt.grades.put(student1.getId(), student1);
StudentGradeMgmt.grades.put(student3.getId(), student3);
System.out.println(StudentGradeMgmt.grades);
StudentGradeMgmt.recordGrade(grades, 444444, 4.0, "phys121");
System.out.println(StudentGradeMgmt.grades);
StudentGradeMgmt.recordGrade(grades, 123456, 4.0, "egr111");
System.out.println(StudentGradeMgmt.grades);
}
public static void recordGrade(Map<Integer, Student> grades, int id,
double grade, String courseName) {
Student student = grades.get(id);
if (student != null) {
if (!StudentGradeMgmt.isCourseContains(student.getCourse(),
courseName)) {
String[] courses = student.getCourse();
String[] newCourses = new String[courses.length + 1];
double[] gradesL = student.getGrade();
double[] newGradeL = new double[student.getGrade().length + 1];
int i = 0;
for (; i < courses.length; i++) {
newCourses[i] = courses[i];
newGradeL[i] = gradesL[i];
}
newCourses[i] = courseName;
newGradeL[i] = grade;
student.setCourse(newCourses);
student.setGrade(newGradeL);
} else {
String[] courses = student.getCourse();
double[] gradesLocal = student.getGrade();
for (int i = 0; i < student.getCourse().length; i++) {
if (courses[i].equals(courseName)) {
if (gradesLocal[i] < grade) {
gradesLocal[i] = grade;
}
}
}
}
} else {
String[] course = new String[] { courseName };
double[] gradesLocal = new double[] { grade };
Student studentLocal = new Student(id, course, gradesLocal);
grades.put(id, studentLocal);
}
}
public static boolean isCourseContains(String[] courses, String course) {
if (courses == null || course == null)
return false;
for (String string : courses) {
if (course.equals(string)) {
return true;
}
}
return false;
}
}
class Student {
private int id;
private String[] course;
private double[] grade;
public Student(int id, String[] course, double[] grade) {
this.id = id;
this.course = course;
this.grade = grade;
}
public void setId(int id) {
this.id = id;
}
public void setCourse(String[] course) {
this.course = course;
}
public void setGrade(double[] grade) {
this.grade = grade;
}
public int getId() {
return id;
}
public String[] getCourse() {
return course;
}
public double[] getGrade() {
return grade;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder("");
if (course != null && grade != null) {
for (int i = 0; i < course.length; i++) {
builder.append("{"+course[i] + "=" + grade[i] + "}");
}
}
return builder.toString();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(course);
result = prime * result + Arrays.hashCode(grade);
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (!Arrays.equals(course, other.course))
return false;
if (!Arrays.equals(grade, other.grade))
return false;
if (id != other.id)
return false;
return true;
}
}
output
-----------
{123456={egr221=3.0}{egr111=3.6}, 444444={egr131=3.0}}
{123456={egr221=3.0}{egr111=3.6}, 444444={egr131=3.0}{phys121=4.0}}
{123456={egr221=3.0}{egr111=4.0}, 444444={egr131=3.0}{phys121=4.0}}
Description
-----------------
Please run the main method of StudentGradeMgmt.java class
Question 2:
--------------------
package chegg.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class AdmissionEntryMain {
public static void main(String[] args) {
List<AdmissionEntry> list = new ArrayList<AdmissionEntry>();
AdmissionEntry entry1 = new AdmissionEntry(2222221);
entry1.rate(3.5);
entry1.rate(3.7);
entry1.rate(4);
entry1.rate(4.5);
entry1.flag();
System.out.println(entry1.toString());
list.add(entry1);
AdmissionEntry entry2 = new AdmissionEntry(2222222);
entry2.rate(2.5);
entry2.rate(2.7);
entry2.rate(3.1);
entry2.rate(2.5);
entry2.flag();
System.out.println(entry2.toString());
list.add(entry2);
AdmissionEntry entry3 = new AdmissionEntry(222223);
entry3.rate(2.5);
entry3.rate(1.7);
entry3.rate(4.2);
entry3.rate(2.1);
entry3.flag();
System.out.println(entry3.toString());
list.add(entry3);
AdmissionEntry entry4 = new AdmissionEntry(2222224);
entry4.rate(2.5);
entry4.rate(2.7);
entry4.rate(3.1);
entry4.rate(2.5);
entry4.flag();
System.out.println(entry4.toString());
list.add(entry4);
AdmissionEntry entry5 = new AdmissionEntry(2222225);
entry5.rate(1.0);
entry5.rate(1.0);
entry5.rate(4.2);
entry5.rate(1.0);
entry5.flag();
System.out.println(entry5.toString());
list.add(entry5);
try {
AdmissionEntry clonabbleAE = entry5.clone();
if(clonabbleAE.equals(entry5)){
System.out.println(clonabbleAE.toString());
}
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Collections.sort(list);
System.out.println(list.toString());
}
}
class AdmissionEntry implements Comparable<AdmissionEntry> , Cloneable {
private int id;
private double[] rating;
private double average = 0.0;
private boolean isToBeDiscussed = false;
public AdmissionEntry(int id) {
super();
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double[] getRating() {
return rating;
}
protected void setRating(double[] rating) {
this.rating = rating;
}
protected double getAverage() {
return average;
}
public void setAverage(double average) {
this.average = average;
}
@Override
public String toString() {
return "[" + id + ": =" + average + "," + this.isToBeDiscussed + "]";
}
public void rate(double rating) {
double[] newRating = null;
if (this.rating != null) {
newRating = new double[this.rating.length + 1];
int i = 0;
for (; i < this.rating.length; i++) {
newRating[i] = this.rating[i];
}
newRating[i] = rating;
this.rating = null;
} else {
newRating = new double[1];
newRating[0] = rating;
}
this.rating = newRating;
}
public void flag() {
if (this.rating != null && this.rating.length > 0) {
for (int i = 0; i < this.rating.length; i++) {
this.average = this.average + this.rating[i];
}
this.average = this.average / this.rating.length;
} else {
this.average = 0.0;
}
if (this.average > 4.0) {
this.setToBeDiscussed(true);
} else {
isToBeDiscussed();
}
}
public boolean isToBeDiscussed() {
for (double d : rating) {
if (d > 4.0) {
this.setToBeDiscussed(true);
}
}
return isToBeDiscussed;
}
public void setToBeDiscussed(boolean isToBeDiscussed) {
this.isToBeDiscussed = isToBeDiscussed;
}
@Override
public int compareTo(AdmissionEntry o) {
int rating = 0;
if (o == null)
rating = -1;
if (this.average == o.average) {
if (this.isToBeDiscussed) {
rating = 0;
} else {
rating = -1;
}
} else if (this.average > o.average) {
if (this.isToBeDiscussed) {
rating = 1;
} else {
rating = 1;
}
} else if (this.average < o.average) {
if (this.isToBeDiscussed) {
rating = -1;
} else {
rating = 1;
}
}
return rating;
}
public AdmissionEntry clone()throws CloneNotSupportedException{
return (AdmissionEntry)super.clone();
}
}
Ouput
------------
[2222221: =3.925,true]
[2222222: =2.7,false]
[222223: =2.625,true]
[2222224: =2.7,false]
[2222225: =1.8,true]
[[2222225: =1.8,true], [222223: =2.625,true], [2222221: =3.925,true], [2222224: =2.7,false], [2222222: =2.7,false]]
Description :
----------------
Please run the main method of AdmissionEntryMain.java class