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:
  • 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.

The Sample data is here. can't upload a file it seems.

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

------------------------------------------------------------------My code listed below--------------------------------------------------

public class MpDate { //* Variables of date int month; int day; int year; //**default constructor public MpDate(){ month = 0; day = 0; year = 0; } //*Display constructor for MM/DD/YYYY public MpDate(int m, int d, int y) { month = m; day = d; year = y; } //*String overloaded constructor public MpDate(String m, int d, 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; } } //*Int overloaded constructor public MpDate(int tdays, int yr){ year=yr; if(tdays<=31){ month=1; day=tdays; } else if(tdays<=59){ month=2; day=tdays-31; } else if(tdays<=90) { month = 3; day = tdays - 59; } else if(tdays<=120){ month=4; day=tdays-90; } else if(tdays<=151){ month=5; day=tdays-120; } else if(tdays<=181){ month=6; day=tdays-151; } else if(tdays<=212){ month=7; day=tdays-181; } else if(tdays<=243){ month=8; day=tdays-212; } else if(tdays<=273){ month=9; day=tdays-243; } else if(tdays<=304){ month=10; day=tdays-273; } else if(tdays<=334){ month=11; day=tdays-304; } else{ month=12; day=tdays-334; } } //** return the date in string format public String toString(){ String sfor="DD/MM/YYYY: " +month+"/"+day+"/"+year+" Month DD, YYYY: "; if(month==1){ sfor+="January "; } else if(month==2){ sfor+="February "; } else if(month==3){ sfor+="March "; } else if(month==4){ sfor+="April "; } else if(month==5){ sfor+="May "; } else if(month==6){ sfor+="June "; } else if(month==7){ sfor+="July "; } else if(month==8){ sfor+="August "; } else if(month==9){ sfor+="September "; } else if(month==10){ sfor+="October "; } else if(month==11){ sfor+="November "; } else{ sfor+="December "; } sfor+=day+"," +year+" DDD, YYYY: "; sfor+=((month-1)*30+day)+ " , "+year; return sfor; } }
import java.util.Scanner; public class TestMpDate { //** getting Selection from user method public static int getSelection(){ Scanner dc = 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("Selection: "); int ch = dc.nextInt(); while (ch < 1 || ch > 4) { System.out.print("Make a better Selection, Please."); System.out.print("Selection: "); ch = dc.nextInt(); } return ch; } //** Main Method public static void main(String[] args) { Scanner dc = new Scanner(System.in); System.out.println("This program converts dates input into different formats."); System.out.println("Enter your date the format you would like and it will display output in all three formats."); //**Get user selection int cho= getSelection(); //Loop requests of info until requested to end while (cho != 4) { //lists out selection choices for input. if (cho == 1) { System.out.print("Enter Month 1-12: "); int m = dc.nextInt(); System.out.print("Enter Day of the Month 1-31: "); int d = dc.nextInt(); System.out.print("Enter the Year: "); int y = dc.nextInt(); MpDate date = new MpDate(m, d, y); System.out.println(date); } else if (cho == 2) { dc.nextLine(); System.out.print("Enter Month Name: "); String m = dc.nextLine(); System.out.print("Enter Day of the Month 1-31:"); int d = dc.nextInt(); System.out.print("Enter the Year: "); int y = dc.nextInt(); MpDate date = new MpDate(m, d, y); System.out.println(date); } else if (cho == 3) { System.out.print("Enter Day of the Year: "); int d = dc.nextInt(); System.out.print("Enter the Year: "); int y = dc.nextInt(); MpDate date = new MpDate(d, y); System.out.println(date); } cho = getSelection(); } //**loops out System.out.println("Thank you for testing my Program."); } }

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

Relational Database And SQL

Authors: Lucy Scott

3rd Edition

1087899699, 978-1087899695

More Books

Students also viewed these Databases questions

Question

What is DDL?

Answered: 1 week ago