Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

  1. Write a Course.java that contains the following attributes:
    1. String courseNumber
    2. String courseName
    3. ArrayList prerequisiteCourseNumbers

The class should contain all appropriate accessors/mutators and a toString() method

  1. Write a ArrayList loadFromFile(String fileNameIn) method that will read courses.txt file in this format:
    1. Course_Num|Course_Name| prerequisite_1^ prerequisite_2^ prerequisite_N
  2. 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
  3. Write a recursive method ArraylistgetAllPrerequisites (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).
  4. 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

  1. The place where you need to make your changes is clearly marked TO-DO
  2. 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 allCourse = null;

/** */ 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 cNames = lc.getAllPrereqs(courseNum);

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 loadFromFile(String fileNameIn) { ArrayList courseList = new ArrayList() ; int rowNum = 0;

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 getAllPrereqs(String courseNumber) { return null; } // TO-DO END

/* This method will take a string arraylist and eliminate any duplicates * @param ArrayList of String * @return arrayList with any duplicates eliminaetd */ private ArrayList eliminateDuplicates(ArrayList al) { // quick and dirty way to eliminate redundancies TreeSet set = new TreeSet <> (al); al = new ArrayList(set); return al; }

/* 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 al = new ArrayList(); if (fields.length > 2) { String r = fields[2]; String [] preReqs = r.split(REQ_DELIM); if ((preReqs != null) && preReqs.length > 0) { al = new ArrayList ( Arrays.asList(preReqs)); } }

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PC Magazine Guide To Client Server Databases

Authors: Joe Salemi

1st Edition

156276070X, 978-1562760700

More Books

Students also viewed these Databases questions

Question

Exactly why was it made?

Answered: 1 week ago

Question

Which team solution is more likely to be pursued and why?

Answered: 1 week ago

Question

4. I can tell when team members dont mean what they say.

Answered: 1 week ago