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, 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

Heres student.txt

James Bond,200304,3.2,undergraduate,true
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
Nancy Drew,244833,2.9,graduate,Case Western
Lady Gaga,230940,3.1,undergraduate,false
Sam Jackson,215443,3.9,graduate,Ohio State University

He said you would need aorund 5 classes

Solutions

Expert Solution

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


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, 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...
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....
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...
Java program. Need to create a class names gradesgraph which will represent the GradesGraphtest program. Assignment...
Java program. Need to create a class names gradesgraph which will represent the GradesGraphtest program. Assignment Create a class GradesGraph that represents a grade distribution for a given course. Write methods to perform the following tasks: • Set the number of each of the letter grades A, B, C, D, and F. • Read the number of each of the letter grades A, B, C, D, and F. • Return the total number of grades. • Return the percentage of...
Java Program Problem // Description: Read five positive integers and average them. // Enter a negative...
Java Program Problem // Description: Read five positive integers and average them. // Enter a negative integer to end the program. import java.util.Scanner; public class Lab5 { public static void main (String args[]) {        Scanner sc;        sc = new Scanner(System.in);    final int MAX_NUMS = 5;        int num;       // variable to store the number        int sum;   // variable to store the sum        int count;    // variable to store...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT