Question
Lab4 requirements: Write a Course.java that contains the following attributes: String courseNumber String courseName ArrayList prerequisiteCourseNumbers The class should contain all appropriate accessors/mutators and a
Lab4 requirements:
- Write a Course.java that contains the following attributes:
- String courseNumber
- String courseName
- ArrayList
prerequisiteCourseNumbers
The class should contain all appropriate accessors/mutators and a toString() method
- Write a ArrayList
loadFromFile(String fileNameIn) method that will read courses.txt file in this format: - Course_Num|Course_Name| prerequisite_1^ prerequisite_2^ prerequisite_N
- Write a Course parseRecord(String record) that will convert one line of the file that will convert one line from the file to a Course Object
- Write a recursive method Arraylist
getAllPrerequisites (String courseNum) that returns a complete list of prerequisites for a given course. That is, all its *immediate* pre-requisites, as well as all the prerequisites of its prerequisites until there are no more prerequisites (see example below). - Lab4 main, should invoke loadFromFile to load the courses, and repeatedly prompte the user to enter a course number and it will call getAllPrerequisites() and list all those pre-requisites back to the user.
I have written a program that fulfills requirements 2, 3 and 5 and contains a getAllPrequisites() shell In addition to coding the Course class and coding the recursive getAllPrerequisites() method, you will make sure there are no rawtype warnings
- The place where you need to make your changes is clearly marked TO-DO
- Fix [rawtypes] warnings. The program may work with these warnings, but you need to eliminate them
Course.txt>>
GEN1|Intro to School| ENG1|English 1|GEN1 ENG2|English 2|ENG1 ENG4|Reading 1| ENG5|Writing 1|ENG2 ENG6|Novel Writing|ENG5 MTH1|Numbers|GEN1 MTH2|Algebra|MTH1 MTH3|Geometry|MTH2 MTH4|Trig|MTH3 EGG0|Drafting|GEN1 EGG1|Engineering 1|MTH4^EGG0 SCI1|Basic Science|GEN1 SCI2|Biology|SCI1 SCI6|Physics|SCI1 SCI7|Nuclear Physics|SCI6^EGG1^ENG4 SCI8|Quantum Physics|SCI7 ENG7|Sci-Fi Writing|SCI6^ENG6 MUS1|Intro to Music| MUS2|Singing|MUS1 MUS9|Song Composition|MUS2^ENG5
Lab4_Headstart-2.java>>
// package yourpackage;
import java.io.BufferedReader;
import java.io.FileReader; import java.io.IOException; import java.util.Scanner; import java.util.ArrayList; import java.util.Arrays; import java.util.TreeSet;
/** * This class * 1. loads a list of courses from a course text file * 2. prompts a user for a specific course code * 3. lists all pre-requisities for that course * (both immediate and prior pre-requisities) * * @author M. Dabney * @version Feb 2020 */ //---------------------------------------------------------------------------- // public class Lab4_Headstart {
protected static final String DELIMITER = "\\|"; protected static final String REQ_DELIM = "\\^";
protected ArrayList
/** */ public Lab4_Headstart() {
}
public static void main(String [] args) { Lab4_Headstart lc = new Lab4_Headstart(); lc.allCourse = lc.loadFromFile("courses.txt"); Scanner input = new Scanner(System.in); while ( true ) { System.out.println(" Enter a course (enter 'quit' to exit)"); String courseNum = input.nextLine(); if (courseNum.equalsIgnoreCase("QUIT")) { break; } if (lc.getCourse(courseNum) == null) { System.out.println(courseNum +" is not a course on file"); continue; } ArrayList
System.out.println(" The pre-requisites for "+courseNum+" are : "); for (int i = 0; i < cNames.size(); i++) { Course c = lc.getCourse(cNames.get(i)); System.out.println(c.getCourseNumber()+" - "+c.getCourseName()); } } input.close(); }
public ArrayList
BufferedReader fileIn = null; try { fileIn = new BufferedReader (new FileReader(fileNameIn)); } catch (java.io.IOException ioe) { System.out.println("Open for read failed for " + fileNameIn + " : "+ ioe.getMessage()); System.exit(1); }
try {
String record = null;
while ((record = fileIn.readLine()) != null) { if ( ! isEmpty(record) ) { Course p = parseRecord(record); courseList.add(p); } rowNum++; }
fileIn.close();
// System.out.println(rowNum+" records read "+rowNum);
} catch (IOException ioe) { System.out.println("Unable to read " + fileNameIn + " : " + ioe.getMessage()); System.exit(1); } catch (Exception e) { System.out.println("Exception with " + fileNameIn + " at line "+rowNum+" : " + e.toString()); e.printStackTrace(); System.exit(1); } finally {
} return courseList; }
//TO-DO CODE THIS METHOD /* This method will be your recursive method * @param a course number * @return arrayList of *all* prereqs (not just its immediate prereqs) */ public ArrayList
/* This method will take a string arraylist and eliminate any duplicates * @param ArrayList of String * @return arrayList with any duplicates eliminaetd */ private ArrayList
/* Locate a specific course on this objects courseList by its course number * @param CourseNumber * @return return the entire Course */ public Course getCourse(String courseNumber) { Course c = null;
for (int i = 0; i < this.allCourse.size(); i++) { Course thisCourse = this.allCourse.get(i); if (thisCourse.getCourseNumber().equals(courseNumber)) { c = thisCourse; } }
return c; }
/* Takes one line from the courses.txt file and creates Course object * @param String (one line from file) * @return Course */ protected Course parseRecord(String record) throws Exception { Course course = null; if (! isEmpty(record)) { String [] fields = record.split(DELIMITER); course = new Course(); course.setCourseNumber(fields[0]); course.setCourseName(fields[1]); ArrayList
course.setPrerequisiteCourseNumbers(al); } return course;
}
/* This method will check to see if a String is all spaces or null. * @param string this is the string to be checked for spaces. * @return returns a boolean value of * True if the String is empty. (null, empty string, or *only* spaces) * False if the string has a nonspace in it. */ public static boolean isEmpty(String s) { boolean isEmpty = false; if (s == null || s.trim().length() == 0) { isEmpty = true; } return isEmpty; }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started