Please provide a java code for the following problem: 1. Create a class hierarch
ID: 3777387 • Letter: P
Question
Please provide a java code for the following problem:
1. Create a class hierarchy which has an abstract class called DateList. This class should have two subclasses: UnsortedDateList and SortedDateList, each having a method called add. In the UnsortedDateList the method add will add to the end of the list (append), and in the SortedDateList it will do an insert. So now, rather than in project 2 where you used two of the same kind of list (DateList) you will use one unsorted list and one sorted list.
2. Create a new exception called IllegalDate212Exception by extending lllegalArgumentException.
3. When you create a new Date212 from a String read from the input file, catch any exceptions
thrown by the constructor and print the offending string to the console along with the message
from the Exception.
4. Add a File menu to your GUI which has menu items for Open and Quit. You should now be able
to select an input file using the GUI.
Submitting the Project.
You should now have at least the following files to submit for this project:
Project3.java
Date212.java
DateGUI.java
DateNode.java
DateList.java
UnsortedDateList.java
SortedDateList.java
FileMenuHandler.java
IllegalDate212Exception.java
Project3.java Date212.java DateGUI.java DateNode.java DateList.java UnsortedDateList.java SortedDateList.java FileMenuHandler.java IllegalDate212Exception.java
Explanation / Answer
Code:
Project3.java
package project3;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class Project3{
public static void createAndShowGUI()
{
JFrame frame;
JPanel panel;
JMenu menu;
JMenuBar menuBar;
frame=new JFrame("Date Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(400, 500));
frame.setLayout(new BorderLayout());
menu= new JMenu("File"); //File menu
JMenuItem open=new JMenuItem("Open"); //Open menu item
open.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser=new JFileChooser(); //file chooser
int status=fileChooser.showOpenDialog(frame); //show open file dialog
//checks if a file is selected
if(status==JFileChooser.APPROVE_OPTION)
{
try
{
UnsortedDateList l=new UnsortedDateList();
SortedDateList s=new SortedDateList();
File file=fileChooser.getSelectedFile();
FileReader fileReader=new FileReader(file);
BufferedReader br=new BufferedReader(fileReader);
//read date string
String dateString;
while((dateString=br.readLine())!=null)
{
Date212 date212=new Date212 (dateString);
l.add(date212);
s.add(date212);
}
br.close();
System.out.println(l.printList());
System.out.println(s.printList());
}
catch (IllegalDate212Exception exception)
{
exception.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
JMenuItem quit=new JMenuItem("Quit"); //Quit menu item
quit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//add to menu
menu.add(open);
menu.add(quit);
//create menu bar
menuBar=new JMenuBar();
//add menu
menuBar.add(menu);
//create a panel
panel=new JPanel();
panel.setLayout(new BorderLayout());
//add menubar to panel
panel.add(menuBar, BorderLayout.NORTH);
//add panel to frame
frame.getContentPane().add(panel);
//show frame
frame.setVisible(true);
frame.pack();
}
/**
* function to create a Date212 object
*/
public static Date212 createDate212(String dateString)
{
return new Date212(dateString);
}
/**
* @param args
*/
public static void main(String[] args) {
Project3.createAndShowGUI();
}
}
DateNode.java
package project3;
public class DateNode {
protected Date212 data;
protected DateNode next;
public DateNode (Date212 d)
{
data=d;
next=null;
}
}
Date212.java
package project3;
public class Date212 {
int year;
int month;
int day;
Date212(String dateString) throws IllegalDate212Exception{
boolean valid=isValidString(dateString);
if(valid ==false)
{
throw new IllegalDate212Exception("Not a valid input");
}
int y=Integer.parseInt(dateString.substring(0,4));
int m=Integer.parseInt(dateString.substring(4,6));
int d=Integer.parseInt(dateString.substring(6,8));
boolean validdate=isValidDate(y, m, d);
if(validdate==true)
{
year=y;
month=m;
day=d;
}
else
{
throw new IllegalDate212Exception("Not a valid input");
}
}
private boolean isValidString(String dateString) {
if (dateString.matches("[0-9]+")&& dateString.length() ==8 ) {
return true;
}
else
return false;
}
private boolean isValidDate(int y, int m, int d) {
if (y < 9999 && y > 0000)
{
if (m <= 12 && m >= 1)
{
if (d >= 1 && d <= 31)
{
return true;
}
}
}
return false;
}
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;
}
}
DateList.java
package project3;
/**
* DateList abstract class
*
*/
public abstract class DateList {
DateNode first=null;
DateNode last=null;
private int length=0;
public int getLength() {
return length;
}
public void append(Date212 d) {
DateNode n = new DateNode(d);
if(length==0)
{
last=first=n;
}
else
{
last.next = n;
last = n;
length++;
}
} // method append(String)
public void insert(Date212 d) {
DateNode n = new DateNode(d);
if (length == 0) //If your list is empty
{
last = n;
first = n;
length++;
}
else //There is element
{
DateNode p = first; //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 != ((DateList) other).length)
return false;
DateNode nodeThis = first;
DateNode nodeOther = ((DateList) 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 = "";
DateNode p = first;
while(p != null){
s += p.data.toString() + " ";
p = p.next;
}
return s;
}
}
IllegalData212exception.java
package project3;
public class IllegalDate212Exception extends IllegalArgumentException {
/**
*
*/
private static final long serialVersionUID = 2591569031927210976L;
/**
*
*/
public IllegalDate212Exception() {
// TODO Auto-generated constructor stub
}
/**
* @param arg0
*/
public IllegalDate212Exception(String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public IllegalDate212Exception(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public IllegalDate212Exception(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
}
SorteddateList.java
package project3;
public class SortedDateList extends DateList{
public void add(Date212 date) {
insert(date);
}
}
UnsortedDateList.java
package project3;
import java.util.ArrayList;
import java.util.Date;
/**
* UnsortedDateList
*
*/
public class UnsortedDateList extends DateList {
public void add(Date212 date) {
append(date);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.