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

Altered some of the given code, and getting my Student code working.

parent ac54fdd9
package com.ds.labfour;
import java.util.*;
public class MyBucket {
private Set<MyEntry> elements;
private int key;
private int num;
public MyBucket(int k){
elements = new TreeSet<MyEntry>();
key = k;
}
public void add(MyEntry e){
boolean flag=false;
Iterator it = elements.iterator();
while(it.hasNext())
if(e.getKey().equals( ((MyEntry)it.next()).getKey()) ){
flag= true;
break;
}
if (!flag) elements.add(e);
}
public Set<MyEntry> getElements(){
return elements;
}
public MyEntry getElement(String key){
if (elements.isEmpty()) return null;
Iterator it = elements.iterator();
while(it.hasNext()){
MyEntry entry = (MyEntry)it.next();
if(key.equals( entry.getKey()) )
return entry;
}
return null;
}
public boolean removeElement(String key){
if (elements.isEmpty()) return false;
for(MyEntry e: elements){
if (key.equals(e.getKey())) {
elements.remove(e);
return true;
}
}
return false;
}
public int getKey(){
return key;
}
public int getSize(){
return num;
}
}
\ No newline at end of file
package com.ds.labfour;
public class MyEntry implements Comparable{
private String key;
private String value;
public MyEntry (String k, String v){
key = k;
value = v;
}
public void setKey(String k) { key = k; }
public void setValue(String v) {value = v; }
public String getKey() { return key; }
public String getValue() {return value; }
public int compareTo(Object o) {
if( o instanceof MyEntry){
MyEntry newEntry = (MyEntry)o;
if(newEntry.getKey().compareTo(key)<0) return -1;
else if (newEntry.getKey().compareTo(key)>0) return 1;
else return 0;
}
return -2; //invalid comparison
}
} //end of class
\ No newline at end of file
package com.ds.labfour;
import java.util.*;
public class MyHashmap implements Map, Cloneable{
private MyBucket[] index;
private static final int DEFAULT = 100;
private int size= 0;
/**
* I changed this to overloading.
* Before if you initialized it with MyHashmap(int), index[] was all nulls,
* so everything had NullPointer exceptions.
*/
public MyHashmap() {
this(DEFAULT);
}
public MyHashmap(int capacity) {
index = new MyBucket[capacity];
for (int i=0; i<index.length; i++)
index[i] = new MyBucket(i);
}
private int calcPosition(String k){
return k.hashCode() % (index.length -1); //this is the index for a bucket.
}
public Set<String> keySet() {
TreeSet<String> keys = new TreeSet<String>();
for(int i=0; i<index.length; i++){
Set<MyEntry> kTemp = index[i].getElements();
for(MyEntry e : kTemp)
keys.add(e.getKey());
}
return keys;
}
public Set<MyEntry> entrySet() {
TreeSet<MyEntry> entryset = new TreeSet<MyEntry>();
for(int i=0; i<index.length; i++){
Set<MyEntry> kTemp = index[i].getElements();
if ( kTemp!=null && kTemp.size()>0)
entryset.addAll(kTemp);
}
return entryset;
}
public Collection<String> values() {
LinkedList<String> values = new LinkedList<String>();
for(int i=0; i<index.length; i++){
Set<MyEntry> kTemp = index[i].getElements();
for(MyEntry e : kTemp)
values.add(e.getValue());
}
return values;
}
public boolean isEmpty(){
return (size==0) ?true:false;
}
public MyHashmap clone(){
MyHashmap answer= null;
try{
answer= (MyHashmap) super.clone();
answer.index = index.clone();
return answer;
}
catch(CloneNotSupportedException e){
throw new RuntimeException("This class is not Cloneable!");
}
}
public void clear() {//Removes all the mapping from this map
for(MyBucket b: index) b=null;
size=0;
}
public int size() {
return size;
}
public boolean containsKey(Object key) {
MyBucket b = index[calcPosition((String) key)];
MyEntry e=b.getElement((String) key);
return (e==null) ? false:true;
}
public boolean containsValue(Object value) {
//returns true if map has one or more keys to the specified value
ArrayList<MyEntry> match = new ArrayList<MyEntry>();
for(MyBucket b: index){
for(MyEntry e: b.getElements()){
if(value.equals(e.getValue())){
match.add(e);
}
}
}
return (match.size()==0) ? false :true;
}
/**
* I changed this to return MyEntry that's what MyBucket.getElement returns
*/
public MyEntry get(Object key) {
MyBucket b = index[calcPosition((String)key)];
return (b==null) ?null : b.getElement((String)key);
}
public Object put(Object key, Object value) {
MyEntry oldEntry = ((MyEntry) this.get((String) key)) ;
MyEntry e = new MyEntry((String) key, (String) value);
index[calcPosition((String)key)].add(e);
size++;
return (oldEntry==null ) ? null : oldEntry.getValue();
}
public Object put(Object key, Double value) {
MyEntry oldEntry = ((MyEntry) this.get((String) key)) ;
MyEntry e = new MyEntry((String) key, "" + value);
index[calcPosition((String)key)].add(e);
size++;
return (oldEntry==null ) ? null : oldEntry.getValue();
}
public Object remove(Object key) {
MyBucket b = index[calcPosition((String)key)];
String value = null;
value = b.getElement((String) key).getValue();
//remove:
b.removeElement((String) key);
size--;
return value;
}
public void putAll(Map m) {
// TODO Complete this method as an exercise
}
}
\ No newline at end of file
package com.ds.labfour;
import java.util.Arrays;
import java.util.HashSet;
/**
* 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.
......@@ -10,19 +13,32 @@ package com.ds.labfour;
public class Student {
public final static int MAX = 100;
public final static String[] COURSE_LIST = {"IT106","IT206" , "IT306","IT212","IT213","IT214","COMM100","MATH112","IT293"};
//This doesn't need to be private because Strings are immutable.
private static final HashSet<String> COURSE_SET = new HashSet <> (Arrays.asList(COURSE_LIST));
// 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";
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;
public Student(){
/*
courseGrades = new double[MAX];
courses = new String[MAX];
*/
courses = new MyHashmap(COURSE_LIST.length);
this.numCourses = 0;
totalStudent++;
this.id = totalStudent;
......@@ -32,7 +48,10 @@ public class Student {
public void setMajor(String major) {this.major = major; }
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;
......@@ -40,8 +59,10 @@ public class Student {
return true;
}
}
return false;
*/
this.courses.put(course, grade);
numCourses++;
return true;
}
public String getName() { return name; }
......@@ -52,14 +73,14 @@ public class Student {
public int getId(){ return id;}
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;
}
}
return -1;
*/
return Double.parseDouble(courses.get(course).getValue());
}
}
\ No newline at end of file
......@@ -52,8 +52,9 @@ public class UniversityApplication {
}
public static Student getInput(){
Student s = new Student();
Student s = new Student();
s.setName(JOptionPane.showInputDialog("Enter student name."));
JOptionPane.showMessageDialog(null, s.getName() + "'s id is: " + s.getId());
s.setMajor(JOptionPane.showInputDialog("Enter student major."));
do{
String course = null;
......
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