Question

(1) Objective: Create java Class to implement Iterable interface (2 points) Download Course.java (a) Write a java class that implements Iterable interface to Read the input file CourseForProgram.txt which contains course records (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 dont 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;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is your code: -

CourseIterableMain.java

public class CourseIterableMain {

/**

* @param args

* the command line arguments

*/

public static void main(String[] args) {

BufferedReader br = null;

String line = "";

CourseIterator clist = new CourseIterator();

try {

br = new BufferedReader(new FileReader(new File("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(CourseIterableMain.class.getName()).log(Level.SEVERE, null, ex);

} catch (IOException ex) {

Logger.getLogger(CourseIterableMain.class.getName()).log(Level.SEVERE, null, ex);

} finally {

try {

br.close();

} catch (IOException ex) {

Logger.getLogger(CourseIterableMain.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

}

Course.java

/**

*

* 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;

}

}

CourseIterator.java

public class CourseIterator 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;

}

}

}

Add a comment
Know the answer?
Add Answer to:
Course.java import java.io.Serializable; /** * Represents a course that might be taken by a student. *...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Course,java import java.util.ArrayList; import java.util.Arrays; /* * To change this license header, choose License Headers in...

    Course,java import java.util.ArrayList; import java.util.Arrays; /* * 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. */ /** * * @author fenghui */ public class Course {     private String cName;     private ArrayList<Subject> cores;     private ArrayList<Major> majors;     private ArrayList<Subject> electives;     private int cCredit;          public Course(String n, int cc){         cName = n;         cCredit = cc;         cores = new ArrayList<Subject>(0);         majors =...

  • Write a class called Course_xxx that represents a course taken at a school. Represent each student...

    Write a class called Course_xxx that represents a course taken at a school. Represent each student using the Student class from the Chapter 7 source files, Student.java. Use an ArrayList in the Course to store the students taking that course. The constructor of the Course class should accept only the name of the course. Provide a method called addStudent that accepts one Student parameter. Provide a method called roll that prints all students in the course. Create a driver class...

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

  • There is a data structure called a drop-out stack that behaves like a stack in every...

    There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...

  • Methods enforced by the set interface: import java.util.Iterator; public interface Set<E> { /** Removes all of...

    Methods enforced by the set interface: import java.util.Iterator; public interface Set<E> { /** Removes all of the elements from this set */ void clear(); /** Returns true if this set contains no elements @return true if this set contains no elements */ boolean isEmpty(); /** Returns the number of elements in this set (its cardinality) @return the number of elements in this set */ int size(); /** Returns an iterator over the elements in this set @return an iterator over...

  • PLEASE COMPLETE THE CODES. package javaprogram; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; /** * Movie class...

    PLEASE COMPLETE THE CODES. package javaprogram; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; /** * Movie class definition. * * @author David Brown * @version 2019-01-22 */ public class Movie implements Comparable { // Constants public static final int FIRST_YEAR = 1888; public static final String[] GENRES = { "science fiction", "fantasy", "drama", "romance", "comedy", "zombie", "action", "historical", "horror", "war" }; public static final int MAX_RATING = 10; public static final int MIN_RATING = 0; /** * Converts a string of...

  • How to complete this methods: import java.io.FileInputStream; import java.io.FileNotFoundException; import java.time.Loc...

    How to complete this methods: import java.io.FileInputStream; import java.io.FileNotFoundException; import java.time.LocalDate; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; /** * Utility class that deals with all the other classes. * * @author EECS2030 Summer 2019 * */ public final class Registrar {    public static final String COURSES_FILE = "Courses.csv";    public static final String STUDENTS_FILE = "Students.csv";    public static final String PATH = System.getProperty("java.class.path");    /**    * Hash table to store the list of students using their...

  • Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /**...

    Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /** Removes all of the elements from this set */ void clear(); /** Returns true if this set contains no elements @return true if this set contains no elements */ boolean isEmpty(); /** Returns the number of elements in this set (its cardinality) @return the number of elements in this set */ int size(); /** Returns an iterator over the elements in this set @return...

  • Requirements:  Your Java class names must follow the names specified above. Note that they are...

    Requirements:  Your Java class names must follow the names specified above. Note that they are case sensitive. See our template below.  BankAccount: an abstract class.  SavingAccount: a concrete class that extends BankAccount. o The constructor takes client's firstname, lastname and social security number. o The constructor should generate a random 6-digit number as the user account number. o The initial balance is 0 and the annual interest rate is fixed at 1.0% (0.01).o The withdraw method signature...

  • The following is for java programming. the classes money date and array list are so I are are pre...

    The following is for java programming. the classes money date and array list are so I are are pre made to help with the coding so you can resuse them where applicable Question 3. (10 marks) Here are three incomplete Java classes that model students, staff, and faculty members at a university class Student [ private String lastName; private String firstName; private Address address; private String degreeProgram; private IDNumber studentNumber; // Constructors and methods omitted. class Staff private String lastName;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT