Question
Book Icon
Chapter 9, Problem 3PP
Program Plan Intro

Date format converter

Program plan:

“DateFormatConverter.java”:

  • Import required packages.
  • Define the class “DateFormatConverter”.
    • Declare required variables.
    • Define an array and assign it with the months.
    • Define the “main()” method.
      • Do until the user enters other than “Y” or “y”.
        • Inside the “try” block,
          • Get the date from the user.
          • Trim the input.
          • Store the month value in a variable.
          • Check if that value is less than 0.
            • Assign “false” to “validInput”.
            • Throw an exception.
          • Check if month value is less than 1 or greater than 12.
            • Assign “false” to “validInput”.
            • Throw an exception.
          • Otherwise,
            • Get the date value and store it in a variable.
            • Call the function “dayCheck()”.
        • Catch “MonthException”.
          • Print the error message.
        • Check if the function “validInput()” returns true.
          • Print the month name and its date.
        • Get the response from the user whether the user wants to do it again or not.
    • Give function to convert string to integer for month.
      • Get the position of “/”.
      • Check if that position is equal to 2.
        • Return the value.
      • Check if that position is equal to 1.
        • Return the integer value.
      • Otherwise,
        • Return -1.
    • Give function to convert string to integer for day.
      • Get the number of characters.
      • Check if that number is equal to 2.
        • Return the value.
      • Check if that number is equal to 1.
        • Return the integer value.
      • Otherwise,
        • Return -1.
    • Function definition to convert ASCII value to integer.
      • Switch to the digit.
        • Assign the value according to the character digit.
    • Give function definition to check the day.
      • Inside the “try” block,
        • Switch to the month number.
          • If the month number is 1, 3, 5, 7, 8, 10 and 12,
            • Check if the day is less than 1 or greater than 31.
              • Assign “false” to “validInput”.
              • Print the error message.
                • Otherwise,
                  • Assign “true” to “validInput”.
                • Break the case.
          • If the month number is 4, 6, 9 and 11,
            • Check if the day is less than 1 or greater than 30.
              • Assign “false” to “validInput”.
              • Print the error message.
                • Otherwise,
                  • Assign “true” to “validInput”.
                • Break the case.
          • If the month number is 2,
            • Check if the day is less than 1 or greater than 29.
              • Assign “false” to “validInput”.
              • Print the error message.
                • Otherwise,
                  • Assign “true” to “validInput”.
                • Break the case.
      • Inside the “catch” block for “DayException”, print the error message.

“DayException.java”:

  • Define the exception class “DayException”.
    • Define a default constructor.
      • Call the parent class’s method.
    • Define two parameterized constructors.
      • Call the parent class’s method by passing a message.

“MonthException.java”:

  • Define the exception class “MonthException”.
    • Define a default constructor.
      • Call the parent class’s method.
    • Define two parameterized constructors.
      • Call the parent class’s method by passing a message.

Blurred answer
Students have asked these similar questions
Write a program that converts a time from 24-hour notation to 12-hour notation. To make the solution easier, a requirement is imposed on the input: It must be in xx:xx format, i.e. it must have two digits, a colon, and then another two digits. Define an exception class called TimeException. If the user enters an illegal time, like 10:65, or even gibberish, like 8&*68, your program should throw and handle a TimeException. Test your program with the file "times.txt" as input and store the result in the file "result.txt"   File times.txt: 00:00 12:00 12:01 11:59 23:59 24:00 10:65 3:23 1145 8&*68   File result.txt: #         24-hour           12-hour -------------------------------------------- 1          00:00               12:00 AM 2          12:00               12:00 PM 3          12:01                12:01 PM 4          11:59                11:59 AM 5          23:59               11:59 PM 6          24:00               Time Exception 7          10:65…
Write a program that prompts the user to enter a length in feet and then enter a length in inches and outputs the equivalent length in centimeters. If the user enters a negative number or a non-digit number, throw and handle an appropriate exception and prompt the user to enter another set of numbers. Your error message should read A non positive number is entered and allow the user to try again. Format your output with setprecision(2) to ensure the proper number of decimals for testing!
Write a class TimeOfDay that uses the exception classes defined in the previous exercise. Give it a method setTimeTo(timeString) that changes the time if timeString corresponds to a valid time of day. If not, it should throw an exception of the appropriate type. [Java]

Chapter 9 Solutions

Java: An Introduction To Problem Solving And Programming Plus Mylab Programming With Pearson Etext -- Access Card Package (8th Edition)

Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning