Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project Description You will build part of a college\'s course registration syst

ID: 3695106 • Letter: P

Question

Project Description

You will build part of a college's course registration system (similar to WebReg, though considerably less complex). The system will keep track of a catalog of courses that students can take. It will allow the user (a student) to view and search the course catalog, and to add (register for) and drop (withdraw from) courses.

The following restrictions apply:

All courses meet only once per week, for one class period.

Days will be denoted by the following letters:
M - Monday, T - Tuesday, W - Wednesday, H - Thursday, F - Friday, S - Saturday. There are no Sunday courses.

Class periods (time slots) are numbered from 1 to 9.

A student cannot register for a course that is not in the catalog.

There is a limit on the number of courses a student may take in one semester.

There is NO explicit limit on the number of credits a student may carry in one semester.

A student cannot register for two courses that meet on the same day and time.

System Implementation (Phase 3)

The user interface has been written for you in WebRegApp.java. This program handles all keyboard and file input, and all screen output. You will complete the following methods in WebReg.java, which are designed to perform actions such as searching and sorting the catalog.

WebRegApp.java first reads the catalog (a list of courses) from catalog.txt. The catalog will be represented as an array of Course objects. WebRegApp reads the catalog from the file catalog.txt - you may edit this file if you wish. If you add entries to it, be sure they follow the same format as the entries already in the file. It then shows the user a menu from which they can choose various options. In most cases, WebRegApp will call methods from WebReg to execute the option chosen by the user. *ignore options 3 & 4 – you do not need to implement those methods until the next milestone.

Courses for which the student registers should be placed into the student’s schedule array starting at index 0. Empty (unused) entries in the array should remain null; all empty entries should be on the right side of the array (there should be no "gaps" between used entries). For example, since the maximum number of courses allowed is 6 and if the student is currently taking 3 courses, the schedule array would be of size 6, entries 0 through 2 would contain Course objects, and entries 3 through 5 would contain null. This should be the case regardless of the sequence of adds and drops the student performed in order to create his or her current schedule. The same concept applies for the roster of students for a specific course.

Implement the following methods in WebReg.java. DO NOT modify WebRegApp.java or your Period, Course, and Student objects. If needed, you may add helper methods and fields in WebReg.java.

public static Course lookupCourseByName(Course[] catalog, String courseName)

search for the course with the given name in the catalog, and return the corresponding Course object.

if the course is not found, return null.

*you may ignore the letter casing of the course name – in other words, if courseName is all uppercase but the name of the course in the catalog is all lowercase, it would still be a match.

public static Course[] lookupCoursesByDept(Course[] catalog, int department)

search for all courses offered by the given department in the catalog, and return the corresponding Course objects in a new array (in any order).

the returned array should be just large enough to hold the results, and no larger; it should not contain any empty (null) entries.

if no courses are found with that department number, return null.

public static int countCredits(Student s)

compute the student's current total credit load.

public static boolean addCourse(Student s, Course c)

attempt to add a course, c, to the student's schedule.

put the Course object into the student’s schedule in the first free slot (null entry), update the courses’ roster, and return true.

if the student is not allowed to register for this course (for any reason), return false and do not alter the student’s schedule or the courses’ roster.

public static boolean dropCourse(Student s, Course c)

attempt to remove a course, c, from the student's schedule. search for the Course object in the student's schedule.

if you find the Course object in the student’s schedule, remove it from the student’s schedule, update the courses’ roster, making sure not to create any "gaps" (null entries between used entries), and return true.

if you cannot find the course in the student's schedule or the student is not in the courses’ roster, return false.

Explanation / Answer

Hello there,

Please find below code and it's output. Let me know if you have any queires. Thanks.

