Question

Merge the following two text files to another text file named "allstudents.txt". The new file should...

Merge the following two text files to another text file named "allstudents.txt". The new file should be sorted in terms of the students' graduation date from the nearest to furthest. (USING JAVA PROGRAMMING LANGUAGE)

Text file: boys.txt

NAME,ID,GRADUATION_DATE,AGE

1. John, 6, 2020-07-01 10:50:00 EST, 22

2. Mike, 9, 2020-07-07 10:40:00 EST, 23

3. Lucas, 10, 2020-07-07 10:42:00 EST, 23

Text file: girls.txt

NAME,ID,GRADUATION_DATE,AGE

1. Emma, 7, 2020-06-02 10:50:00 EST, 22

2. Olivia, 4, 2020-07-06 10:40:00 EST, 21

3. Sophia, 1, 2020-06-01 10:42:00 EST, 23

The output should be the following:

NAME,ID,GRADUATION_DATE,AGE

1. Sophia, 1, 2020-06-01 10:42:00 EST, 23

2. Emma, 7, 2020-06-02 10:50:00 EST, 22

3.  John, 6, 2020-07-01 10:50:00 EST, 22

4.  Olivia, 4, 2020-07-06 10:40:00 EST, 21

5. Mike, 9, 2020-07-07 10:40:00 EST, 23

6.  Lucas, 10, 2020-07-07 10:42:00 EST, 23

0 0
Add a comment Improve this question Transcribed image text
Answer #1

below is your code: -

Person.java


import java.util.Date;

public class Person implements Comparable<Person> {
   private String name;
   private int ID;
   private Date gradDate;
   private int age;

   public Person(String name, int iD, Date gradDate, int age) {
       this.name = name;
       ID = iD;
       this.gradDate = gradDate;
       this.age = age;
   }

   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 Date getGradDate() {
       return gradDate;
   }

   public void setGradDate(Date gradDate) {
       this.gradDate = gradDate;
   }

   public int getAge() {
       return age;
   }

   public void setAge(int age) {
       this.age = age;
   }

   @Override
   public int compareTo(Person o) {
       if (this.getGradDate().before(o.getGradDate())) {
           return -1;
       } else if (this.getGradDate().after(o.getGradDate())) {
           return 1;
       } else
           return 0;
   }

  
  
}

MergeFiles.java


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TimeZone;

public class MergeFiles {
   public static void main(String[] args) throws FileNotFoundException, ParseException {
       ArrayList<Person> persons = new ArrayList<>();
       Scanner scan1 = new Scanner(new File("boys.txt"));
       Scanner scan2 = new Scanner(new File("girls.txt"));

       scan1.nextLine();
       scan2.nextLine();
       String name, line;
       int id, age;
       Date d;
       String[] words;
       DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
       while (scan1.hasNextLine()) {
           line = scan1.nextLine();
           words = line.split(",");
           name = words[0];
           id = Integer.parseInt(words[1]);
           d = (Date) formatter.parse(words[2]);
           age = Integer.parseInt(words[3]);
           persons.add(new Person(name, id, d, age));
       }
       while (scan2.hasNextLine()) {
           line = scan2.nextLine();
           words = line.split(",");
           name = words[0];
           id = Integer.parseInt(words[1]);
           d = (Date) formatter.parse(words[2]);
           age = Integer.parseInt(words[3]);
           persons.add(new Person(name, id, d, age));
       }

       Collections.sort(persons);
       PrintWriter pw = new PrintWriter(new FileOutputStream("output.txt"));
       int c = 0;
       pw.println("NAME,ID,GRADUATION_DATE,AGE");
       for (Person p : persons)
           pw.println((++c) + ". " + p.getName() + " ," + p.getID() + " ," + formatDateToString(p.getGradDate(),"yyyy-MM-dd HH:mm:ss 'EST'","EST") + " ,"
                   + p.getAge());
       pw.close();
   }

   public static String formatDateToString(Date date, String format, String timeZone) {
       // null check
       if (date == null)
           return null;
       SimpleDateFormat sdf = new SimpleDateFormat(format);
       // default system timezone if passed null or empty
       if (timeZone == null || "".equalsIgnoreCase(timeZone.trim())) {
           timeZone = Calendar.getInstance().getTimeZone().getID();
       }
       sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
       return sdf.format(date);
   }
}

boys.txt

NAME,ID,GRADUATION_DATE,AGE
John,6,2020-07-01 10:50:00 EST,22
Mike,9,2020-07-07 10:40:00 EST,23
Lucas,10,2020-07-07 10:42:00 EST,23

girls.txt

NAME,ID,GRADUATION_DATE,AGE
Emma,7,2020-06-02 10:50:00 EST,22
Olivia,4,2020-07-06 10:40:00 EST,21
Sophia,1,2020-06-01 10:42:00 EST,23

Output.txt

NAME,ID,GRADUATION_DATE,AGE
1. Sophia ,1 ,2020-06-01 10:42:00 EST ,23
2. Emma ,7 ,2020-06-02 10:50:00 EST ,22
3. John ,6 ,2020-07-01 10:50:00 EST ,22
4. Olivia ,4 ,2020-07-06 10:40:00 EST ,21
5. Mike ,9 ,2020-07-07 10:40:00 EST ,23
6. Lucas ,10 ,2020-07-07 10:42:00 EST ,23

Add a comment
Know the answer?
Add Answer to:
Merge the following two text files to another text file named "allstudents.txt". The new file should...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The file containing the JAVA files, pseudocode file and doc file that have written for this...

    The file containing the JAVA files, pseudocode file and doc file that have written for this lab. Preamble The file releasedates.txt contains a list of video games and their release dates. Each line of the file contains the release date, a tab character, and then the name. The list is currently totally unsorted. The object of today's lab is to write a series of methods that allow us to perform the following tasks: read contents from a file and store...

  • Need help with C++ assignment Assignment 1 and .txt files are provided at the bottom. PART...

    Need help with C++ assignment Assignment 1 and .txt files are provided at the bottom. PART A PART B Assignment 1 #include <iostream> #include <string> #include <fstream> #include <iomanip> #include <stdio.h> #include <ctype.h> #include <string.h> #include <algorithm> using namespace std; /** This structure is to store the date and it has three integer fields **/ struct Date{    int day;    int month;    int year; }; /** This structure is to store the size of the box and it...

  • The code should work exactly the same as given example. The files: ---------------------------------------------------...

    The code should work exactly the same as given example. The files: ---------------------------------------------------------------------------------------------------- -------- FILE NAME ------- album0 ---------- FILE ------- <album> 323 Licensed to Ill Beastie Boys 25.0 </album> <album> 70 Enter the Wu_Tang: 36 Cham... Wu Tang Clan 25.99 </album> turning to Alice, she went on, `What's your name, child?' and there stood the Queen in front of them, with her arms folded, <album> 115 Daydream Nation Sonic Youth 5.0 </album> <album> 414 52nd Street Billy Joel 25.75...

  • Refer to Figure 24.11 to answer this question. Suppose you purchase a September 2015 call option...

    Refer to Figure 24.11 to answer this question. Suppose you purchase a September 2015 call option on crude oil futures with a strike price of 5,100 cents per barrel. Assume 1,000 barrels per contract. How much does your option cost per barrel of oil? Option cost per barrel $ What is the total cost? Total cost Suppose the price of oil future is 5,550 cents per barrel at expiration of the option contract. What is your net profit or loss...

  • First create the two text file given below. Then complete the main that is given. There...

    First create the two text file given below. Then complete the main that is given. There are comments to help you. An output is also given You can assume that the file has numbers in it Create this text file: data.txt (remember blank line at end) Mickey 90 Minnie 85 Goofy 70 Pluto 75 Daisy 63 Donald 80 Create this text file: data0.txt (remember blank line at end) PeterPan 18 Wendy 32 Michael 28 John 21 Nana 12 Main #include...

  • Python programming- Download the following two files you will need for this activity: customerData.csv This file...

    Python programming- Download the following two files you will need for this activity: customerData.csv This file contains randomly generated fictitious customer data. customer_regex.py This is a Python script that imports the customer data into a list of customer details. In your personal playground in Codio, upload the two files and investigate the contents before considering the task you will pose to your peers. Assume the position of a manager of an online retailer. Pose a question to your IT expert...

  • Please show how you did this in excel. :13-19 Every home football game for the past...

    Please show how you did this in excel. :13-19 Every home football game for the past eight years at Eastern State University has been sold out. The revenues from ticket sales are significant, but the sale of food, beverages, and souvenirs has contrib- uted greatly to the overall profitability of the football program. One particular souvenir is the football pro- gram for each game. The number of programs sold at each game is described by the following probabil- ity distribution:...

  • Please, clearly state what you are doing. a) Convert the data for each year into an...

    Please, clearly state what you are doing. a) Convert the data for each year into an approximate Average Annual Daily Traffic volume b) Using the survey data, determine the compounding growth rate of the road c) Determine the Average Annual Daily Traffic volume for the opening year of 2020 d) Determine the cumulative compound growth rate (CGR) for the design traffic? Date and Time Ch 1: Eastbound Left Lane Ch 2: Eastbound Right Lane Ch 3: Westbound Right Lane Ch...

  • Jacket Frenzy is a small clothing manufacturer, specializing in creating custom, one-of-a-kind jackets. They exclusively sell...

    Jacket Frenzy is a small clothing manufacturer, specializing in creating custom, one-of-a-kind jackets. They exclusively sell their product through their online store. Jacket Frenzy is run by a team of three staff members: April, Beatrice, and Claudia. April and Beatrice work together to manufacture jackets. Claudia manages orders, sales and delivery. Jacket Frenzy receive requests through their online store, and Claudia responds with a quote, after consulting with a member of the manufacturing team. If the customer confirms their order,...

  • Question 7b - Conduct a Hypothesis Test to determine if there is evidence that the proportion of accounts with Good quality rating is less than 26%. Use significance level alpha=5%. Test Statistic = _...

    Question 7b - Conduct a Hypothesis Test to determine if there is evidence that the proportion of accounts with Good quality rating is less than 26%. Use significance level alpha=5%. Test Statistic = _________ P value = ___________ 377 1 96·33 199 2 98 72 27 98 006 014 717 002 86 997 87 3 7 4 996 7 9 le le le le rede-le Ex Ex Ex Ex Ex Ex Ex Ex ee le le le le en Ma...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT