Question

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 assignment 5, the Student objects are sorted before they are written to the output file. • The program must implement a main class, three student classes (Student, UndergradStudent, GradStudent), and a Comparator class called StudentIDComparator. • The StudentIDComparator class must implement the java.util.Comparator interface, and override the compare() method. Since the Comparator interface is a generic interface, you must specify Student as the concrete type. The signature of the compare method should be public int compare(Student s1, Student s2). The compare() method returns a negative, 0, or positive value if s1 is less than, equals, or is greater than s2, respectively. • To sort the ArrayList, you need to create a StudentIDComparator object and use it in the Collections’ sort method: StudentIDComparator idSorter = new StudentIDComparator(); Collections.sort(students, idSorter); //students is an arrayList of Students

IV. Bonus features (optional) You may implement as many bonus features as you want. If you implement any bonus feature in your program, please put a comment in your Blackboard submission and in your main Java file. In your comments, explain which bonus feature(s) you implemented.

2 1. (10 points) Your program will ask the user which data field is used to sort the students, name, ID, or gpa. It will then sort the students accordingly and write the sorted list to an output file provided at command line. A sample run may look like this:

Which field should be used to sort Students(1-3):

1. Name

2. ID

3. GPA 0

Which field should be used to sort Students(1-3):

1. Name

2. ID 3.

GPA 4

Which field should be used to sort Students(1-3):

1. Name

2. ID

3. GPA

Homework Answers

Answer #1

Java Code:

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

Screenshot:

Please give me thumbs up,if you like the answer .
Please let me know if you don't understand any step before giving Down rating directly :). Happy to help....

Know the answer?
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
Similar Questions
The class name is StudentTest and the filename is StudentTest.java. The program must do the following...
The class name is StudentTest and the filename is StudentTest.java. The program must do the following (all in main): The program must create three Student objects using the Student constructor. Use literal data for the arguments to the constructor for the first two Students, and variables for the third Student. Prompt for and input the first name, last name and GPA for the third Student object. The program must display the name and GPA for each Student. Use printf and...
Write a Java Program, that opens the file "students.txt" The program must read the file line...
Write a Java Program, that opens the file "students.txt" The program must read the file line by line The program parses each line that it reads For example, for this line: 1:mohamed:ali:0504123456:cs102:cs202 The program must print    >ID = 1    >First Name = Mohamed   >Last Name = Ali   >Mobie = 0504123456   >Courses = cs102, cs202 In addition, it adds the mobile phone number into an ArrayList called studentPhoneList Print the content and the size of studentPhoneList Show your results and provide...
Write a program that sorts the content of a map by Values. Before we start, we...
Write a program that sorts the content of a map by Values. Before we start, we need to check the explanation of Java Map Interface and Java LinkedHashMap. This program can be implemented as follows: 1. Create a LinkedHashMap class <String (Country), String(Capital)> that stores Capitals of FIVE countries. 2. Use sortMap() for receiving content of the created class of LinkedHashMap and reorder it in a sorted Map. (sort in natural order). 3. You need to transfer the map into...
Using JAVA For this assignment, you will analyze code that uses a file input stream and...
Using JAVA For this assignment, you will analyze code that uses a file input stream and a file output stream. Read through the linked Java™ code. In a Microsoft® Word document, answer the following questions: Could this program be run as is? If not, what is it lacking? Does this program modify the contents of an input stream? In what way? What are the results of running this code? ********************************************** CODE TO ANALYZE  ******************************************************** /********************************************************************** *   Program:   Datasort *   Purpose:   ...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you start doing anything… This project involves implementing a simple university personnel management program. The program contains two different kinds of objects: students and faculty. For each object, the program stores relevant information such as university ID, name, etc. Different information is stored depending on the type of the object. For example, a student has a GPA, while a faculty has a title and department...
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
Write a complete and syntactically correct Python program to solve the following problem: Write a program...
Write a complete and syntactically correct Python program to solve the following problem: Write a program for your professor that allows him to keep a record of the students’ average grade in his class. The program must be written in accordance with the following specs: 1. The input must be interactive from the keyboard. You will take input for 12 students. 2. You will input the students’ name and an average grade. The student cannot enter an average below zero...
You are required to write a program in JAVA based on the problem description given. Read...
You are required to write a program in JAVA based on the problem description given. Read the problem description and write a complete program with necessary useful comment for good documentation. Compile and execute the program. ASSIGNMENT OBJECTIVES: • To introduce queue data structure. DESCRIPTIONS OF PROBLEM: Exercise : Write a program to reverse element of a stack. For any given word (from input), insert every character (from the word) into a stack. The output from the stack should be...
Assignment Overview This programming exercise introduces generics and interfaces. The students must create methods that accept...
Assignment Overview This programming exercise introduces generics and interfaces. The students must create methods that accept generic parameters and perform operation on them. Deliverables A listing of the fully commented, working source code of the Java program Test data for the code A screen shot of the application in execution Step 1 Create a new project. Name it "Assignment_2_1". Step 2 Build a solution. Write the Java source code necessary to build a solution for the problem below:You have just...
I need to create a UNIX program for this assignment In this assignment, you are to...
I need to create a UNIX program for this assignment In this assignment, you are to create an "archive" utility. It is a simple way to have some version control. You've created software projects before that change and grow over time. You might have several different copies of it (let's assume that it's all in one file). Perhaps before making major changes, you make a back-up copy, in case you later decide to revert to the earlier version. The idea...