. *Date212.java X 围 1 //Declare the class Date212 2 public class Date212 3 { 4 //Declare local variables 5 private int M

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

. *Date212.java X 围 1 //Declare the class Date212 2 public class Date212 3 { 4 //Declare local variables 5 private int M

Post by answerhappygod »

 1
1 (82.9 KiB) Viewed 52 times
. *Date212.java X 围 1 //Declare the class Date212 2 public class Date212 3 { 4 //Declare local variables 5 private int Month; 6 private int Year; 7 private int Day; 8 protected String full_Date; 9 10 //Define the Constructor 110 public Date212 (String s) 12 { 13 // initial constructor 14 String validDate = s.replaceAll("\\s", ""); 15 16 // check the input date is valid string or not 17 if(validDate.length() != 8) 18 { 19 throw new Date212Exception ("Your input " + s + " is not 8 digits."); ( S 20 } 21 22 full_Date = s; 2023 int daysInMonth; 24 Boolean leapYear = true; = 25 int tMonth = Integer.parseInt(s.substring(4,6)); 26 //Pull off month, but in a temp value. 27 28 if(!((tMonth) >0 && (tMonth) < 13)) 29 { 30 throw new Date212Exception ("Month " + tMonth +" is not an actual month."); 31 } int tYear = Integer.parseInt(s.substring(0,4)); 033 int tDay = Integer.parseInt(s.substring(6,8)); 34 35 // Date date = sdf.parse (vald); 36 // System.out.println (date); 37 if (tMonth == 4 || Month == 6 || Month == 9 || Month == 11) 38 daysInMonth = 30; 39 else if (tMonth == 2) 40 daysInMonth= (leapYear) ? 29 : 28; ( 41 else if (tMonth == 1 || tMonth == 3 || tMonth == 5 || tMonth == 7 42 || tMonth == 8|| tMonth == 10 || tMonth == 12) 43 daysInMonth = 31; 44 else 45 throw new Date212Exception ("The date you entered " 46 + Day +"for the month " 47 + Month + " does not exist"); 48 Year = Integer.parseInt(s.substring(0,4)); Month = Integer.parseInt(s.substring (4,6)); 50 Day = Integer.parseInt(s.substring (6,8)); 51 } 52 53 //Implement toString method public String toString() { return Year + " / " + String.format(" $020 ", Month) + "/" + String.format(" $020 ", Day); } 832 49 = . $26.:::::: // compare to, compare the m, d, and class if they are all value public int compareTo (Date212 other) { int yearDiff - this. Year - other. Year; = 65 66 if (yearDiff == 0) { int monthDiff = this.Month - other.Month; 2 ವ ವ ವ ತ 3 if (monthDiff == 0) { { int dayDiff = this. Day - other. Day; return dayDiff;

Java 212 - Project41/src/Date212.java - Eclipse IDE File Edit Source Refactor Navigate Search Project Run Window Help HH * -G 嗯 画心|| 1432 = = 5 *Date212.java X 12 1 13 //initial constructor 14 String validDate = s.replaceAll("\\s", ""); 15 16 // check the input date is valid string or not 17 if(validDate.length() != 8) 18 { 19 throw new Date212Exception ("Your input " + s + " is not 8 digits."); 20 } 21 22 full_Date = s; 123 int daysInMonth; 24 Boolean leap Year = true; 25 int tMonth = Integer.parseInt(s.substring(4,6)); 26 //Pull off month, but in a temp value. 27 28 if(!((tMonth) >0 && (tMonth) < 13)) 29 { 30 throw new Date212Exception ("Month " + tMonth +" is not an actual month."); 31 } int tYear = Integer.parseInt(s.substring(0,4)); 9433 int tDay = Integer.parseInt(s.substring(6,8)); 34 35 // Date date = sdf.parse(vald); 36 // System.out.println (date); 37 if (tMonth == 4 || Month == 6 || Month == 9 || Month == 11) (4 38 days InMonth = 30; 39 else if (tMonth == 2) ) 40 daysInMonth - (leapYear) ? 29 : 28; 41 else if (tMonth == 1 || Month == 3 || Month == 5 || Month == 7 42 || tMonth == 8|| tMonth == 10 || Month == 12) 43 daysInMonth - 31; = ; 44 else 45 throw new Date212Exception ("The date you entered " 46 + Day +"for the month " 47 + Month + " does not exist"); 48 Year = Integer.parseInt(s.substring(0,4)); 49 Month = Integer.parseInt(s.substring(4,6)); 50 Day = Integer.parseInt(s.substring (6,8)); 51 } 52 53 //Implement toString method A540 public String toString() 55 { 56 return Year + " / " + String.format(" %02d", Month) + "/" 57 + String.format(" $020 ", Day); 58 } 59 60 // compare to, compare the m, d, and class if they are all value 610 public int compareTo (Date212 other) 62 { 63 int yearDiff = this. Year - other. Year; 64 65 if (yearDiff == 0) 66 { 67 int monthDiff = this.Month - other.Month; ; 68 69 if (monthDiff == 0) 70 { 71 int dayDiff = this.Day - other.Day; 72 73 return dayDiff; 74 } 75 else 76 { 77 return monthDiff; 78 } 79 } 80 else { 82 return yearDiff; 83 } 84 } 85 }//Date212 class 88m88swas a s was agn%BB%BDB%8a8888 82

*Date212.java Date212Exception.java X 1 //Define the exception 2 public class Date212Exception extends IllegalArgumentException { 3 //Define the message 40 public Date212Exception(String s) { 6 super(s); 7 } public Date212Exception() 9 { 10 super("Invalid Word!"); 11 } 12 } мнооо ол

1 7 *Date212.java Date212Exception.java D Date212Listjava X //define the abstract class 2 public abstract class Date212 List 3 { 4 //define the objects 5 protected Date212Node first last; 6 protected int length; //Constructor 80 public Date212 List() 9 { 10 first = last = new Date212Node (null); 11 length = 0; 12 } 13 // check whether list is empty or not 140 public boolean isEmpty() 15 { 16 return length = 0; 17 1 18 //implement append method 190 public void append (Date212 d) 20 { 21 last.next = new Date212Node (d); 22 last last.next; 23 length++; 24 } 25 //implement toString method 260 public String toString() 27 { 28 Date212Node curr first.next; 29 String str = ""; 30 while (curr != null) { 32 str += curr.data.toString() + "\n\n"; 33 curr = curr.next; 34 } 35 return str; 36 }} 31

Date212List.java Date212Node.java X *Date212.java Date212Exception.java i\/define the class Date212Node 2 public class Date212Node 3 { 4 //define the objects 5 protected Date212 data; 6 protected Date212Node next; //constructor 80 public Date212Node (Date212 d) 9 { 10 data = d; 11 next = null; 12 } 13 } 7

DateGUI.java X Date 212.java Date212Exception.java Date212List.java Date212Node.java 1 //Import required header files 2+ import javax.swing. *; 13 14 //Define the class 15 public class DateGUI extends JFrame 16 17 //declare objects 18 private static Date212 date; 19 public static StringTokenizer myTokens; 20 public static String line; 21 public static Text FileInput inFile; 22 public static TextArea unsortedList = new TextArea(); 23 public static TextArea sortedList = new TextArea(); 24 public static Container myContentPane; 25 public static JFrame date Frame: 26 private static Date212 rDate; 27 private static Date212 iDate; 28 private String[] hi; 296 TreeMap <Date212, Integer> Dates = new TreeMap <Date212, Integer> 30 (new DateComparator()); 31 32 //Define the constructor 330 public DateGUI (String title, int height, int width) 34 { 35 setTitle(title); 36 setSize (height,width); 37 setLocation (600, 300); 38 Font font = new Font ("Segoe Script", Font.BOLD, 14); 39 unsortedList.set Font (font); 40 sortedList.setFont (font); 41 setLayout (new GridLayout(1, 2)); 42 createDosMenu(); 43 setDefaultCloseOperation (EXIT_ON_CLOSE) ; 44 setVisible(true); 45 } //DateGUI constructor. 46 47 //implement method to create GUI 480 private void createDosMenu() 49 { 50 //define the objects 51 JMenuItem item; 52 JMenuBar menuBar = new JMenuBar(); 53 JMenu fileMenu = new JMenu ("File"); 54 JMenu edit Menu = new JMenu ("Edit"); 55 56 //create objects for both edithandler and file handler 57 EditMenuHandler menuHanlder = new EditMenuHandler (this); 58 FileMenuHandler FileHandler = new FileMenuHandler (this); 59 60 //Create opetion 61 item = new JMenuItem("Open"); 62 //add the listener to the option 63 item.addActionListener( FileHandler ); 64 fileMenu.add (item); fileMenu.addSeparator(); 66 67 item = new JMenuItem("Quit"); 68 item.addActionListener( FileHandler ); 69 fileMenu.add( item); 70 menuBar.add(fileMenu); 71 72 item = new JMenuItem("Insert"); 73 item.addActionListener (menuHanlder); 74 editMenu.add( item); ; 75 76 menuBar.add (editMenu); 77 setJMenuBar (menuBar); 78 } // CreateDosMenu method 79 80 //implement the listener 810 public class EditMenuHandler implements ActionListener 82 { 83 String dateInput; 65 RA

= 105 3 Date 212.java Date 212Exception.java Date212 List.java Date 212Node.java DateGUl.java X 83 String dateInput; 84 85 //EditMenuHandler constructor method 860 public EditMenuHandler (JFrame jf) 87 { 88 //Works on the JFrame passed in. 89 date Frame = jf; 90 } 91 - 920 public void actionPerformed (ActionEvent event) 93 { 94 String menuName = event.getActionCommand(); 95 if (menuName.equals("Insert")) 96 dateInput JOptionPane.showInputDialog(null, "Enter a new date in the 8 digit " 97 + "format (YYYYMMdd), as in the file."); 98 99 input Date (dateInput); 100 } 101 102 //Implement the insert new date and sort the date with previous 103 //dates list 1040 public void inputDate (String d) { 106 int p = 0; 107 myContentPane = dateFrame.getContentPane(); 108 try 109 { 110 i Date = new Date212 (d); 111 unsortedList.append("\n"+iDate + "\n"); 112 Dates. put (i Date, ++p); 113 } 114 catch (Date212Exception e) 115 { 116 System.out.println(e); 117 } 118 sortedlist.append(""); 119 Set<Date212> value = Dates. keySet(); 120 sortedList.append("\n"); 121 for (Date212 key:value) 122 sortedlist.append (key+ "\n"); 123 date Frame.setVisible(true); 124 }// input Date method 125 }//EditMenuHandler method 126 127 // compareter class to compareDates 1280 public class DateComparator implements Comparator <Date212> 129 { -1300 public int compare (Date212 numl, Date212 num2) 131 { { 132 return numl.compareTo (num2); 133 } 134 } 135 136 //Create FileHandler 1370 public class FileMenuHandler implements ActionListener 138 { { 139 //Implement FileMenuHandler Constructor 1400 public FileMenuHandler (JFrame jf) 141 { 142 date Frame = jf; 143 } 144 145 //Action performed -1460 public void actionPerformed (ActionEvent event) 147 { 148 String menuName; 149 menuName = event.getActionCommand(); 150 // 151 if (menuName.equals("Open")) 152 openFile(); //Calls the method... //If Quit is selected then program is exited. 154 else if (menuName.equals("Quit")) 155 System.exit(0); //Exits the programs 156 1 Iltan Darformed method 153

*Date212.java Date212Exception.java Date 212 Listjava Date212Node.java DateGUl.java X 151 11 menuname.equals("open")) 152 openFile(); //Calls the method... 153 //If Quit is selected then program is exited. 154 else if (menuName.equals("Quit")) 155 System.exit(0); //Exits the programs 156 } //actionPerformed method 157 1580 private void openFile() 159 { 160 JFileChooser chooser; 161 162 chooser = new JFileChooser(); //GUI for choosing a file. 163 if (chooser.showOpenDialog (null) == JFileChooser. APPROVE_OPTION) 164 { 165 readSource (chooser.getSelectedFile()); 166 } 167 else 168 { 169 JOptionPane.showMessageDialog(null, "Open File dialog canceled"); 170 } 171 } //openFile method 172 173 //Read the file 1740 private void readSource (File chosenFile) 175 { 176 // Get the filename 177 String chosenFileName = chosenFile.getAbsolutePath(); 178 int p = 0; 179 myContentPane = dateFrame.getContentPane(); ; 180 myContentPane.add (unsortedlist); 181 myContentPane. add (sortedList); 182 183 //Add the two textAreas to the contentPane. 184 inFile = new TextFileInput (chosenFileName); 185 line = inFile.readLine(); 186 187 //read the line 188 while (line != null) 189 { 190 myTokens = new StringTokenizer (line, ""); 191 while (myTokens.hasMoreTokens()) 192 { 193 //While there is more tokens on the line. 194 String[] hi = new String [myTokens.countTokens ()]; 195 for(int i =0; i< hi.length; i++) { 196 197 try 198 { 199 hi= myTokens.nextToken(); 200 rDate = new Date212 (hi); 201 202 unsortedList.append (rDate + "\n"); 203 204 Dates. put (rDate, ++p); 205 206 } catch (Date212Exception e) 207 { 208 System.out.println(e); 209 } 210 } 211 } 212 //Read next line. 213 line = inFile.readLine(); 214 } 215 //close the file 216 inFile.close(); 217 //Returns a set view of keys in this treeMap. 218 Set<Date212> value = Dates. keySet(); 219 for (Date212 key:value) 220 sortedlist.append (key+ "\n"); 221 dateFrame.setVisible(true); 222 }//readSource method 223 }//FileMenuHandler method 224 }//Class DateGUI

Date212Node.java DateGUI.java Project4.java X 3 5 *Date212.java Date212Exception.java Date212 List.java 1 //define the class Project 4 2 public class Project 4 { 4 //declare Object static DateGUI dateGUI; 6 7 //main method 8 public static void main(String[] args) 9 { 10 //call the class with the dimenstion of the 11 //Frame 12 dateGUI = new DateGUI ("DateGUI", 900, 300); 13) 14 }
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply