Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

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 data 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 is expected. Your submission must include all program files, output text files, 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

My Existing Code is down below :-

Date.java

public class Date { //Data members private int day,month,year; //Default constructor public Date() { day=month=year=0; } //3 Int overloaded construcor public Date(int d,int m,int y) { day=d; month=m; year=y; } //String overloaded construcor public Date(int d,String m,int y) { day=d; year=y; if(m.compareToIgnoreCase("January")==0) { month=1; } else if(m.compareToIgnoreCase("February")==0) { month=2; } else if(m.compareToIgnoreCase("March")==0) { month=3; } else if(m.compareToIgnoreCase("April")==0) { month=4; } else if(m.compareToIgnoreCase("May")==0) { month=5; } else if(m.compareToIgnoreCase("June")==0) { month=6; } else if(m.compareToIgnoreCase("July")==0) { month=7; } else if(m.compareToIgnoreCase("August")==0) { month=8; } else if(m.compareToIgnoreCase("September")==0) { month=9; } else if(m.compareToIgnoreCase("October")==0) { month=10; } else if(m.compareToIgnoreCase("December")==0) { month=11; } else { month=12; } } //2 Int overloaded constructor public Date(int days,int yr) { year=yr; if(days<=31) { month=1; day=days; } else if(days<=59) { month=2; day=days-31; } else if(days<=90) { month=3; day=days-59; } else if(days<=120) { month=4; day=days-90; } else if(days<=151) { month=5; day=days-120; } else if(days<=181) { month=6; day=days-151; } else if(days<=212) { month=7; day=days-181; } else if(days<=243) { month=8; day=days-212; } else if(days<=273) { month=9; day=days-243; } else if(days<=304) { month=10; day=days-273; } else if(days<=334) { month=11; day=days-304; } else { month=12; day=days-334; } } //Return 3 date formatted string public String toString() { String str="DD/MM/YYYY: "+month+"/"+day+"/"+year+" Month DD, YYYY: "; if(month==1) { str+="January "; } else if(month==2) { str+="February "; } else if(month==3) { str+="March "; } else if(month==4) { str+="April "; } else if(month==5) { str+="May "; } else if(month==6) { str+="June "; } else if(month==7) { str+="July "; } else if(month==8) { str+="August "; } else if(month==9) { str+="September "; } else if(month==10) { str+="October "; } else if(month==11) { str+="November "; } else { str+="December "; } str+=day+","+year+" DDD YYYY: "; str+=((month-1)*30+day)+" "+year; return str; } }

DateTest.java

import java.util.Scanner;

public class DateTest {

public static void main(String[] args) { Scanner sc=new Scanner(System.in); // Header messages System.out.println("This program converts dates entered to multiple formats."); System.out.println("Enter your date in the format you choose and all three formats will be provided as an output."); //Get user option int opt=getOptions(); //Loop until exit while(opt!=4) { //Execute ech options if(opt==1) { System.out.print("Enter Month (1-12): "); int m=sc.nextInt(); System.out.print("Enter Day of Month: "); int d=sc.nextInt(); System.out.print("Enter Year: "); int y=sc.nextInt(); Date date=new Date(d,m,y); System.out.println(date); } else if(opt==2) { sc.nextLine(); System.out.print("Enter Month Name: "); String m=sc.nextLine(); System.out.print("Enter Day of Month: "); int d=sc.nextInt(); System.out.print("Enter Year: "); int y=sc.nextInt(); Date date=new Date(d,m,y); System.out.println(date); } else if(opt==3) { System.out.print("Enter Day of Year: "); int d=sc.nextInt(); System.out.print("Enter Year: "); int y=sc.nextInt(); Date date=new Date(d,y); System.out.println(date); } //Loop repetition opt=getOptions(); } System.out.println("Thank you for using my program."); } //MEthod for choices public static int getOptions() { Scanner sc=new Scanner(System.in); System.out.println("Enter 1 for format: MM/DD/YYYY " + "Enter 2 for format: Month DD, YYYY " + "Enter 3 for format: DDD YYYY " + "Enter 4 to exit"); System.out.print("Choice: "); int ch=sc.nextInt(); while(ch<1 || ch>4) { System.out.println("Wrong choice!!Try again..."); System.out.print("Choice: "); ch=sc.nextInt(); } return ch; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Sham Navathe,Ramez Elmasri

5th Edition

B01FGJTE0Q, 978-0805317558

More Books

Students also viewed these Databases questions