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

(1) Objective: Create java Class to implement Iterable interface (2 points) (a)

ID: 3818670 • Letter: #

Question

(1)Objective: Create java Class to implement Iterable interface (2 points)

(a)Write a java class that implements Iterable interface to:

(1)Read the input file “CourseForProgram.txt” which contains course records.

(2)Provide add course, remove course and find course methods.

(b)From the main driver program:

(1)Implement Iterable interface to display the list of course records from “CourseForProgram.txt” input file.

(2)Remove all courses that don’t have grade.

(3)Display the list after removing those records.

course.java

import java.io.Serializable;

/**
* Represents a course that might be taken by a student.
*
*/
public class Course implements Serializable
{
   private String prefix;
   private int number;
   private String title;
   private String grade;
  
   /**
   * Constructs the course with the specified information.
   *
   * @param prefix the prefix of the course designation
   * @param number the number of the course designation
   * @param title the title of the course
   * @param grade the grade received for the course
   */
   public Course(String prefix, int number, String title, String grade)
   {
       this.prefix = prefix;
       this.number = number;
       this.title = title;
       if (grade == null)
           this.grade = "";
       else
           this.grade = grade;
   }
  
   /**
   * Constructs the course with the specified information, with no grade
   * established.
   *
   * @param prefix the prefix of the course designation
   * @param number the number of the course designation
   * @param title the title of the course
   */
   public Course(String prefix, int number, String title)
   {
       this(prefix, number, title, "");
   }

   /**
   * Returns the prefix of the course designation.
   *
   * @return the prefix of the course designation
   */
   public String getPrefix()
   {
       return prefix;
   }
  
   /**
   * Returns the number of the course designation.
   *
   * @return the number of the course designation
   */
   public int getNumber()
   {
       return number;
   }
  
   /**
   * Returns the title of this course.
   *
   * @return the prefix of the course
   */
   public String getTitle()
   {
       return title;
   }
  
   /**
   * Returns the grade for this course.
   *
   * @return the grade for this course
   */
   public String getGrade()
   {
       return grade;
   }
  
   /**
   * Sets the grade for this course to the one specified.
   *
   * @param grade the new grade for the course
   */
   public void setGrade(String grade)
   {
       this.grade = grade;
   }
  
   /**
   * Returns true if this course has been taken (if a grade has been received).
   *
   * @return true if this course has been taken and false otherwise
   */
   public boolean taken()
   {
       return !grade.equals("");
   }
  
   /**
   * Determines if this course is equal to the one specified, based on the
   * course designation (prefix and number).
   *
   * @return true if this course is equal to the parameter
   */
   public boolean equals(Object other)
   {
       boolean result = false;
       if (other instanceof Course)
       {
           Course otherCourse = (Course) other;
           if (prefix.equals(otherCourse.getPrefix()) &&
                   number == otherCourse.getNumber())
               result = true;
       }
       return result;
   }
  
   /**
   * Creates and returns a string representation of this course.
   *
   * @return a string representation of the course
   */
   public String toString()
   {
       String result = prefix + " " + number + ": " + title;
       if (!grade.equals(""))
           result += " [" + grade + "]";
       return result;
   }
}

CourseForProgram.txt

CS 101: Introduction to Programming [A-]
ARCH 305: Building Analysis [A]
FRE 110: Beginning French [B+]
CS 320: Computer Architecture
CS 321: Operating Systems
THE 201: The Theatre Experience [A-]

Explanation / Answer


import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestDriver {

   public static void main(String args[]) throws NumberFormatException, IOException {
       CourseManager crsMang = new CourseManager("/home/zubair/Desktop/CourseForProgram.txt");
      
       /* Display the courses */
       crsMang.printCourses();
      
       /*
       * Find the list of non-graded courses
       * */
       Iterator<Course> crsIter = crsMang.iterator();
       List<Course> nonGradedCourses = new ArrayList<Course>();
       while (crsIter.hasNext()) {
           Course course = crsIter.next();
           if (course.getGrade().isEmpty()) {
               nonGradedCourses.add(course);
           }
       }
      
       /*
       * Remove the courses
       */
       for (Course course : nonGradedCourses) {
           crsMang.removeCourse(course);
       }

       /* Display the courses */
       crsMang.printCourses();
   }
}


-------------------------------------------------------------------------------------------------------------------------------

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class CourseManager implements Iterable<Course> {

   protected List<Course> courseList = new ArrayList<>();
  
   public CourseManager(String fileName)
           throws NumberFormatException, IOException {
      
       readFromFile(fileName);
   }
  
   @Override
   public Iterator<Course> iterator() {
       return courseList.iterator();
   }

   public void addCourse(Course course) {
       if (course != null)
           courseList.add(course);
   }
  
   /**
   * Removes the given course object from the list
   *
   * @param inputCourse
   */
   public void removeCourse(Course inputCourse) {
       if (inputCourse == null) {
           return;
       }
      
       int index = -1, i = 0;
       for (Course course : courseList) {
           if (course.equals(inputCourse)) {
               index = i;
               break;
           }
           i++;
       }
       if (index != -1) {
           courseList.remove(index);
       }
   }

   /**
   *
   * Removes the course based on coursePrefix and courseNumber
   *
   * @param prefix
   * @param courseNum
   */
   public void removeCourse(String prefix, int courseNum) {
       int index = -1, i = 0;
       for (Course course : courseList) {
           if (course.getPrefix().equalsIgnoreCase(prefix) && course.getNumber() == courseNum) {
               index = i;
               break;
           }
           i++;
       }
       if (index != -1) {
           courseList.remove(index);
       }
   }
  
   /**
   * Removes the course based on courseTitle
   *
   * @param courseTitle
   */
   public void removeCourse(String courseTitle) {
       int index = -1, i = 0;
       for (Course course : courseList) {
           if (course.getTitle().equalsIgnoreCase(courseTitle)) {
               index = i;
               break;
           }
           i++;
       }
       if (index != -1) {
           courseList.remove(index);
       }
   }
  
   /**
   *
   * finds the course based on coursePrefix and courseNumber
   *
   * @param prefix
   * @param courseNum
   */
   public Course findCourse(String prefix, int courseNum) {
       for (Course course : courseList) {
           if (course.getPrefix().equalsIgnoreCase(prefix) && course.getNumber() == courseNum) {
               return course;
           }
       }
       return null;
   }
  

   /**
   * Finds the course based on courseTitle
   *
   * @param courseTitle
   */
   public Course findCourse(String courseTitle) {
       for (Course course : courseList) {
           if (course.getTitle().equalsIgnoreCase(courseTitle)) {
               return course;
           }
       }
       return null;
   }
  
   /**
   * Reads data from the file and prepares a list of course objects
   *
   * @param filePath
   * @throws NumberFormatException
   * @throws IOException
   */
   public void readFromFile(String filePath) throws NumberFormatException, IOException {
       /*
       * open the file for reading
       * */
       BufferedReader bufferedInput = new BufferedReader(new FileReader(filePath));
      
       /*
       * read each line from file and add it to the List.
       */
       String inputLine;
       while ((inputLine = bufferedInput.readLine()) != null) {
          
           /*
           * Parse each input line and read the attributes
           */
           String coursePrefix = inputLine.substring(0, inputLine.indexOf(' '));
           int courseNum = Integer.valueOf(inputLine.substring(inputLine.indexOf(' ')+1, inputLine.indexOf(':')).trim());
           String courseTitle;
           String grade = "";
           if (inputLine.contains("[")) {
               courseTitle = inputLine.substring(inputLine.indexOf(':')+2, inputLine.indexOf('[')).trim();
               grade = inputLine.substring(inputLine.indexOf('[')+1, inputLine.indexOf(']')).trim();
           }
           else {
               courseTitle = inputLine.substring(inputLine.indexOf(':')+2).trim();
           }

           /*
           * Make a course object and add it to the list
           */
           Course course = new Course(coursePrefix, courseNum, courseTitle, grade);
           courseList.add(course);
       }
       bufferedInput.close();
   }
  
  
   /**
   * Prints the list of courses
   */
   public void printCourses() {
       Iterator<Course> crsIter = iterator();
      
       System.out.println(" List of courses are : ");
       while (crsIter.hasNext()) {
           System.out.println(crsIter.next());
       }
   }
}


--------------------------------------------------------------------------------------------------------------------------


import java.io.Serializable;

/**
* Represents a course that might be taken by a student.
*
*/
public class Course implements Serializable {


   /**
   *
   */
   private static final long serialVersionUID = -3414076556011163077L;
  
   private String prefix;
   private int number;
   private String title;
   private String grade;

   /**
   * Constructs the course with the specified information.
   *
   * @param prefix the prefix of the course designation
   * @param number the number of the course designation
   * @param title the title of the course
   * @param grade the grade received for the course
   */
   public Course(String prefix, int number, String title, String grade) {
       this.prefix = prefix;
       this.number = number;
       this.title = title;
       if (grade == null)
           this.grade = "";
       else
           this.grade = grade;
   }

   /**
   * Constructs the course with the specified information, with no grade
   * established.
   *
   * @param prefix the prefix of the course designation
   * @param number the number of the course designation
   * @param title the title of the course
   */
   public Course(String prefix, int number, String title) {
       this(prefix, number, title, "");
   }

   /**
   * Returns the prefix of the course designation.
   *
   * @return the prefix of the course designation
   */
   public String getPrefix() {
       return prefix;
   }

   /**
   * Returns the number of the course designation.
   *
   * @return the number of the course designation
   */
   public int getNumber() {
       return number;
   }

   /**
   * Returns the title of this course.
   *
   * @return the prefix of the course
   */
   public String getTitle() {
       return title;
   }

   /**
   * Returns the grade for this course.
   *
   * @return the grade for this course
   */
   public String getGrade() {
       return grade;
   }

   /**
   * Sets the grade for this course to the one specified.
   *
   * @param grade
   *            the new grade for the course
   */
   public void setGrade(String grade) {
       this.grade = grade;
   }

   /**
   * Returns true if this course has been taken (if a grade has been
   * received).
   *
   * @return true if this course has been taken and false otherwise
   */
   public boolean taken() {
       return !grade.equals("");
   }

   /**
   * Determines if this course is equal to the one specified, based on the
   * course designation (prefix and number).
   *
   * @return true if this course is equal to the parameter
   */
   public boolean equals(Object other) {
       boolean result = false;
       if (other instanceof Course) {
           Course otherCourse = (Course) other;
           if (prefix.equals(otherCourse.getPrefix()) && number == otherCourse.getNumber())
               result = true;
       }
       return result;
   }

   /**
   * Creates and returns a string representation of this course.
   *
   * @return a string representation of the course
   */
   public String toString() {
       String result = prefix + " " + number + ": " + title;
       if (!grade.equals(""))
           result += " [" + grade + "]";
       return result;
   }
}