Create a class called Date212 to represent a date It will st

Create a class called Date212 to represent a date. It will store the year, month and day as integers (not as a String), so you will need three private instance variables. Two constructors should be provided, one that takes three integer parameters, and one that takes a String (representing the date as in project 1). The constructor with the String parameter should validate the parameter as in project 1, and then use the substring method of class String to pull out the month, day and year, parse them as integers and call the three-argument constructor. The three-argument constructor should make sure that the month and day values are legal. To call another constructor from within a constructor, use the method this:

public Date212 (String d) { // the one-argument constructor
...
... this(year,month,day) //invokesthethree-argumentconstructor.

}
The format of the input file will be the same as Project 1.

List of Dates

Create a class called DateNode which has fields for the data (a Date212) and next (DateNode) instance variables. Include a one-argument constructor which takes a Date as a parameter. (For hints, see the PowerPoint on \"Static vs. Dynamic Structures”.)

The instance variables should have protected access. There will not be any get and set methods for the two instance variables.

Create linked list class called DateList. This should be a linked list with head node as described in lecture. Modify it so that the data type in the nodes is Date212. The no-argument constructor should create an empty list with first and last pointing to an empty head node, and length equal to zero.

Include two methods in class DateList: append and insert. The append method will add the new node to the end of the list; the insert method will add the node in the proper position to keep the list sorted in order by date.

Instantiate two linked lists, and for every date read from the file, add it to the first list using append, and to the second list using insert. You will end up with the first list having the dates from the input file in the order they were read, and in the second list the dates will be in sorted order. Display the unsorted and sorted dates in the GUI just as in project 1.

ToString() for Class Date212.

Create a toString method in lass Date212 the will return the date in the form mm/dd/yyyy. Use this method to display the dates in the GUI.

Submitting the Project.

You should now have the following files to submit for this project:

Submit a jar file.

Rather than upload all the files above separately, we will use Java’s facility to create the equivalent of a zip file that is known as a Java ARchive file, or “jar” file.

Instructions on how to create a jar file using Eclipse are on Blackboard. Create a jar file called Project2.jar and submit that.

Upload your project to BlackBoard by the due date for full credit.

Solution

public class Date212 {
private int month;
private int day;
private int year;

public Date212(String d){

String temp;
temp = d.substring(4,6);
int theMonth = Integer.parseInt(temp);
temp = d.substring(6,8);
int theDay = Integer.parseInt(temp);
temp = d.substring(0,4);
int theYear = Integer.parseInt(temp);

setDate212(theYear, theMonth, theDay);
}
public void setDate212(int y, int m, int d){
year = y;
month = m;
day = d;


}

public int getMonth(){
return month;
}
public int getDay(){
return day;
}
public int getYear(){
return year;
}

public int compareTo(Date212 other){
if(this.toString().compareTo( ((Date212)other).toString()) < 0)
return -1; //This temp is smaller
else if(this.toString().compareTo( ((Date212)other).toString()) > 0){
return 1; //This temp is bigger
}
return 0; //Must be equal
}

public String toString(){
String s = Integer.toString(year) + \"/\" + Integer.toString(month) + \"/\" + Integer.toString(day);
return s;
}   

}


public class ListNode
{
protected Date212 data;
protected ListNode next;

public ListNode(Date212 d)
{
data = d;
next = null;
} // constructor
} // class Shor

public class LinkedList {

/** First node in linked list - dummy node */
private ListNode first = new ListNode(null);

/** Last node in linked list */
private ListNode last = first;

/** Number of data items in the list. */
private int length = 0;

/**
* Gets the number of data values currently stored in this LinkedList.
*
* @return the number of elements in the list.
*/

public int getLength() {
return length;
}

/**
* Appends a String data element to this LinkedList.
*
* @param data
* the data element to be appended.
*/
public void append(Date212 d) {
ListNode n = new ListNode(d);
last.next = n;
last = n;
length++;
} // method append(String)


public void insert(Date212 d) {
ListNode n = new ListNode(d);
if (length == 0) //If your list is empty
{
last = n;
first.next = n;
n.next = null;
length++;
}
else //There is element
{
ListNode p = first.next; //Start with the first element
for(int i = 0; i<length; i++){
if(p.data.compareTo(d) < 0){ //If you are smaller than the *following* element
n.next = p.next; //Insert the element after the actual
p.next = n;
return; //Return early
}
p = p.next;
}
//We are greater than any element
this.append(d);
}
}

public boolean equals(Object other) {
if (other == null || getClass() != other.getClass()
|| length != ((LinkedList) other).length)
return false;

ListNode nodeThis = first;
ListNode nodeOther = ((LinkedList) other).first;
while (nodeThis != null) {
// Since the two linked lists are the same length,
// they should reach null on the same iteration.

if (nodeThis.data != nodeOther.data)
return false;

nodeThis = nodeThis.next;
nodeOther = nodeOther.next;
} // while

return true;
} // method equals
public String printList(){
String s = \"\";
ListNode p = first.next;

while(p != null){
s += p.data.toString() + \"\ \";
p = p.next;
}
return s;

}
}//


public void insert(Date212 d) {
ListNode n = new ListNode(d);
ListNode p = first;

// Find the insertion point
while ((p.next != null) && (p.next.data.compareTo(d) < 0)) {
p = p.next;
}

// Insert the node
n.next = p.next;
p.next = n;

if (n.next == null) {
last = n;
}

// Update the list length
length++;
}

Create a class called Date212 to represent a date. It will store the year, month and day as integers (not as a String), so you will need three private instance
Create a class called Date212 to represent a date. It will store the year, month and day as integers (not as a String), so you will need three private instance
Create a class called Date212 to represent a date. It will store the year, month and day as integers (not as a String), so you will need three private instance
Create a class called Date212 to represent a date. It will store the year, month and day as integers (not as a String), so you will need three private instance

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site