In: Computer Science
Well our Data Structures and Algorithms professor had the incorrect date set for this assignment (was November 7, 2019 and now is October 7, 2019) so I was putting it off until the end of this month, now I have two days to complete it which is going to be near impossible with the assignments I have for C Programming, Calculus II and Anthropology also all due on Monday, so I need some help. I have the general outline for this program from the last assignment we did but I am unsure how to implement the new methods we were shown Thursday.
I. General Description
In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, shuffle them, and write them 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 FuAssignment4 and your main class name is FuMain, 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_shuffled.txt”: C:\Users\2734848\eclipse-workspace\CIS 265 Assignments\bin > java FuAssignment4.FuMain students.txt students_shuffled.txt
2. 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: FuAssignment4.FuMain input_file output_file” where FuAssignment4 is the package and FuMain is the main class.
3. 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
4. The program will read the lines and create undergraduate students or graduate students accordingly. The students are added to an ArrayList.
5. The program then shuffles the ArrayList, and write the shuffled list of students to the output file.
6. Given the previous input file students.txt, a possible output file, students_shuffled.txt, maybe like this: Abby Wasch,294830,3.6,graduate,West Virginia Michelle Chang,200224,3.3,graduate,Cleveland State University David Jones,265334,2.7,undergraduate,true Tayer Smoke,249843,2.4,undergraduate,false
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. You may use my code posted on Blackboard for previous assignments.
• 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 file using the output PrintWriter.
• 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 2 isTransfer after that. The GradStudent’s printStudent(PrintWriter output) writes “graduate” and college after that. • The UML class diagram should be as follows:
Student | ||
-name: String | ||
-id: int | ||
-gpa: float | ||
+Student() | ||
+Student(name,id,gpa) | ||
+printStudent():void | ||
+printStudent(PrintWriter output:void | ||
UndergradStudent | GradStudent | |
-boolean: isTransfer | -college:String | |
+UndergradStudent(name,id,gpa,isTransfer) |
+GradStudent(name,id,gpa,college) | |
+printStudent():void +printStudent(PrintWriter output):void |
+printStudent():void +printStudent(PrintWriter output):void |
• 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 “Spiderman”, your main class file name must start with “Spiderman” such as “Spiderman3.java”, “SpidermanAssign3.java”, etc.
• 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 variable 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 an GradStudent object. The constructor must call the Student’s constructor using super.
• 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.
• To shuffle the ArrayList, you need to use the Collections’s shuffle method: import java.util.Collections; Collections.shuffle(students); //assume students is an ArrayList of Students
• The printing of students should use dynamic binding: for (Student s: students) //students is an ArrayList of Students s.printStudent(output); // output is a PrintWriter for output file
• 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.
V. Bonus features (optional)
If a line in the input file has incorrect format, your program should 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.
/***************************Student.java**************************/
package TrumpCIS265AS3;
import java.io.PrintWriter;
/**
* The Class Student.
*/
public abstract class Student {
/** The name. */
private String name;
/** The id. */
private int id;
/** The gpa. */
private float gpa;
/**
* Instantiates a new student.
*/
public Student() {
this.name = "";
this.id = 0;
this.gpa = 0;
}
/**
* Instantiates a new student.
*
* @param name the name
* @param id the id
* @param gpa the gpa
*/
public Student(String name, int id, float gpa) {
super();
this.name = name;
this.id = id;
this.gpa = gpa;
}
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Gets the id.
*
* @return the id
*/
public int getId() {
return id;
}
/**
* Gets the gpa.
*
* @return the gpa
*/
public float getGpa() {
return gpa;
}
/**
* Prints the student.
*/
public abstract void printStudent();
/**
* Prints the student.
*
* @param output the output
*/
public abstract void printStudent(PrintWriter
output);
}
/**********************************GradStudent.java***********************/
package TrumpCIS265AS3;
import java.io.PrintWriter;
/**
* The Class GradStudent.
*/
public class GradStudent extends Student {
/** The college. */
private String college;
/**
* Instantiates a new grad student.
*
* @param name the name
* @param id the id
* @param gpa the gpa
* @param college the college
*/
public GradStudent(String name, int id, float gpa,
String college) {
super(name, id, gpa);
this.college = college;
}
/**
* Gets the college.
*
* @return the college
*/
public String getCollege() {
return college;
}
@Override
public void printStudent() {
System.out.println("Student
name: " + getName());
System.out.println("Student id: " +
getId());
System.out.println("Student GPA: "
+ getGpa());
System.out.println("Student
College: " + college);
}
/*
* (non-Javadoc)
*
* @see
TrumpCIS265AS3.Student#printStudent(java.io.PrintWriter)
*/
@Override
public void printStudent(PrintWriter output) {
output.write(getName() + "," +
getId() + "," + getGpa() + "," + getCollege());
output.write("\n");
output.flush();
}
}
/***************************************UndergradStudent.java******************************/
package TrumpCIS265AS3;
import java.io.PrintWriter;
/**
* The Class UndergradStudent.
*/
public class UndergradStudent extends Student {
/** The is transfer. */
private boolean isTransfer;
/**
* Instantiates a new undergrad student.
*
* @param name the name
* @param id the id
* @param gpa the gpa
* @param isTransfer the is transfer
*/
public UndergradStudent(String name, int id, float
gpa, boolean isTransfer) {
super(name, id, gpa);
this.isTransfer = isTransfer;
}
/**
* Checks if is transfer.
*
* @return true, if is transfer
*/
public boolean isTransfer() {
return isTransfer;
}
/*
* (non-Javadoc)
*
* @see TrumpCIS265AS3.Student#printStudent()
*/
@Override
public void printStudent() {
System.out.println("Student
name: " + getName());
System.out.println("Student id: " +
getId());
System.out.println("Student GPA: "
+ getGpa());
System.out.println("Student
College: " + isTransfer);
}
/*
* (non-Javadoc)
*
* @see
TrumpCIS265AS3.Student#printStudent(java.io.PrintWriter)
*/
@Override
public void printStudent(PrintWriter output) {
output.write(getName() + "," +
getId() + "," + getGpa() + "," + isTransfer);
output.write("\n");
output.flush();
}
}
/**************************************SpidermanAssign3.java*******************/
package TrumpCIS265AS3;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/**
* The Class SpidermanAssign3.
*/
public class SpidermanAssign3 {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
try {
Scanner scan =
new Scanner(new File("students.txt"));
while
(scan.hasNextLine()) {
String line = scan.nextLine();
String[] data = line.split(",");
String name = data[0];
int id = Integer.parseInt(data[1]);
float gpa = Float.parseFloat(data[2]);
String type = data[3];
boolean isTransfer;
String college;
if (type.equalsIgnoreCase("graduate")) {
college = data[4];
students.add(new
GradStudent(name, id, gpa, college));
} else {
isTransfer =
Boolean.parseBoolean(data[4]);
students.add(new
UndergradStudent(name, id, gpa, isTransfer));
}
}
scan.close();
} catch (Exception e) {
}
Collections.shuffle(students);
for (Student student : students) {
student.printStudent();
}
try {
PrintWriter
writer = new PrintWriter(new
FileWriter("students_shuffled.txt"));
for (Student
student : students) {
student.printStudent(writer);
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/***************output*******************/
Student name: Tayer Smoke
Student id: 249843
Student GPA: 2.4
Student College: false
Student name: Michelle Chang
Student id: 200224
Student GPA: 3.3
Student College: Cleveland State University
Student name: Abby Wasch
Student id: 294830
Student GPA: 3.6
Student College: West Virginia
Student name: David Jones
Student id: 265334
Student GPA: 2.7
Student College: true
Please let me know if you have any doubt or modify the answer, thanks :)