Question: Sort the Dates using a TreeMap. The project will open a file chosen by the user, and display the unsorted dates in the left TextArea,

Sort the Dates using a TreeMap. The project will open a file chosen by the user, and display the unsorted dates in the left TextArea, and the sorted dates from the TreeMap in the right TextArea. I need the following classes, which I will provide, to be modifed to use treemaps. The classes I have already give the output it is supposed to. I just need it to do the same thing, except by using TreeMap. Thank you for any assistance.

Format of input file to be read : Dates.txt

20141001 20080912,20131120,19980927 20020202 20120104

/*Date212 checks parameters

* of input text to

* see if requirements are met.

*/

public class Date212 {

private String monthsInYear[]=

{"","January","February",

"March","April","May",

"June","July","August",

"September","October",

"November","December"};

private int day;//Variable day set to private. Can't be accessed or altered by user.

private String monthString;

private int month;//Variable month set to private. Can't be accessed or altered by user.

private int year;//Variable year set to private. Can't be accessed or altered by user.

public Date212(int y, int m, int d) {

this.year = y;

this.month = m;

this.day = d;

this.monthString = monthsInYear[m];

}

public int getDay() {

return day;

}

public int getMonth() {

return month;

}

public int getYear() {

return year;

}

public void setDay(int d) {

this.day=d;

}

public void setMonth(int m) {

this.month=m;

}

public void setYear(int y) {

this.year=y;

}

public Date212 (String date)

{

/*Calling three argument constructor

* Substring reads the data from the textfile

* and separates them, with years being the first

* 4 numbers, months being the fifth and sixth numbers,

* and days being the seventh and eighth numbers.

*/

this(Integer.parseInt(date.substring(0, 4)), Integer.parseInt(date.substring(4, 6)), Integer.parseInt(date.substring(6, 8)));

}

public String getAsString()

{

return monthString + " " + String.format("%02d", day) + ", " + String.format("%04d", year);

}

/*

* Returns the string representation of the Date212 object

*/

public String toString() {

String date = "";

date += + this.year;

date += this.month < 10 ? "0" + this.month : this.month;

date += this.day < 10 ? "0" + this.day : this.day;

return date;

}

public int compareTo(Date212 other) {

if (this.year < other.year) {

return -1;

} else if (this.year > other.year) {

return 1;

}

if (this.month < other.month) {

return -1;

} else if (this.month > other.month) {

return 1;

}

if (this.day < other.day) {

return -1;

} else if (this.day > other.day) {

return 1;

}

return 0;

}

}

________________________________________________________________________________

/* This class displays

* the dates entered through a text file

* and displays the sorted and unsorted dates

* on a one row, two column GUI. */

import javax.swing.*;

import java.awt.GridLayout;

import java.awt.TextArea;

public class DateGui extends JFrame {

/**

*

*/

private static final long serialVersionUID = 1L;

TextArea unsortedDates, sortedDates;

public DateGui() {

setTitle("Project 4");//Displays the title of the project.

setSize(400, 400);//Sets the size of the GUI ContentPane to an appropriate width and length.

setLocation(100, 100);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

unsortedDates = new TextArea("Unsorted: " + " ");

sortedDates = new TextArea("Sorted: " + " ");//Allows

setLayout(new GridLayout(1, 2)); // Creates the 1 row, 2 column layout

getContentPane().add(unsortedDates);//Allows the unsorted text to be displayed on the left column.

getContentPane().add(sortedDates);//Allows the sorted text to be displayed on the right column.

createFileMenu();

setVisible(true);

}

private void createFileMenu() {

JMenu fileMenu = new JMenu("File"); //creates drop-down menu, File

JMenuBar menuBar = new JMenuBar();

JMenuItem item = new JMenuItem("Open"); //Creates drop-down menu option, open.

FileMenuHandler fmh = new FileMenuHandler(this);

item.addActionListener(fmh);

fileMenu.add(item);

fileMenu.addSeparator(); //Separates Open and Quit on the drop-down menu.

item = new JMenuItem("Quit"); //Creates drop-down menu option, Quit.

item.addActionListener(fmh);

fileMenu.add(item);

setJMenuBar(menuBar);

menuBar.add(fileMenu);//Adds file menu to the menu bar

}

}

