Commit e9aedc51 authored by michael lundquist's avatar michael lundquist
Browse files

Adding comments

parent f8bbcbcd
Pipeline #4211 passed with stage
in 1 minute and 20 seconds
......@@ -2,6 +2,10 @@ package com.ds.labfour;
import java.util.*;
/**
* This code was taken from IT306, spring 2019
*
*/
public class MyBucket {
private Set<MyEntry> elements;
private int key;
......
package com.ds.labfour;
/**
* This code was taken from IT306, spring 2019.
*/
public class MyEntry implements Comparable{
private String key;
private String value;
......
......@@ -2,6 +2,14 @@ package com.ds.labfour;
import java.util.*;
/**
* The author only made small changes,
* the majority of the code was taken from IT 306, spring 2019
* This class is an implementation of HashMap given to us for learning purposes.
*
* @author <a href="mailto:mlundqu2@masonlive.gmu.edu">Michael Lundquist</a>
* Gnumber: 00737340
*/
public class MyHashmap implements Map, Cloneable{
......@@ -109,7 +117,11 @@ public class MyHashmap implements Map, Cloneable{
}
/**
* I changed this to return MyEntry that's what MyBucket.getElement returns
* This gets the value corresponding to the given key
*
* @param key The key who's value you want to get.
* @return The MyEntry object from the bucket
* @see MyBucket#getElement(String)
*/
public MyEntry get(Object key) {
MyBucket b = index[calcPosition((String)key)];
......
......@@ -4,11 +4,16 @@ import java.util.Arrays;
import java.util.HashSet;
/**
* This is the Student class taken from IT306, spring 2019, with the following modifications:
*
* To elaborate on lab 4, you need to apply the modifications to "Student.java" class, including revising the class fields and changing the implementation of "getGrade" method, setCourse method, and Student constructor so as to improve the complexity from linear to constant time.
* In the for-loop, if any of the elements in the COURSE_LIST array match the "String course", then you use the index to return the corresponding grade. However, you should change the structure to a hashmap instead so as to reduce the time complexity to O(1). For instance, inside the constructor, rather than having arrays, you need to initiaize a hashmap for recording student courses.
* You should also create another map to represent your COURSE-LIST structure that represents the list of permissible courses for the students to take.
* In the set course method, once use wants to enter a course, first, the code should check if the input course is permissible (in O(1) time) and if it is, then it should add it to the map structure created to hold the student's current course list (e.g., hashmap courses).
*
*
* @author <a href="mailto:mlundqu2@masonlive.gmu.edu">Michael Lundquist</a>
* Gnumber: 00737340
*/
public class Student {
public final static int MAX = 100;
......@@ -19,26 +24,26 @@ public class Student {
// This needs to be private because you can alter a final hash set's values. The only final thing is the pointer to the set.
public final static String LIST= "IT106,IT206,IT306,IT212,IT213,IT214,COMM100,MATH112,IT293";
//This hashmap allows searching of the courses for the student's grade in O(1)
private MyHashmap courses = new MyHashmap(COURSE_LIST.length);
private String name;
private String address;
private String major;
/*TODO
private double[] courseGrades;
private String[] courses;
*/
private MyHashmap courses;
private int id;
private int numCourses;
private static int totalStudent;
/**
* This is a constructor for student.
*
* The only change I made to this is removing the parallel arrays.
*/
public Student(){
/*
courseGrades = new double[MAX];
courses = new String[MAX];
*/
courses = new MyHashmap(COURSE_LIST.length);
this.numCourses = 0;
totalStudent++;
this.id = totalStudent;
......@@ -47,19 +52,16 @@ public class Student {
public void setAddress(String address) { this.address = address; }
public void setMajor(String major) {this.major = major; }
/**
* Set course validates course and adds it to this student's courses.
*
* @return false if input is invalid (under these conditions), else true
- The course doesn't exist
- The student is already taking too many classes
*/
public boolean setCourse(double grade, String course){
if(numCourses >= MAX) return false;
if(!COURSE_SET.contains(course)) return false;
/*TODO
for(int i=0; i<COURSE_LIST.length; i++){
if(COURSE_LIST[i].equalsIgnoreCase(course)){
courseGrades[numCourses]=grade;
courses[numCourses++]=course;
return true;
}
}
*/
this.courses.put(course, grade);
numCourses++;
return true;
......@@ -72,15 +74,18 @@ public class Student {
public int getNumCourses(){ return numCourses;}
public int getId(){ return id;}
/**
* This finds the grade the student got in this course.
*
* @param course the name of the course to get the grade for
* @return the grade the student received in the course, or -1 if they haven't taken the course yet.
*/
public double getGrade(String course) {
/*TODO
for(int i= 0;i<COURSE_LIST.length; i++){
if(COURSE_LIST[i].equalsIgnoreCase(course)){
double grade = courseGrades[i];
return grade;
}
if(courses.containsKey(course)){
return Double.parseDouble(courses.get(course).getValue());
}else{
return -1;
}
*/
return Double.parseDouble(courses.get(course).getValue());
}
}
\ No newline at end of file
package com.ds.labfour;
/*
/**
* The author only made minor changes. The code was taken from IT306, spring 2019
*
* In this application, course list for students is implemented using parallel arrays.
* List of university courses is also represented with an array.
* For this Activity, add/make the required changes so that the complexity of the accessing course grade is improved to O(1).
* hint: use Map instead of array structure.
* Upload all YOUR java files (those you created/modified/used).
*
* @author <a href="mailto:mlundqu2@masonlive.gmu.edu">Michael Lundquist</a>
* Gnumber: 00737340
*/
import java.util.*;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment