Commit 9ee64734 authored by michael lundquist's avatar michael lundquist
Browse files

Adding code given by the instructor

parent e18f7635
Pipeline #4201 passed with stage
in 1 minute and 28 seconds
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"};
public final static String LIST= "IT106,IT206,IT306,IT212,IT213,IT214,COMM100,MATH112,IT293";
private String name;
private String address;
private String major;
private double[] courseGrades;
private String[] courses;
private int id;
private int numCourses;
private static int totalStudent;
public Student(){
courseGrades = new double[MAX];
courses = new String[MAX];
this.numCourses = 0;
totalStudent++;
this.id = totalStudent;
}
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;
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) {
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;
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 UniversityApplication {
/**
* @param args
*/
public static void main(String[] args) {
Map<Integer, Student> studentsRecords = new HashMap<Integer, Student>();
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:
Student s = getInput();
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 " + Student.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 Student getInput(){
Student s = new Student();
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
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