Question: Java For this programming assignment, you will be re-using portions of your code from the last assignment as a starting point. You are to create
Java
For this programming assignment, you will be re-using portions of your code from the last assignment as a starting point. You are to create a new DateFileTest class that uses the existing Date Class for the following:
- Instead of bringing in dates from a user, you will be bringing in dates from a text file.
- You should use the existing Date class to create date objects from file inputs
- The data in the file is provided in alternating formats in order from MM/DD/YYYY to Month name, DD, YYYY (just those two formats to simplify the input handling and verification). Spaces are used in the file as delimiters between integers and strings in the date format. You will use the file provided here:
dates in multiple formats.txt
8 17 1943 April 1 1985 4 27 2010 September 27 2005 12 13 2001 June 5 1075 11 25 1974 December 11 2004 21 14 1980
- Your code is to handle exceptions for the following situations;
- the file is not found or can't be opened, in which case the program informs the user and ends.
- the month is not a valid month (between 1-12), using a try/catch block
- the year is not a valid year (assume between 1800-2020 is valid, using a try/catch block)
- the entry is not a valid month name (must start with a capital and be spelled correctly as a string)
- If any of these exceptions occur, your program must disregard that specific date, output an error message to the display, NOT write that specific date to the file, and continue to move on to process the rest of the data in the file
- Your program will create a new text file and write dates out to that file. You will write out all 3 formats (with appropriate / and , delimiters) to the file for each date read in from the file.
- Your program files must include a program header that includes your name, course discussion section, and program description. Well structured and commented code are expected. Your submission must include all program files, output text file, and execution screenshots showing your messages from error handling. Submissions without all the files will not receive a grade and no late submissions will be accepted.
Previous code is here:
Date.java
4 public class Date { 5 //Data members 6 private int day,month,year; 7 //Default constructor 8 public Date() { 9 day=month=year=0; 10 } 11 //Int overloaded construcor 12 public Date(int d,int m,int y) { 13 day=d; 14 month=m; 15 year=y; 16 } 17 //String overloaded construcor 18 public Date(int d,String m,int y) { 19 day=d; 20 year=y; 21 if(m.compareToIgnoreCase("January")==0) { 22 month=1; 23 } 24 else if(m.compareToIgnoreCase("February")==0) { 25 month=2; 26 } 27 else if(m.compareToIgnoreCase("March")==0) { 28 month=3; 29 } 30 else if(m.compareToIgnoreCase("April")==0) { 31 month=4; 32 } 33 else if(m.compareToIgnoreCase("May")==0) { 34 month=5; 35 } 36 else if(m.compareToIgnoreCase("June")==0) { 37 month=6; 38 } 39 else if(m.compareToIgnoreCase("July")==0) { 40 month=7; 41 } 42 else if(m.compareToIgnoreCase("August")==0) { 43 month=8; 44 } 45 else if(m.compareToIgnoreCase("September")==0) { 46 month=9; 47 } 48 else if(m.compareToIgnoreCase("October")==0) { 49 month=10; 50 } 51 else if(m.compareToIgnoreCase("December")==0) { 52 month=11; 53 } 54 else { 55 month=12; 56 } 57 } 58 //Int overloaded constructor 59 public Date(int days,int yr) { 60 year=yr; 61 if(days<=31) { 62 month=1; 63 day=days; 64 } 65 else if(days<=59) { 66 month=2; 67 day=days-31; 68 } 69 else if(days<=90) { 70 month=3; 71 day=days-59; 72 } 73 else if(days<=120) { 74 month=4; 75 day=days-90; 76 } 77 else if(days<=151) { 78 month=5; 79 day=days-120; 80 } 81 else if(days<=181) { 82 month=6; 83 day=days-151; 84 } 85 else if(days<=212) { 86 month=7; 87 day=days-181; 88 } 89 else if(days<=243) { 90 month=8; 91 day=days-212; 92 } 93 else if(days<=273) { 94 month=9; 95 day=days-243; 96 } 97 else if(days<=304) { 98 month=10; 99 day=days-273; 100 } 101 else if(days<=334) { 102 month=11; 103 day=days-304; 104 } 105 else { 106 month=12; 107 day=days-334; 108 } 109 } 110 //Return 3 date formatted string 111 public String toString() { 112 String str="DD/MM/YYYY: "+month+"/"+day+"/"+year+" Month DD, YYYY: "; 113 if(month==1) { 114 str+="January "; 115 } 116 else if(month==2) { 117 str+="February "; 118 } 119 else if(month==3) { 120 str+="March "; 121 } 122 else if(month==4) { 123 str+="April "; 124 } 125 else if(month==5) { 126 str+="May "; 127 } 128 else if(month==6) { 129 str+="June "; 130 } 131 else if(month==7) { 132 str+="July "; 133 } 134 else if(month==8) { 135 str+="August "; 136 } 137 else if(month==9) { 138 str+="September "; 139 } 140 else if(month==10) { 141 str+="October "; 142 } 143 else if(month==11) { 144 str+="November "; 145 } 146 else { 147 str+="December "; 148 } 149 str+=day+","+year+" DDD YYYY: "; 150 str+=((month-1)*30+day)+" "+year; 151 return str; 152 } 153 } 154 155 156 157
DateTest.Java
4 import java.util.Scanner; 5 6 public class DateTest { 7 8 public static void main(String[] args) { 9 Scanner sc=new Scanner(System.in); 10 // Header 11 System.out.println("This program converts dates entered to multiple formats."); 12 System.out.println("Enter your date in the format you choose and all three formats will be provided as an output."); 13 //Get user option 14 int opt=getOptions(); 15 //Loop until exit 16 while(opt!=4) { 17 //Execute options 18 if(opt==1) { 19 System.out.print("Enter Month (1-12): "); 20 int m=sc.nextInt(); 21 System.out.print("Enter Day of Month: "); 22 int d=sc.nextInt(); 23 System.out.print("Enter Year: "); 24 int y=sc.nextInt(); 25 Date date=new Date(d,m,y); 26 System.out.println(date); 27 } 28 else if(opt==2) { 29 sc.nextLine(); 30 System.out.print("Enter Month Name: "); 31 String m=sc.nextLine(); 32 System.out.print("Enter Day of Month: "); 33 int d=sc.nextInt(); 34 System.out.print("Enter Year: "); 35 int y=sc.nextInt(); 36 Date date=new Date(d,m,y); 37 System.out.println(date); 38 } 39 else if(opt==3) { 40 System.out.print("Enter Day of Year: "); 41 int d=sc.nextInt(); 42 System.out.print("Enter Year: "); 43 int y=sc.nextInt(); 44 Date date=new Date(d,y); 45 System.out.println(date); 46 } 47 //Loop repetition 48 opt=getOptions(); 49 } 50 System.out.println("Thank you for using my program."); 51 } 52 //Method for choices 53 public static int getOptions() { 54 Scanner sc=new Scanner(System.in); 55 System.out.println("Enter 1 for format: MM/DD/YYYY " + 56 "Enter 2 for format: Month DD, YYYY " + 57 "Enter 3 for format: DDD YYYY " + 58 "Enter 4 to exit"); 59 System.out.print("Choice: "); 60 int ch=sc.nextInt(); 61 while(ch<1 || ch>4) { 62 System.out.println("Wrong choice!!Try again..."); 63 System.out.print("Choice: "); 64 ch=sc.nextInt(); 65 } 66 return ch; 67 } 68 }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
