Linked based project would like some help
im posting my coding to see if anyone here can help me understand and debug my coding. ill be putting up the instructions on what i need to do
The Course class
A Course object should store the department, course number, and course title. It should have linked lists of prerequisites and "subsequents" (the courses for which this course is a prerequisite).
The constructor should construct a course given a department, course number, and course title.
There should be methods to add prerequisites and subsequents.
There should be accessor methods for all the fields.
The toString method should return a String with all the course information.
You are free to implement other methods as needed to complete the project.
Code:
public class Course{
private String department,coursetitle;
private int coursenumber;
private LinkedList<String> prerequisites;
private LinkedList<String> subsequents;
public Course(String dep, int num, String title) {
department = dep;
coursenumber = num;
coursetitle = title;
prerequisites = new LinkedList<String>();
subsequents = new LinkedList<String>();
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public int getCoursenumber() {
return coursenumber;
}
public void setCoursenumber(int coursenumber) {
this.coursenumber = coursenumber;
}
public String getCoursetitle() {
return coursetitle;
}
public void setCoursetitle(String coursetitle) {
this.coursetitle = coursetitle;
}
public void addPrerequisite(String department, String number) {
this.prerequisites.add(department + number);
}
public LinkedList<String> getPrerequisites() {
return prerequisites;
}
public void addSubsequent(Course course) {
this.subsequents.add(course.getDepartment() + course.getCoursenumber());
}
public LinkedList<String> getSubsequents() {
return subsequents;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Course)) {
return false;
}
Course course = (Course) obj;
if (course.getCoursenumber() == this.getCoursenumber() &&
course.getDepartment().equals(this.getDepartment())) {
return true;
}
return false;
}
public String toString() {
StringBuffer str = new StringBuffer();
str.append(department + " ");
str.append(coursenumber);
if (coursetitle != null) {
str.append(" " + coursetitle);
}
if (!this.prerequisites.isEmpty()) {
str.append("\n\t\tPrereqs: " + this.prerequisites);
}
if (!this.subsequents.isEmpty()) {
str.append("\n\t\tSubsequents: " + this.subsequents);
}
return str.toString();
}
}
The CourseCatalog class
Required Constructor:
public CourseCatalog(String filename)
Required Method:
public String toString()
This class should store a linked list of courses.
The constructor should construct a course catalog given a file name. Example files are provided below. All the courses in the file should be constructed and should include all prerequisite and subsequent information.
There should be methods to return a course given its name. It should have some flexibility, e.g., "CS 2123" or "cs2123" should result in the same course.
The toString method should return a String describing all the courses.
There should be some way to access or loop thru all the courses in the course catalog.
You are free to implement other methods as needed to complete the project.
Code:
package courses;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;
import java.util.LinkedList;
public class CourseCatalog<T>{
private BufferedReader read;
private LinkedList<Course> catalog;
public CourseCatalog(String filename) throws FileNotFoundException {
this.read = new BufferedReader(new FileReader(filename));
catalog = new LinkedList<Course>();
}
public LinkedList<Course> getCatalog() {
return this.catalog;
}
public String toString() {
Iterator<Course> itr = catalog.iterator();
StringBuffer str = new StringBuffer();
Course course = null;
str.append("{\n");
while(itr.hasNext()) {
course = itr.next();
str.append("\n");
str.append(course);
}
str.append("}");
return str.toString();
}
}
Code:
package courses;
public class CourseManager<T> {
private T[] array;
private int size;
public CourseManager(CourseCatalog catalog) {
array =(T[]) new Object();
size =0;
}
public String[] allowedCourses(){
/*
* Returns courses that can be taken based on the
* course list. This should not include courses already on the
* course list. This should include courses that have no prerequisites,
* except for courses already on the course list.
*/
if((String[]) array[size] != null);
return null;
}
public boolean add(String courseName){
/*
* Returns true if the course was successfully
* added to the list. Returns false if the course
* cannot be added.
*/
if(size()<array.length){
array[size]=(T) courseName;
size++;
return true;
}else{
return false;
}
}
public boolean remove(String courseName){
/*
* Returns true if the course was successfully
* removed fron the list. Returns false if the
* course cannot be removed.
*/
for(int i=0;i<size;i++){
if(courseName.equals(array[i])){
array[i]=array[size-1];
size--;
return true;
}else{
return false;
}
}
return false;
}
public String[] getCourseList(){
/*
* Return the courses in the course list.
* The course list field in a CourseManager object should
* be a linked list of Course objects. This method should
* return the courses as a String array.
*/
return (String[]) array[size];
}
public String[] getPrerequisites(String courseName){
/*
* Returns the prerequisites of the given course.
*/
return getPrerequisites(courseName);
}
public String[] getSubsequents(String courseName){
/*
* Returns the courses for which this course is a prerequisite.
*/
return getSubsequents(courseName);
}
public String toString(){
/*
* Returns a String with all courses in course list
*/
return null;
}
String toString(String courseName){
/*
* Returns verbose String description of the course.
*/
return null;
}
public int size(){
return 0;
}
}