Objective: ObjectInputStream, ObjectOutputStream,Iterator interface In Client/Se
ID: 3573565 • Letter: O
Question
Objective: ObjectInputStream, ObjectOutputStream,Iterator interface
In Client/Server communication system, ObjectOutputStream and ObjectInputStream can provide an application passing objects between hosts using a (TCP/IP) socket stream or for marshaling and unmarshaling arguments when used with a FileOutputStream and FileInputStream respectively.
https://docs.oracle.com/javase/7/docs/api/java/io/ObjectInputStream.html
CourseForProgram file which was created using ObjectOutputStream contains all course records for the program. Course.java defines the course record format. Use
FileInputStream fis = new FileInputStream(“CourseForProgram”);
ObjectInputStream ois = new ObjectInputStream(fis);
ProgramOfStudy pos = (ProgramOfStudy) ois.readObject();
ois.close();
Download both Course.java and CourseForProgram
Write a main java program to perform following specifications.
Create a class object ProgramOfStudy as
import java.io.*;
import java.util.*;
public class ProgramOfStudy implements Iterable<Course>, Serializable
{
private List<Course> list;
/*
* Constructs an initially empty Program of Study.
*/
public ProgramOfStudy()
{
list = new LinkedList<Course>();
}
/* insert methods for reading the input file, adding courses, */
(b)Implement Iterable interface to display the list of course records from CourseForProgram.
(c)Remove all courses that don’t have grade.
(d)Display the list after removing those records.
Provided Infomation:
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;
}
}
Text:
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
PROGRAM CODE:
ProgramOfStudy.java
package sample;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class ProgramOfStudy implements Iterable<Course>, Serializable
{
private List<Course> list;
/*
* Constructs an initially empty Program of Study.
*/
public ProgramOfStudy()
{
list = new LinkedList<Course>();
}
public void readTextFile(String filename)
{
Scanner input;
try {
input = new Scanner(new File(filename));
while(input.hasNextLine())
{
String courseDetails= input.nextLine();
String tokens[] = courseDetails.split(" ");
String title = "";
Course course;
int lengthOfTokens = tokens.length;
tokens[1] = tokens[1].replace(":", "");
for(int i=2; i<lengthOfTokens-1; i++)
{
title +=tokens[i] + " ";
}
if(tokens[lengthOfTokens-1].contains("[") && tokens[lengthOfTokens-1].contains("]") )
{
tokens[lengthOfTokens-1] = tokens[lengthOfTokens-1].replace("[", "");
tokens[lengthOfTokens-1] = tokens[lengthOfTokens-1].replace("]", "");
course = new Course(tokens[0], Integer.valueOf(tokens[1]), title, tokens[lengthOfTokens-1]);
}
else
{
title+= tokens[lengthOfTokens-1];
course = new Course(tokens[0], Integer.valueOf(tokens[1]), title,"");
}
list.add(course);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void removeCoursesWithoutGrades()
{
for(int i=0; i<list.size(); i++)
{
Course course = list.get(i);
if(!course.taken())
list.remove(course);
}
}
public void displayList()
{
Iterator<Course> itr = iterator();
while(itr.hasNext())
{
System.out.println(itr.next().toString());
}
}
public void readObjectFile()
{
FileInputStream fis;
try {
fis = new FileInputStream(new File("CourseForProgram"));
ObjectInputStream ois = new ObjectInputStream(fis);
ProgramOfStudy pos = (ProgramOfStudy) ois.readObject();
ois.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public Iterator<Course> iterator() {
return new Iterator<Course>() {
int counter = -1;
final int size = list.size();
@Override
public boolean hasNext() {
if(counter<size-1)
return true;
else
return false;
}
@Override
public Course next() {
return list.get(++counter);
}
};
}
}
ProgramOfStudyDriver.java
package sample;
public class ProgramOfStudyDriver {
public static void main(String[] args) {
ProgramOfStudy study = new ProgramOfStudy();
study.readTextFile("courseDetails.txt");
study.removeCoursesWithoutGrades();
study.displayList();
}
}
OUTPUT:
CS 101: Introduction to Programming [A-]
ARCH 305: Building Analysis [A]
FRE 110: Beginning French [B+]
THE 201: The Theatre Experience [A-]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.