Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

programming language Java For this programming assignment you will be re-using portions of your code from the last assignment as a starting point. You are

programming language 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.

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

MY Existing code below

import java.util.Scanner; public class MyDateClass { String month; int day; int year; int monthNum; int dayNum; // default constructor public MyDateClass () { month = "January"; day = 26; year = 2021; } public MyDateClass (int numMonth, int numDay, int numYear) { monthNum = numMonth; day = numDay; year = numYear; month = getMonthName(numMonth); // calls the getMonthName method to get the month name. dayNum = getDayNum(numDay, numMonth, numYear); // calls the getDayNum method to get the day number. } public MyDateClass (String monthName, int monthDay, int monthYear) { month = monthName; day = monthDay; year = monthYear; monthNum = getNumOfMonth(monthName); dayNum = getDayNum(day,monthNum,year); } public MyDateClass (int dd, int yy) { dayNum = dd; // dd = day, yy = year year = yy; day = getDay(dd, yy); monthNum = getMonth(dd, yy); month = getMonthName(monthNum); } public static String getMonthName(int monthNum) { String mm = ""; switch (monthNum) { case 1: mm = "January"; break; case 2: mm = "February"; break; case 3: mm = "March"; break; case 4: mm = "April"; break; case 5: mm = "May"; break; case 6: mm = "June"; break; case 7: mm = "July"; break; case 8: mm = "August"; break; case 9: mm = "September"; break; case 10: mm = "October"; break; case 11: mm = "November"; break; case 12: mm = "December"; break; } return mm; } public static int getNumOfMonth(String month) { int num = 0; switch (month) { case "January": num = 1; break; case "February": num = 2; break; case "March": num = 3; break; case "April": num = 4; break; case "May": num = 5; break; case "June": num = 6; break; case "July": num = 7; break; case "August": num = 8; break; case "September": num = 9; break; case "October": num = 10; break; case "November": num = 11; break; case "December": num = 12; break; } return num; } public static int getDayNum(int day, int month, int year) { int m[] = {31,28,31,30,31,30,31,31,30,31,30,31}; int count = 0; int counter = 0; while (counter < (month - 1)) { count += m[counter]; counter++; } return count + day; } public static int getDay(int dNum, int yy) { int m[] = {31,28,31,30,31,30,31,31,30,31,30,31}; int count = 0; int counter = 0; while (counter < 12) { if(count + m[counter] < dNum) count = count + m[counter]; else break; counter++; } return dNum - count; } public static int getMonth(int dNum, int yy) { int m[] = {31,28,31,30,31,30,31,31,30,31,30,31}; int count = 0; int counter = 0; int month = 0; while (counter < 12) { if (count + m[counter] <= dNum){ count += m[counter]; month = month + 1; } else break; counter++; } return month + 1; } public static void main(String[] args) { Scanner input = new Scanner(System.in); int choice = 0; 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. "); while (choice < 4) { System.out.println("Enter 1 for format: MM/DD/YYYY "); System.out.println("Enter 2 for format: Month DD, YYYY"); System.out.println("Enter 3 for format: DDD YYYY"); System.out.println("Enter 4 to exit"); System.out.print("choice: "); choice = input.nextInt(); switch (choice) { case 1: System.out.print("Enter Month(1-12): "); int monthInput = input.nextInt(); System.out.print("Enter Day of Month: "); int dayInput = input.nextInt(); System.out.print("Enter Year: "); int yearInput = input.nextInt(); System.out.println(); MyDateClass option1 = new MyDateClass (monthInput, dayInput, yearInput); System.out.println("MM/DD/YYYY: " + option1.monthNum + "/" + option1.day + "/" + option1.year); System.out.println("Month DD, YYYY: " + option1.month + " " + option1.day + "," + option1.year); System.out.println("DDD YYYY: " + option1.dayNum + " " + option1.year); System.out.println(); break; case 2: System.out.print("Enter Month Name: "); String monthNameInput = input.nextLine(); monthNameInput = input.nextLine(); System.out.print("Enter Day of Month: "); dayInput = input.nextInt(); System.out.print("Enter Year: "); yearInput = input.nextInt(); System.out.println(); MyDateClass option2 = new MyDateClass (monthNameInput, dayInput, yearInput); System.out.println("MM/DD/YYYY: " + option2.monthNum + "/" + option2.day + "/" + option2.year); System.out.println("Month DD, YYYY: " + option2.month + " " + option2.day + "," + option2.year); System.out.println("DDD YYYY: " + option2.dayNum + " " + option2.year); System.out.println(); break; case 3: System.out.print("Enter Day of year: "); dayInput = input.nextInt(); System.out.print("Enter Year: "); yearInput = input.nextInt(); System.out.println(); MyDateClass option3 = new MyDateClass (dayInput, yearInput); System.out.println("MM/DD/YYYY: " + option3.monthNum + "/" + option3.day + "/" + option3.year); System.out.println("Month DD, YYYY: " + option3.month + " " + option3.day + "," + option3.year); System.out.println("DDD YYYY: " + option3.dayNum + " " + option3.year); System.out.println(); break; } } System.out.println("Thank you for using 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_2

Step: 3

blur-text-image_3

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

Beginning ASP.NET 4.5 Databases

Authors: Sandeep Chanda, Damien Foggon

3rd Edition

1430243805, 978-1430243809

More Books

Students also viewed these Databases questions