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 students.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

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

StudentTest.java

package c15;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

class Student{
   private String name;
   private int ID;
   private double marks;

   public Student(String name, int iD, double marks) {
       super();
       this.name = name;
       ID = iD;
       this.marks = marks;
   }

   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public int getID() {
       return ID;
   }
   public void setID(int iD) {
       ID = iD;
   }
   public double getMarks() {
       return marks;
   }
   public void setMarks(double marks) {
       this.marks = marks;
   }
}

class UndergradStudent extends Student{
   private String status;
   private String isUnderGraduate;

   public UndergradStudent(String name, int iD, double marks,String s,String s2) {
       super(name,iD,marks);
       this.status = s;
       this.isUnderGraduate = s2;
   }

   public UndergradStudent(String array[]) {
       super(array[0],Integer.parseInt(array[1]),Double.parseDouble(array[2]));
       this.status = array[3];
       this.isUnderGraduate = array[4];
   }

   public String getStatus() {
       return status;
   }

   public void setStatus(String status) {
       this.status = status;
   }

   public String getIsUnderGraduate() {
       return isUnderGraduate;
   }

   public void setIsUnderGraduate(String isUnderGraduate) {
       this.isUnderGraduate = isUnderGraduate;
   }
  
}

class GradStudent extends Student{
   private String status;
   private String clg;

   public GradStudent(String name, int iD, double marks,String s,String s2) {
       super(name,iD,marks);
       this.status = s;
       this.clg = s2;
   }

   public GradStudent(String array[]) {
       super(array[0],Integer.parseInt(array[1]),Double.parseDouble(array[2]));
       this.status = array[3];
       this.clg = array[4];
   }

   public String getStatus() {
       return status;
   }

   public void setStatus(String status) {
       this.status = status;
   }

   public String getClg() {
       return clg;
   }

   public void setClg(String clg) {
       this.clg = clg;
   }
  
}


class StudentIDComparator implements Comparator<Student> {
   @Override
   public int compare(Student o1, Student o2) {
       if(o1.getID()>o2.getID()) {
           return 1;
       }else if(o1.getID()<o2.getID()) {
           return -1;
       }
       return 0;
   }
}

public class StudentTest {
   public static void main(String[] args) {
       File file = new File("student.txt"); //reading data from this file
       Scanner reader;
       String line="";
       try {
           //read file using scanner
           reader = new Scanner(file);
           ArrayList<UndergradStudent> undergraduateStudent = new ArrayList<UndergradStudent>();
           ArrayList<GradStudent> graduateStudent = new ArrayList<GradStudent>();
           while(reader.hasNextLine()){
               //read line
               line = reader.nextLine();
               String data[] = line.split(",");
               if(data[3].equalsIgnoreCase("graduate")) {
                   graduateStudent.add(new GradStudent(data));
               }else {
                   undergraduateStudent.add(new UndergradStudent(data));
               }
           }
           System.out.println("Successfully loaded "+graduateStudent.size()+" Graduate records.");
           System.out.println("Successfully loaded "+undergraduateStudent.size()+" Under Graduate records.");
          
           StudentIDComparator idSorter = new StudentIDComparator();
           Collections.sort(undergraduateStudent, idSorter); //students is an arrayList of Students
           Collections.sort(graduateStudent, idSorter); //students is an arrayList of Students
          
          
           PrintWriter outputFile = new PrintWriter("GraduateStudent.txt");
           for(GradStudent tmp : graduateStudent) {
               outputFile.write(tmp.getName()+" "+tmp.getID()+" "+tmp.getMarks()+" "+tmp.getStatus()+" "+tmp.getClg()+"\n");
           }
           outputFile.close();
           System.out.println("Data exported successfully for GraduateStudent!!!");
          
           PrintWriter outputFile2 = new PrintWriter("UnderGraduateStudent.txt");
           for(UndergradStudent tmp : undergraduateStudent) {
               System.out.println(tmp);
               outputFile2.write(tmp.getName()+" "+tmp.getID()+" "+tmp.getMarks()+" "+tmp.getStatus()+" "+tmp.getIsUnderGraduate()+"\n");
           }
           outputFile2.close();
           System.out.println("Data exported successfully for UnderGraduateStudent!!!");
          
          
       } catch (FileNotFoundException e) {
           System.out.println("file not found");
       }
   }

}

output

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

GraduateStudent.txt

Michelle Chang 200224 3.3 graduate Cleveland State University
Sam Jackson 215443 3.9 graduate Ohio State University
Nancy Drew 244833 2.9 graduate Case Western
Abby Wasch 294830 3.6 graduate West Virginia

UnderGraduateStudent.txt

James Bond 200304 3.2 undergraduate true
Lady Gaga 230940 3.1 undergraduate false
Tayer Smoke 249843 2.4 undergraduate false
David Jones 265334 2.7 undergraduate 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....
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