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: 3816988 • 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

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaiterable;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author aditya
*/
public class JavaIterable {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        BufferedReader br = null;
        String line = "";
        CourseList clist = new CourseList();
        try {
            br = new BufferedReader(new FileReader(new File("C:\Users\aditya\Documents\CourseForProgram.txt")));
            while ((line = br.readLine()) != null) {
                // System.out.println(line);
                String temp[] = line.split(":");
                String courseCodeandPrefix[] = temp[0].split(" ");
                String descAngGrade[] = temp[1].split(" ");
                String coursePrefix = courseCodeandPrefix[0];
                int courseCode = Integer.parseInt(courseCodeandPrefix[1]);
                String courseDesc = descAngGrade[0];

                String grade = "";
                try {
                    grade = descAngGrade[1].replace("[", "").replace("]", "");
                } catch (ArrayIndexOutOfBoundsException ex) {
                    grade = "";
                }
                // System.out.println("Grade:" + grade);
                Course c = new Course(coursePrefix, courseCode, courseDesc, grade);
                clist.addtoCourseList(c);

            }
            //System.out.print(Arrays.asList(clist.courseArray));
            Iterator it = clist.iterator();
            while (it.hasNext()) {
                Course nextCourse = (Course) it.next();
                //System.out.println("nextCourse:" + nextCourse);
                if (nextCourse != null && (nextCourse.getGrade() == null || nextCourse.getGrade().equals("") || nextCourse.getGrade().equals("null"))) {
                    it.remove();
                }

            }
            Iterator it2 = clist.iterator();
            while (it2.hasNext()) {
                Course nextCourse = (Course) it2.next();
                if (nextCourse != null) {
                    System.out.println(nextCourse);
                }
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(JavaIterable.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(JavaIterable.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                br.close();
            } catch (IOException ex) {
                Logger.getLogger(JavaIterable.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

}

class Course {

    private String prefix;
    private int number;
    private String title;
    private String grade;

    public Course(String prefix, int number, String title, String grade) {
        this.prefix = prefix;
        this.number = number;
        this.title = title;
        this.grade = grade;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    @Override
    public boolean equals(Object obj) {
        Course c = (Course) obj;
        return this.prefix.equals(c.prefix) && this.number == c.number;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 11 * hash + Objects.hashCode(this.prefix);
        hash = 11 * hash + this.number;
        return hash;
    }

    @Override
    public String toString() {
        return "Course{" + "prefix=" + prefix + ", number=" + number + ", title=" + title + ", grade=" + grade + '}';
    }

}

class CourseList implements Iterable<Course> {

    Course[] courseArray = new Course[16]; // Initial Size of 16
    int currIndex = 0;

    public boolean addtoCourseList(Course c) {
        courseArray[currIndex++] = c;
        return true;
    }

    public boolean findCourse(Course c) {
        int i = 0;
        boolean itemFound = false;
        while (i < courseArray.length) {
            if (courseArray[i].equals(c)) {
                itemFound = true;
                break;
            }
        }
        return itemFound;
    }

    @Override
    public Iterator<Course> iterator() {
        return new CourseListIterator();
    }

    private class CourseListIterator implements Iterator<Course> {

        int iteratingIndex = 0;

        @Override
        public boolean hasNext() {
            if (iteratingIndex < courseArray.length) {
                return true;
            } else {
                return false;
            }
        }

        @Override
        public Course next() {
            return courseArray[iteratingIndex++];
        }

        @Override
        public void remove() {
            courseArray[--iteratingIndex] = null;
        }
    }
}