________________________________________________________________________________

/*abstract linkedlist class

extends to UnsortedDateList and SortedDateList

*/

public abstract class DateList {

protected DateNode first;//First node in linked list for unsorted.

protected DateNode last;//Last node in linked list for unsorted.

protected DateNode d;

protected DateNode a;

protected DateNode x;//First node in linked list for sorted.

protected DateNode y;//Last node in linked list for sorted.

protected int length;//Number of data items in the list

public abstract void add(Date212 date);

public String toString() {

String listOfDates = "";

d = first.next;

while (d != null) {

listOfDates += d.date.toString() + " ";

d = d.next;

}

return listOfDates;

}

public String getAsString() {

String listOfDates = "";

a = x.next;

while (a != null) {

listOfDates += a.date.getAsString() + " ";

a = a.next;

}

return listOfDates;

}

}

___________________________________________________________________________

//nodes for linkedlists

public class DateNode {

protected Date212 date;

protected DateNode next;

public DateNode(Date212 date) {

this.date = date;

this.next = null;

}//constructor

}

_______________________________________________________________________

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.*;

import javax.swing.JFileChooser;

import java.util.Scanner;

import java.util.StringTokenizer;

public class FileMenuHandler implements ActionListener {

private DateGui gui;

public FileMenuHandler(DateGui gui) {

this.gui = gui;

}

public void actionPerformed(ActionEvent event) {

String menuName = event.getActionCommand();

if (menuName.equals("Open"))

openFile();//Opens file if open is selected.

else if (menuName.equals("Quit"))

System.exit(0);//Exits GUI if quit is selected.

}

private void openFile() {

JFileChooser chooser = new JFileChooser();

int status = chooser.showOpenDialog(null);

if (status == JFileChooser.APPROVE_OPTION)

readSource(chooser.getSelectedFile());//Gets the selected text file.

}

private void readSource(File chosenFile) {

//to read dates from selected file:

Scanner reader;

DateList unsorted = new UnsortedDateList();

DateList sorted = new SortedDateList();

try {

reader = new Scanner(chosenFile);

while (reader.hasNextLine()) {

String line = reader.nextLine();

StringTokenizer token = new StringTokenizer(line, ", ");

while (token.hasMoreTokens()) {

Date212 dateObject = new Date212(token.nextToken());

System.out.println(dateObject.toString());

unsorted.add(dateObject);

sorted.add(dateObject);

}

}

}

catch (FileNotFoundException ex) {

System.out.println("File not found. " + ex.getMessage());

}

finally

{

//Prints dates to GUI after reading and sorting through the text file.

gui.unsortedDates.append(unsorted.toString());

gui.sortedDates.append(sorted.getAsString());

}

}

}

_____________________________________________________________________________

public class Project4 {

public static void main(String[] args) {

DateGui gui = new DateGui();

}

}

____________________________________________________________________________

//This class prints the sorted information from the text file.

public class SortedDateList extends DateList {

public SortedDateList() {

DateNode ln = new DateNode(null);

x = ln;

y = ln;

length = 0;

}

public void add(Date212 date) {

DateNode newNode = new DateNode(date);

if (x.next == null) {

y.next = newNode;

y= newNode;

length++;

return;

}

a = x;

while (a.next != null && a.next.date.compareTo(date) < 0)

a = a.next;

if (a.next == null) {

y.next = newNode;

y = newNode;

} else {

newNode.next = a.next;

a.next = newNode;

}

a = x;

length++;

}

}

_______________________________________________________________________________

//This class prints the date as read from the text file.

public class UnsortedDateList extends DateList {

public UnsortedDateList() {

DateNode ln = new DateNode(null);

first = ln;

last = ln;

length = 0;

}

public void add(Date212 date) {

//adds dates to the end of the linkedlist:

DateNode newNode = new DateNode(date);

last.next = newNode;

last = newNode;

length++;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!