public class Course{
  
String name;
int department;
int courseNum;
char day;
int timeSlot;
int credits;
private Period period = new Period(day,timeSlot);
Student students[] = new Student[20];
public Course(int department, int courseNum, String name, char day, int timeSlot, int credits) {
this.department = department;
this.courseNum = courseNum;
this.name = name;
this.day = day;
this.timeSlot = timeSlot;
this.credits = credits;
period = new Period(day,timeSlot);
}
  
public int getDepartment() {
return department;
}
public String getName() {
return name;
}
  
public Student[] getRoster() {
return students;
}
  
public Period getPeriod() {
return period;
}
  
public int getCourseNumber() {
return courseNum;
}
  
public int getCredits() {
return credits;
}
  
public boolean equals(Course other){
return courseNum == other.courseNum && department==other.department;
}
  
public String toString(){
return department + ":" + courseNum + " ["+name+"] " + period + " credits:" + credits;
}
}

=========================

public class Student {
   private String firstName;
   private String lastName;
   private int gradYear;
   private int id;
   Course courses[] = new Course[6];

   public Student(String firstName, String lastName, int id, int gradYear) {

       this.firstName = firstName;
       this.lastName = lastName;
       this.id = id;
       this.gradYear = gradYear;
   }

   public String getLastName() {
       return lastName;
   }

   public int getGradYear() {
       return gradYear;
   }

   public int getId() {
       return id;
   }

   public String getFirstName() {
       return firstName;
   }

   public Course[] getSchedule() {
       return courses;
   }

   public void setSchedule(Course[] courses) {
       this.courses = courses;
   }

   public String toString() {
       return id + ": " + lastName + ", " + firstName + " - " + gradYear;
   }

   public boolean equals(Student other) {
       if (this.id == other.id) {
           return true;
       } else {
           return false;
       }
   }
}

===============================

import java.io.*;

public class WebRegApp {
   public static void main(String[] args) {
       Course[] catalog;
       Student me = new Student("Joe", "Smith", 123456789, 2020);
       Course[] results;
       Course course;
       String catalogFilename, courseName;
       int dept, numCredits;
       boolean success;
       catalogFilename = "catalog.txt";
       catalog = readFile(catalogFilename);

       if (catalog == null) {
           System.err.println("Error reading " + catalogFilename + ":");
           return;
       }
       while (true) {
           int choice = getMenuChoice();
           if (choice == 1) {
               System.out.println("Enter name of course:");
               courseName = IO.readString();
               System.out.println();
               course = WebReg.lookupCourseByName(catalog, courseName);
               if (course != null) {
                   System.out.println(course);
               } else {
                   System.out.println("No course found by that name.");
               }
           } else if (choice == 2) {
               System.out.println("Enter number of department:");
               dept = IO.readInt();
               System.out.println();
               results = WebReg.lookupCoursesByDept(catalog, dept);
               if (results != null) {
                   printArray(results);
               } else {
                   System.out.println("No courses found in that department.");
               }
           } else if (choice == 3) {
               // WebReg.sortByNumber(catalog);
               // printArray(catalog);
           } else if (choice == 4) {
               // WebReg.sortByTime(catalog);
               // printArray(catalog);
           } else if (choice == 5) {
               System.out.println("Current schedule:");
               printArray(me.getSchedule());
           } else if (choice == 6) {
               numCredits = WebReg.countCredits(me);
               System.out.println("Credits being taken: " + numCredits);
           } else if (choice == 7) {
               System.out.println("Enter course name:");
               courseName = IO.readString();
               Course c = WebReg.lookupCourseByName(catalog, courseName);
               System.out.println();
               if (c != null) {
                   success = WebReg.addCourse(me, c);
                   if (success) {
                       System.out.println("Successfully registered.");
                   } else {
                       System.out.println("Attempt to register failed.");
                   }
               } else {
                   System.out.println("No course found by that name.");
               }
           } else if (choice == 8) {
               System.out.println("Enter course name:");
               courseName = IO.readString();
               Course c = WebReg.lookupCourseByName(catalog, courseName);
               System.out.println();
               if (c != null) {
                   success = WebReg.dropCourse(me, c);
                   if (success) {
                       System.out.println("Successfully withdrawn.");
                   } else {
                       System.out.println("Attempt to withdraw failed.");
                   }
               } else {
                   System.out.println("No course found by that name.");
               }
           } else if (choice == 9) {

               System.out.println("Enter course name:");
               courseName = IO.readString();
               Course c = WebReg.lookupCourseByName(catalog, courseName);
               System.out.println();
               if (c != null) {
                   printArray(c.getRoster());
               } else {
                   System.out.println("No course found by that name.");
               }
           } else {
               return;
           }
       }
   }

   private static final String[] menuChoices = { "Look up course by name", "Look up courses by department",
           "View catalog, sorted by department/course number", "View catalog, sorted by scheduled day/period", "View my course schedule",
           "View my current credit load", "Register for a course", "Withdraw from a course", "View a courses' roster" };

   private static int getMenuChoice() {
       int numChoices = menuChoices.length + 1;
       System.out.println();
       System.out.println("Menu:");
       for (int i = 0; i < menuChoices.length; i++) {
           System.out.println((i + 1) + ". " + menuChoices[i]);
       }
       System.out.println(numChoices + ". Quit");
       System.out.println();
       System.out.println("Choice (1-" + numChoices + ")?");
       int choice = IO.readInt();
       while (choice < 1 || choice > numChoices) {
           System.out.println();
           System.out.print("That is not a valid menu option. Pick 1-" + numChoices + ". ");
           choice = IO.readInt();
       }
       System.out.println();
       return choice;
   }

   private static Course[] readFile(String filename) {
       BufferedReader file;
       int numEntries;
       Course[] entries;
       try {
           file = new BufferedReader(new FileReader(filename));
           numEntries = parseFile(file, false, null);
           entries = new Course[numEntries];
           file.close();
           file = new BufferedReader(new FileReader(filename));
           parseFile(file, true, entries);
           file.close();
           return entries;
       } catch (IOException e) {
           e.printStackTrace();
           return null;
       } catch (IllegalArgumentException e) {
           e.printStackTrace();
           return null;
       }
   }

   private static int parseFile(BufferedReader file, boolean readEntries, Course[] catalog) throws IOException {
       int numEntries;
       String line;
       numEntries = 0;
       while ((line = file.readLine()) != null) {
           line = line.trim();
           if (line.equals("")) {
               continue;
           }
           if (readEntries) {
               catalog[numEntries] = parseCourseLine(line);
           }
           numEntries++;
       }
       return numEntries;
   }

   private static Course parseCourseLine(String line) {
       String remaining, part;
       int index;
       int dept, courseNum, timeSlot, credits;
       String name;
       char day;
       remaining = line.trim();
       // read dept
       index = remaining.indexOf(':');
       if (index == -1) {
           throw new IllegalArgumentException("badly formatted line in catalog file: " + line);
       }
       part = remaining.substring(0, index);
       try {
           dept = Integer.parseInt(part);
           if (dept < 0 || dept > 999) {
               throw new IllegalArgumentException("invalid department number on line: " + line);
           }
       } catch (NumberFormatException e) {
           throw new IllegalArgumentException("missing department number on line: " + line);
       }
       remaining = remaining.substring(index + 1);
       // read number
       index = remaining.indexOf(' ');
       if (index == -1) {
           throw new IllegalArgumentException("badly formatted line in catalog file: " + line);
       }
       part = remaining.substring(0, index);
       try {
           courseNum = Integer.parseInt(part);
           if (courseNum < 0 || courseNum > 999) {
               throw new IllegalArgumentException("invalid course number on line: " + line);
           }
       } catch (NumberFormatException e) {
           throw new IllegalArgumentException("missing course number on line: " + line);
       }
       remaining = remaining.substring(index + 1);
       // read name
       index = remaining.indexOf('[');
       if (index == -1) {
           throw new IllegalArgumentException("badly formatted line in catalog file: " + line);
       }
       remaining = remaining.substring(index + 1);
       index = remaining.indexOf(']');
       if (index == -1) {
           throw new IllegalArgumentException("badly formatted line in catalog file: " + line);
       }
       name = remaining.substring(0, index);
       remaining = remaining.substring(index + 1);
       // read day
       remaining = remaining.trim();
       if (remaining.length() < 2) {
           throw new IllegalArgumentException("badly formatted line in catalog file: " + line);
       }
       day = Character.toUpperCase(remaining.charAt(0));
       if (day != 'M' && day != 'T' && day != 'W' && day != 'H' && day != 'F' && day != 'S') {
           throw new IllegalArgumentException("invalid day on line: " + line);
       }
       try {
           timeSlot = Integer.parseInt(remaining.substring(1, 2));
           if (timeSlot < 1 || timeSlot > 9) {
               throw new IllegalArgumentException("invalid class time slot on line: " + line);
           }
       } catch (NumberFormatException e) {
           throw new IllegalArgumentException("missing class time slot on line: " + line);
       }
       remaining = remaining.substring(2);
       // read credits
       remaining = remaining.trim();
       if (!remaining.startsWith("credits:")) {
           throw new IllegalArgumentException("badly formatted line in catalog file: " + line);
       }
       remaining = remaining.substring("credits:".length());
       try {
           credits = Integer.parseInt(remaining);
           if (credits < 1) {
               throw new IllegalArgumentException("invalid number of credits on line: " + line);
           }
       } catch (NumberFormatException e) {
           throw new IllegalArgumentException("missing or invalid number of credits on line: " + line);
       }
       Course c = new Course(dept, courseNum, name, day, timeSlot, credits);
       return c;
   }

   private static void printArray(Object[] array) {
       for (int i = 0; i < array.length; i++) {
           if (array[i] == null) {
               System.out.println("null entry in array");
           } else {
               System.out.println(array[i]);
           }
       }
   }
}

===================================


public class WebReg {

   public static Course lookupCourseByName(Course[] catalog, String courseName) {
       Course course = null;
       // iterate the String array
       for (int i = 0; i < catalog.length; i++) {

           // check if course array contains the string
           if (catalog[i].getName().toLowerCase().equals(courseName.toLowerCase())) {
               // course found
               course = catalog[i];
               break;

           }
       }

       return course;

   }

   public static Course[] lookupCoursesByDept(Course[] catalog, int dept) {
       Course[] courses1 = new Course[catalog.length];

       int index = 0;
       // iterate the String array
       for (int i = 0; i < catalog.length; i++) {
           // check if course array contains the dept
           if (catalog[i].getDepartment() == dept) {
               // course found
               courses1[index] = catalog[i];
               index++;
           }
       }
       Course[] courses = new Course[index];

       System.arraycopy(courses1, 0, courses, 0, index);

       return courses;
   }

   public static int countCredits(Student me) {
       Course[] courses = me.getSchedule();
       int countOfCredits = 0;
       for (int i = 0; i < courses.length; i++) {
           countOfCredits = countOfCredits + courses[i].getCredits();
       }
       return countOfCredits;
   }

   public static boolean addCourse(Student me, Course c) {
       Course[] coursesRegistered = me.getSchedule();
       if (!contains(coursesRegistered, c)) {
           for (int i = 0; i < coursesRegistered.length; i++) {
               if (coursesRegistered[i] == null) {
                   coursesRegistered[i] = c;
                   return true;
               }
           }
       } else {
           return false;
       }

       return false;

   }

   public static boolean dropCourse(Student me, Course c) {
       Course[] coursesRegistered = me.getSchedule();
       Course[] coursesRegisteredUpdated = new Course[coursesRegistered.length - 1];
       boolean changed = false;
       if (contains(coursesRegistered, c)) {
           int index = 0;
           for (int i = 0; i < coursesRegistered.length; i++) {
               if (coursesRegistered[i] == c) {
                   changed = true;
                   continue;
               } else {
                   coursesRegisteredUpdated[index] = coursesRegistered[i];
                   index++;
               }
           }
           me.setSchedule(coursesRegisteredUpdated);

       } else {
           return false;
       }

       return changed;
   }

   public static boolean contains(Course[] coursesRegistered, Course c) {
       if (coursesRegistered[0] != null) {

           for (Course course : coursesRegistered) {
               if (course.getName().equals(c.getName()))
                   return true;
           }
       } else {
           return false;
       }
       return false;
   }
}

===================================

public class Period {
  
private int timeSlot;
private char day;
  
public Period(char day, int timeSlot){
if(day >= 'a' && day <= 'z'){
day = (char) (day + (int)'A' - (int)'a');
}
this.day = day;
this.timeSlot = timeSlot;
}
public char getDay() {
return day;
}
public int getTimeSlot() {
return timeSlot;
}
public void setDay(char day) {
this.day = day;
}

public void setTimeSlot(int timeSlot) {
this.timeSlot = timeSlot;
}

public boolean equals(Period p){
return day == p.day && timeSlot == p.timeSlot;
}

public String toString() {
return "" + day + "" + timeSlot;
}
  
public int compareTo(Period other){
if(equals(other)){
return 0;
}
else{
if(other.day == day){
if(timeSlot < other.timeSlot){
return -1;
}
else{
return 1;
}
}
else{
String days = "SMTWF";
int ind1 = days.indexOf(day);
int ind2 = days.indexOf(other.day);
if(ind1 < ind2){
return -1;
}
else{
return 1;
}
}
}
}
  
}

=====================

import java.io.*;
public class IO
{
private static BufferedReader kb =
new BufferedReader(new InputStreamReader(System.in));
  
private static BufferedReader fio = null;
  
public static boolean openFile(String filename){
  
try{
fio = new BufferedReader(new FileReader(filename));
return true;
}catch (IOException e){ return false;}
}
  
public static String readLine(){
if (fio == null)
return null;
  
try{
return fio.readLine();
}catch(IOException e){ return null;}
}
  
public static String readString()
{
while (true) {
try {
return kb.readLine();
} catch (IOException e) {
// should never happen
}
}
}
public static int readInt()
{
while (true) {
try {
String s = kb.readLine();
return Integer.parseInt(s);
} catch (NumberFormatException e) {
System.out.print("That is not an integer. Enter again: ");
} catch (IOException e) {
// should never happen
}
}
}
public static double readDouble()
{
while (true) {
try {
String s = kb.readLine();
return Double.parseDouble(s);
} catch (NumberFormatException e) {
System.out.print("That is not a number. Enter again: ");
} catch (IOException e) {
// should never happen
}
}
}
public static char readChar()
{
String s = null;
try {
s = kb.readLine();
} catch (IOException e) {
// should never happen
}
while (s.length() != 1) {
System.out.print("That is not a single character. Enter again: ");
try {
s = kb.readLine();
} catch (IOException e) {
// should never happen
}
}
return s.charAt(0);
}
public static boolean readBoolean()
{
String s = null;

while (true) {
try {
s = kb.readLine();
} catch (IOException e) {
// should never happen
}

if (s.equalsIgnoreCase("yes") ||
s.equalsIgnoreCase("y") ||
s.equalsIgnoreCase("true") ||
s.equalsIgnoreCase("t")) {
return true;
} else if (s.equalsIgnoreCase("no") ||
s.equalsIgnoreCase("n") ||
s.equalsIgnoreCase("false") ||
s.equalsIgnoreCase("f")) {
return false;
} else {
System.out.print("Enter "yes" or "no": ");
}
}
}
public static void outputStringAnswer(String s)
{
if (s != null) {
System.out.println("RESULT: "" + s + """);
} else {
System.out.println("RESULT: null");
}
}
public static void outputIntAnswer(int i)
{
System.out.println("RESULT: " + i);
}
public static void outputDoubleAnswer(double d)
{
System.out.println("RESULT: " + d);
}
public static void outputCharAnswer(char c)
{
System.out.println("RESULT: '" + c + "'");
}
public static void outputBooleanAnswer(boolean b)
{
System.out.println("RESULT: " + b);
}
public static void reportBadInput()
{
System.out.println("User entered bad input.");
}
}

=======catalog.txt=======

198:112 [Data Structure] M5 credits:4
560:101 [Elementary Italian] M2 credits:4
400:105 [Facets of Food Science] H1 credits:1
198:111 [Introduction to Computer Science] T7 credits:4
560:232 [Italian Culture] M5 credits:3
220:301 [Money and Banking] F5 credits:3
965:300 [New York Theater Experience] W6 credits:3
105:343 [Observational Radio Astronomy] M2 credits:3
776:310 [Plant Propagation] H2 credits:3
965:315 [Playwriting] W4 credits:3
105:342 [Principles of Astrophysics] T5 credits:3
080:201 [Seminar in Contemporary Art] F4 credits:3

==================================O/P===========================

Menu:

1. Look up course by name
2. Look up courses by department
3. View catalog, sorted by department/course number
4. View catalog, sorted by scheduled day/period
5. View my course schedule
6. View my current credit load
7. Register for a course
8. Withdraw from a course
9. View a courses' roster
10. Quit

Choice (1-10)?
7

Enter course name:
Playwriting

Successfully registered.

Menu:
1. Look up course by name
2. Look up courses by department
3. View catalog, sorted by department/course number
4. View catalog, sorted by scheduled day/period
5. View my course schedule
6. View my current credit load
7. Register for a course
8. Withdraw from a course
9. View a courses' roster
10. Quit

Choice (1-10)?
5

Current schedule:
965:315 [Playwriting] W4 credits:3
null entry in array
null entry in array
null entry in array
null entry in array
null entry in array

Menu:
1. Look up course by name
2. Look up courses by department
3. View catalog, sorted by department/course number
4. View catalog, sorted by scheduled day/period
5. View my course schedule
6. View my current credit load
7. Register for a course
8. Withdraw from a course
9. View a courses' roster
10. Quit

Choice (1-10)?
7

Enter course name:
Principles of Astrophysics

Successfully registered.

Menu:
1. Look up course by name
2. Look up courses by department
3. View catalog, sorted by department/course number
4. View catalog, sorted by scheduled day/period
5. View my course schedule
6. View my current credit load
7. Register for a course
8. Withdraw from a course
9. View a courses' roster
10. Quit

Choice (1-10)?
5

Current schedule:
965:315 [Playwriting] W4 credits:3
105:342 [Principles of Astrophysics] T5 credits:3
null entry in array
null entry in array
null entry in array
null entry in array

Menu:
1. Look up course by name
2. Look up courses by department
3. View catalog, sorted by department/course number
4. View catalog, sorted by scheduled day/period
5. View my course schedule
6. View my current credit load
7. Register for a course
8. Withdraw from a course
9. View a courses' roster
10. Quit

Choice (1-10)?
8

Enter course name:
Principles of Astrophysics

Successfully withdrawn.

Menu:
1. Look up course by name
2. Look up courses by department
3. View catalog, sorted by department/course number
4. View catalog, sorted by scheduled day/period
5. View my course schedule
6. View my current credit load
7. Register for a course
8. Withdraw from a course
9. View a courses' roster
10. Quit

Choice (1-10)?
5

Current schedule:
965:315 [Playwriting] W4 credits:3
null entry in array
null entry in array
null entry in array
null entry in array

Menu:
1. Look up course by name
2. Look up courses by department
3. View catalog, sorted by department/course number
4. View catalog, sorted by scheduled day/period
5. View my course schedule
6. View my current credit load
7. Register for a course
8. Withdraw from a course
9. View a courses' roster
10. Quit

Choice (1-10)?
2

Enter number of department:
105

105:343 [Observational Radio Astronomy] M2 credits:3
105:342 [Principles of Astrophysics] T5 credits:3

Menu:
1. Look up course by name
2. Look up courses by department
3. View catalog, sorted by department/course number
4. View catalog, sorted by scheduled day/period
5. View my course schedule
6. View my current credit load
7. Register for a course
8. Withdraw from a course
9. View a courses' roster
10. Quit

Choice (1-10)?