Question

In: Computer Science

I. General Description In this assignment, you will create a Java program to read undergraduate and...

I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, and write them in reverse order to an output file. 1. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. For example, assume your package name is FuAssign5 and your main class name is FuAssignment5, and your executable files are in “C:\Users\2734848\eclipse-workspace\CIS 265 Assignments\bin”. The following command line will read from a local file “students.txt” and write to a local file “students_reversed.txt”: C:\Users\2734848\eclipse-workspace\CIS 265 Assignments\bin > java FuAssign5.FuAssignment5 students.txt students_reversed.txt 2. You need to specify the command line arguments if you use jGrasp or Eclipes. I will show you on jGrasp on Wed. during the lab. 3. The parameter String[] args in the main method contains the command line arguments, where args[0] contains the first argument. For example, in the previous command, args[0] contains students.txt, and args[1] contains students_reversed.txt. 4. If the program is run with incorrect number of arguments, your program must print an error message and exit. The error message must show correct format to run your program, e.g., “Usage: FuAssign5.FuAssignment5 input_file output_file” where FuAssign5 is the package and FuAssignment5 is the main class. 5. Each line in the input file represents a student. There are 5 fields in each line: name, id, gpa, “graduate” or “undergraduate”, isTransfer (for undergraduate) or college (for graduate). The fields are separated by comma, “,”. For example, the input file students.txt file may contain: Michelle Chang,200224,3.3,graduate,Cleveland State University Tayer Smoke,249843,2.4,undergraduate,false David Jones,265334,2.7,undergraduate,true Abby Wasch,294830,3.6,graduate,West Virginia 6. The program will read the lines and create undergraduate students or graduate students accordingly. The students are added to an ArrayList. 7. The program then writes the list of students in reserve order to the output file. 8. Given the previous input file students.txt, the output file, students_reserved.txt, will be: Abby Wasch,294830,3.6,graduate,West Virginia David Jones,265334,2.7,undergraduate,true Tayer Smoke,249843,2.4,undergraduate,false Michelle Chang,200224,3.3,graduate,Cleveland State University II. Implementation Requirements The program must implement a main class and three student classes (Student, UndergradStudent, GradStudent). • You may reuse the code from previous assignments. 2 • However, the Student class must be declared as an abstract class now. It must also have an overloaded printStudent(PrintWriter output) method. The method will write student’s information to the output file using the PrintWriter output. • Accordingly, the UnderGradStudent and GradStudent classes must also have an overloaded printStudent(PrintWriter output) method. They must use the superclass’ method to write student’s information. The UnderGradStudent’s printStudent(PrintWriter output) writes “undergraduate” and isTransfer after that. The GradStudent’s printStudent(PrintWriter output) writes “graduate” and college after that. • The UML class diagram should be as follows. • All classes must be in the same package. The package name must start with your last name. For example, if your last name is “Trump”, your package name must start with “Trump” such as “TrumpCIS265AS3”, “TrumpAS3”, etc. • You main class file name must start with your last name. For example, if your last name is “Fu”, your main class file name must start with “Fu” such as FuAssignment5.java. • Since I/O exceptions are checked exceptions, your program must handle exceptions. You may use the try/catch or throw IOException. To throw exceptions, you declare it as: public static void main(String[] args) throws IOException { • You must create an ArrayList of Students in the main class: import java.util.ArrayList; ArrayList students = new ArrayList<>(); The ArrayList students will store all Student objects created, both undergraduate students and graduate students. • The Student class must have a constructor that takes a name, an id, and a gpa, to create a Student object. • The UndergradStudent class must have a constructor that takes a name, an id, a gpa, and a transfer status to create an UndergradStudent object. The constructor must call the Student’s constructor using super. • The GradStudent class should must a constructor that takes a name, an id, a gpa, and a college to create a GradStudent object. The constructor must call the Student’s constructor using super. Student -name: String -id: int -gpa: float +Student() +Student(name,id,gpa) +pringStudent():void +getID():int +printStudent(PrintWriter output):void UndegradStudent -boolean: isTransfer +UndergradStudent(name,id,gpa,isTransfer) +pringStudent():void +printStudent(PrintWriter output):void GradStudent -college:String +GradStudent(name,id,gpa,college) +pringStudent():void +printStudent(PrintWriter output):void 3 • The Student class must have a public method printStudent(PrintWriter output) that writes the student’s name, id, and gpa to the PrintWriter output. • The UndergradStudent class must override the printStudent(PrintWriter output) method. It must write the student’s name, id, gpa, and transfer status to the PrintWriter output. It should call the Student’s printStudent(PrintWriter output) to write student’s name, id, and gpa. • The gradStudent class must override the printStudent(PrintWriter output) method. It must write the student’s name, id, gpa, and college to the PrintWriter output. It should call the Student’s printStudent(PrintWriter output) to write student’s name, id, and gpa. • Your program must use dynamic binding to invoke the correction printStudent(PrintWriter output) method. • Your program must close both input and output files after they are done. • You can assume that input file has the correct format. You will earn bonus points for handling incorrect input formats. III. Submission This is an individual assignment. Each student needs to submit the source code files of the Java program on Blackboard. 1. Put all you Java files in a folder. Please name the folder after your package name, for example, FuAssign5. Compress the folder into a .zip file and submit the .zip file. 2. You need to only submit the source code, i.e., the Java files. 3. You may submit multiple time. Your most recent submission before the deadline will be graded. IV. Grading 1. A program that does not run will receive 0 point. 2. There is a 10-20 points deduction for each major error, e.g., missing a student in the output. 3. There is a 1-9 points deduction for each minor error, e.g., a spelling error in printed message. 4. A program that does not follow the guidelines will lose 1-10 points. 5. Any type of cheating is not tolerated. You may be asked to explain your program in person. V. Bonus features (optional) If a line in the input file has incorrect format, your program will print an error message with the line, skip the line and continue. The following are possible formatting errors your program can handle: 1. (2 points) if the line does not have 5 fields; 2. (2 points) if the id is not an integer; 3. (2 points) if the gpa is not a float; 4. (2 points) if the 4th field is not “undergraduate” or “graduate”; 5. (2 points) if the 5th field for an undergraduate student is not true or false. For example, for the following lines in the input file, Sam Jackson,215.22,3.9,graduate,Ohio State University Sam Jackson,215443,22af,graduate,Ohio State University Sam Jackson,215443,3.9,graduate Sam Jackson,215443,3.9,graduate,Ohio State University,cleavland Sam Jackson,215443,3.9,nondegree,Ohio State University Lady Gaga,230940,3.1,undergraduate,unknown 4 You program will print : Invalid input: Sam Jackson,215.22,3.9,graduate,Ohio State University Invalid input: Sam Jackson,215443,22af,graduate,Ohio State University Invalid input: Sam Jackson,215443,3.9,graduate Invalid input: Sam Jackson,215443,3.9,graduate,Ohio State University,cleavland Invalid input: Sam Jackson,215443,3.9,nondegree,Ohio State University Invalid input: Lady Gaga,230940,3.1,undergraduate,unknown The incorrect lines will be ignored and not written to the output file. The corrected formatted lines should be

Solutions

Expert Solution

//////////////////
import java.io.PrintWriter;
public abstract class Student {

// private member

private String name;

private int id;

private float gpa;

public Student(){

this.name="";

this.id=-1;

this.gpa=0;

}

public Student(String n,int i,float g){

this.name=n;

this.id=i;

this.gpa=g;

}

// print student in console

void printStudent(){

System.out.println("Name : "+this.name);

System.out.println("ID : "+this.id);

System.out.println("GPA : "+this.gpa);

}

// return id

public int getID(){

return(this.id);

}

// write in file

public void printStudent(PrintWriter output){

output.print(this.name);

output.print(","+this.id);

output.print(","+this.gpa);

}

}
/////////////
import java.io.PrintWriter;
public class UndergradStudent extends Student {

private boolean isTransfer;

public UndergradStudent(){

// call super class

super();

this.isTransfer=false;

}

// constructor with parameter

public UndergradStudent(String m,int i,float g,boolean b){  

// call super class

super(m,i,g);

this.isTransfer=b;

}  

// print in console

void printStudent(){

super.printStudent();

System.out.println("Under Graduate");

System.out.println("isTransfer : "+this.isTransfer);

}  

// write in file

public void printStudent(PrintWriter output){

super.printStudent(output);

output.print(",undergraduate");

output.println(","+this.isTransfer);

}

}
//////////////////////
import java.io.PrintWriter;
public class GradStudent extends Student {

private String college;

public GradStudent(){

// call super class

super();

this.college="";

}

// constructor

public GradStudent(String m,int i,float g,String c){  

// call super class

super(m,i,g);

this.college=c;

}

// print in console

void printStudent(){

super.printStudent();

System.out.println("Graduate");

System.out.println("College : "+this.college);

}

// write in file

public void printStudent(PrintWriter output){

super.printStudent(output);

output.print(",graduate");

output.println(","+this.college);

}

}
//////////////////

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

public class StdentDriver {

public static void main(String[] args) throws FileNotFoundException, IOException {

if(args.length!=2){

System.out.println("Error in input\n<input file name> <output file name>");

return;

}

FileReader f = new FileReader(args[0]);

BufferedReader b = new BufferedReader(f);

String line="";

ArrayList<Student> students=new ArrayList<Student>();

// input from file

while((line=b.readLine())!=null){

String [] spt=line.split(",");  

if(spt[3].equals("graduate")==true){

students.add(new GradStudent(spt[0],Integer.parseInt(spt[1]),Float.parseFloat(spt[2]),spt[4]));

}

if(spt[3].equals("undergraduate")==true){

students.add(new UndergradStudent(spt[0],Integer.parseInt(spt[1]),Float.parseFloat(spt[2]),Boolean.parseBoolean(spt[4])));

}

}

b.close();

System.out.println("Readding Input Complete");

System.out.println("Print in file and console");

// output to file

FileWriter writer= new FileWriter(args[1]);

PrintWriter outWriter = new PrintWriter(writer);

for(int i=students.size()-1;i>=0;i--){

System.out.println("\nRecord "+(students.size()-i));

// get instance of object

if(students.get(i) instanceof UndergradStudent){

UndergradStudent s=(UndergradStudent) students.get(i);

s.printStudent();

s.printStudent(outWriter);

}

if(students.get(i) instanceof GradStudent){

GradStudent s=(GradStudent) students.get(i);

s.printStudent();

s.printStudent(outWriter);

}

}

outWriter.close();

}

}

please do upvote thank you.


Related Solutions

I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
I. General Description In this assignment, you will create a Java program to search recursively for...
I. General Description In this assignment, you will create a Java program to search recursively for a file in a directory. • The program must take two command line parameters. First parameter is the folder to search for. The second parameter is the filename to look for, which may only be a partial name. • If incorrect number of parameters are given, your program should print an error message and show the correct format. • Your program must search recursively...
Java program In this assignment you are required to create a text parser in Java/C++. Given...
Java program In this assignment you are required to create a text parser in Java/C++. Given a input text file you need to parse it and answer a set of frequency related questions. Technical Requirement of Solution: You are required to do this ab initio (bare-bones from scratch). This means, your solution cannot use any library methods in Java except the ones listed below (or equivalent library functions in C++). String.split() and other String operations can be used wherever required....
Description: The purpose of the program is to create a Java ticket purchasing program that allows...
Description: The purpose of the program is to create a Java ticket purchasing program that allows for users to log in, purchase tickets, keep records of tickets purchased, and keep information about the user. Program Requirements: The following are the program requirements: Must be fully functioning including registration, log-in, view events, and purchase tickets Tickets should be purchased using “points”, no information should be provided via the user for payment method. Default each user to an allotted number of points...
In this assignment, you shall create a complete C++ program that will read from a file,...
In this assignment, you shall create a complete C++ program that will read from a file, "studentInfo.txt", the user ID for a student (first letter of their first name connected to their last name Next it will need to read three integer values that will represent the 3 exam scores the student got for the semester. Once the values are read and stored in descriptive variables it will then need to calculate a weighted course average for that student. Below...
Java Programming - please read the description I need this description. Inside a do/ while loop...
Java Programming - please read the description I need this description. Inside a do/ while loop (while choice !='x') , create a method call that calls a switch menu with 3 cases for variable choice and X for exit application.(this is a user input) Each case calls other methods. One of those methods asks for user input float numbers to calculate addition. And the result return to another method that displays the result. Thanks for your help! Thanks for asking....
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then,...
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then, (b) add items onto the stack. (c) Print the stack contents. (d) Remove an item off the stack, and (e) print the removed item. At the end, (f) print the new stack contents: Please use the following comments in your python script: # ************************************************* # COP3375 # Student's full name ( <<< your name goes here) # Week 8: Assignment 1 # ************************************************* #...
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT