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

Deleting the professor's answers that were posted online accidentally. Also...

Deleting the professor's answers that were posted online accidentally. Also adding the email from the professor about what I need to do.
parent 9ee64734
package com.ds.labfour;
/**
* 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).
*
*/
public class Student {
public final static int MAX = 100;
public final static String[] COURSE_LIST = {"IT106","IT206" , "IT306","IT212","IT213","IT214","COMM100","MATH112","IT293"};
......
package com.ds.labfour;
import java.util.HashMap;
import java.util.Map;
public class StudentMap {
public final static int MAX = 100;
//public final static String[] COURSE_LIST = {"IT106","IT206" , "IT306","IT212","IT213","IT214","COMM100","MATH112","IT293"};
public final static String LIST= "IT106,IT206,IT306,IT212,IT213,IT214,COMM100,MATH112,IT293";
public final static Map<String,String> COURSE_LIST = new HashMap<String, String>();
static
{
COURSE_LIST.put("IT106", "IT106");
COURSE_LIST.put("IT206", "IT206");
COURSE_LIST.put("IT306", "IT306");
COURSE_LIST.put("IT212", "IT212");
COURSE_LIST.put("IT213", "IT213");
COURSE_LIST.put("IT214", "IT214");
COURSE_LIST.put("COMM100", "COMM100");
COURSE_LIST.put("MATH112", "MATH112");
COURSE_LIST.put("IT293", "IT293");
}
private String name;
private String address;
private String major;
//private double[] courseGrades;
//private String[] courses;
private Map<String, Double> courses;
private int id;
private int numCourses;
private static int totalStudent;
public StudentMap(){
//courseGrades = new double[MAX];
//courses = new String[MAX];
totalStudent++;
this.id = totalStudent;
courses = new HashMap<String, Double>();
this.numCourses = 0;
}
public void setName(String name) {this.name = name;}
public void setAddress(String address) { this.address = address; }
public void setMajor(String major) {this.major = major; }
public boolean setCourse(double grade, String course){
if(numCourses >= MAX) return false;
if(COURSE_LIST.containsKey(course.toUpperCase())) {
courses.put(course.toUpperCase(), grade);
numCourses++;
return true;
}
/*
for(int i=0; i<COURSE_LIST.length; i++){
if(COURSE_LIST[i].equalsIgnoreCase(course)){
courseGrades[numCourses]=grade;
courses[numCourses++]=course;
return true;
}
}
*/
return false;
}
public String getName() { return name; }
public String getAddress(){ return address; }
public String getMajor(){ return major; }
public static int getTotalStudents(){ return totalStudent; }
public int getNumCourses(){ return numCourses;}
public int getId(){ return id;}
public double getGrade(String course) {
if(courses.containsKey(course.toUpperCase()))
return courses.get(course.toUpperCase());
/*
for(int i= 0;i<COURSE_LIST.length; i++){
if(COURSE_LIST[i].equalsIgnoreCase(course)){
double grade = courseGrades[i];
return grade;
}
}
*/
return -1;
}
}
\ No newline at end of file
package com.ds.labfour;
/*
* 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).
*/
import java.util.*;
import javax.swing.JOptionPane;
public class UniversityApplicationMap {
/**
* @param args
*/
public static void main(String[] args) {
Map<Integer, StudentMap> studentsRecords = new HashMap<Integer, StudentMap>();
String menu ="1- Add Student\n2-Show a student grade\n3-Exit.";
int option = 0;
double grade=-1;
do{
option = Integer.parseInt(JOptionPane.showInputDialog(menu));
switch(option){
case 1:
StudentMap s = getInput();
System.out.println(StudentMap.getTotalStudents());
studentsRecords.put(s.getId(), s); //adding student to hashmap
break;
case 2:
int id = Integer.parseInt(JOptionPane.showInputDialog("Enter an id number between 1 and " + StudentMap.getTotalStudents()) );
if(studentsRecords.containsKey(new Integer(id))){
s = studentsRecords.get(new Integer(id)); //retrieving student from hashmap
String course="";
do{
course = JOptionPane.showInputDialog("Enter course title from the list " + Student.LIST.toString());
grade = s.getGrade(course);
}while(grade==-1);
JOptionPane.showMessageDialog(null, "Grade for course "+course + " is: "+ grade);
}
else{
JOptionPane.showMessageDialog(null, "This id number does not exist.");
}
break;
case 3:
System.exit(0);
}
}while(true);
}
public static StudentMap getInput(){
StudentMap s = new StudentMap();
System.out.println(StudentMap.getTotalStudents());
s.setName(JOptionPane.showInputDialog("Enter student name."));
s.setMajor(JOptionPane.showInputDialog("Enter student major."));
do{
String course = null;
double grade=-1;
boolean flag = true;
course = JOptionPane.showInputDialog("Enter course title from the list of " + Student.LIST.toString());
do{
try{
grade = Double.parseDouble(JOptionPane.showInputDialog("Enter a grade for course " + course));
}catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "You must provide a numeric value.");
flag= false;
}
}while(!flag);
if(!s.setCourse(grade, course))
JOptionPane.showMessageDialog(null, "This course does not exist in the course list.");
}while(JOptionPane.showConfirmDialog(null, "Add another course?") == JOptionPane.YES_OPTION);
return s;
}
}
\ No newline at end of file
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