Question

Create a new project called Date In This new project you are going to design a...

Create a new project called Date In This new project you are going to design a class of Date. This mean that you need to create functions within the class to validate the Date for example: is it a leap year? How many days a months has? How many months are? Create a simple main menu capable of entering a date and validating a date. Instructions • Create a class of Date with attribues and member functions (remember the use of validation of dates) • Create a main menu with 2 options o (1) Enter date o (2) Exit • If the user selects the option #1 then the system asks for day, month and year and validates. If it is valid it displays in the console/ screen “Valid Date” followed by the date in the format of your choosing (day/month/year or MonthDescription day, year or year/month/day). If the date is not valid, the system displays “Not a valid date” and asks the user to re-enter the date until a valid one is entered. • If the user selects option #2 then the system exits • Remember to create a destructor, a constructor and the get and set for each attribute. The purpose of this lab is to create member functions capable of handling validations. Common functions are: IsValidDate and isLeapYear. For the function of Leap Year remember the standard formula.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class DateValidatation{
   //this is for validate the date entered by the user
public static boolean validateJavaDate(String inputDate)
{
   //mm/dd//yyyy this is the formate to check
   SimpleDateFormat sdfrmt = new SimpleDateFormat("MM/dd/yyyy");
   sdfrmt.setLenient(false);
   try
   {
       //parsing user input to required format
   Date javaDate = sdfrmt.parse(inputDate);
   System.out.println(inputDate+" is valid date format");
   }
   //if it not in the valid formate it will throw the exception while parsing
   catch (ParseException e)
   {
   System.out.println(inputDate+" is Invalid Date format");
   return false;
   }
   return true;
}
public static void main(String args[]){
   Scanner in = new Scanner(System.in);
// Display the menu
System.out.println("1) Enter Date");
System.out.println("2) Exit");

System.out.print("Please enter your choice: ");
  
//Get user's choice
int choice=in.nextInt();

//Display the title of the chosen module
switch (choice) {
case 1:
{
       //taking user input until it is correct
       while(true)
       {
       System.out.print("Please enter date(MM/dd/yyyy): ");
       Scanner sc = new Scanner(System.in);
       String date=sc.nextLine();
       if(validateJavaDate(date)){break;}
       }
       break;
}
//case 2 for exiiting the programme
case 2: System.out.println("Thank You Exit the programme...");
       System.exit(0);
break;
default: System.out.println("Invalid choice");
}//end of switch
}
}

1) Enter Date 2) Exit Please enter your choice: 1 Please enter date(MM/dd/yyyy): 12/09/2020 12/09/2020 is valid date format

Add a comment
Know the answer?
Add Answer to:
Create a new project called Date In This new project you are going to design a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 12.2 LAB 12.2: Classes (Date) C++ Rewrite the class definition of Date class (From Lab 12.1)...

    12.2 LAB 12.2: Classes (Date) C++ Rewrite the class definition of Date class (From Lab 12.1) including 4 added member functions: bool isLeapYear(): this function will return true if the year of the date is a leap year. int daysPassed(): This function will return how many days has been passed in this year. int daysLeft(): This function will return how many days has left of this year. int diff(Date AnotherDate): This function will return the difference between the calling object...

  • Design and implement a C++ class called Date that has the following private member variables month...

    Design and implement a C++ class called Date that has the following private member variables month (int) day (nt) . year (int Add the following public member functions to the class. Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day is...

  • CE – Return and Overload in C++ You are going to create a rudimentary calculator. The...

    CE – Return and Overload in C++ You are going to create a rudimentary calculator. The program should call a function to display a menu of three options: 1 – Integer Math 2 – Double Math 3 – Exit Program The program must test that the user enters in a valid menu option. If they do not, the program must display an error message and allow the user to reenter the selection. Once valid, the function must return the option...

  • Please write the program in C++ Problem Description: Design a class called Date. The class should...

    Please write the program in C++ Problem Description: Design a class called Date. The class should store a date in three integers: month, day, and year.     Create the necessary set and get methods.       Design member functions to return the date as a string in the following formats:                                     12/25/2012                                     December 25, 2012                                     25 December 2012 Input Validation: Do not accept values for day greater than 30 or less than 1. Do not accept values for month...

  • JAVA programing Question 1 (Date Class):                                   &nbsp

    JAVA programing Question 1 (Date Class):                                                                                                     5 Points Create a class Date with the day, month and year fields (attributes). Provide constructors that: A constructor that takes three input arguments to initialize the day, month and year fields. Note 1: Make sure that the initialization values for the day, month and year fields are valid where the day must be between 1 and 31 inclusive, the month between 1 and 12 inclusive and the year a positive number. Note 2:...

  • The class dateType is designed to implement the date in a program, but the member function...

    The class dateType is designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the data members. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and the year are checked before storing the date into the data members. Add a function member, isLeapYear, to check whether a year is a leap...

  • An ordinal date consists of a year and a day within it, both of which are...

    An ordinal date consists of a year and a day within it, both of which are integers. The year can be any year in the Gregorian calendar while the day within the year ranges from one, which represents January 1, through to 365 (or 366 if the year is a leap year) which represents December 31. Ordinal dates are convenient when computing differences between dates that are a specific number of days (rather than months). For example, ordinal dates can...

  • Write a class called Date that represents a date consisting of a year, month, and day....

    Write a class called Date that represents a date consisting of a year, month, and day. A Date object should have the following methods: public Date(int year, int month, int day) Constructs a new Date object to represent the given date. public void addDays(int days) Moves this Date object forward in time by the given number of days. public void addWeeks(int weeks) Moves this Date object forward in time by the given number of seven-day weeks. public int daysTo( Date...

  • - Classes Write a C++ program that creates a class called Date that includes three pieces...

    - Classes Write a C++ program that creates a class called Date that includes three pieces of information as data members, namely:  month (type int)  day (type int)  year (type int). Program requirements: - Provide set and a get member functions for each data member. - For the set member functions assume that the values provided for the year and day are correct, but in the setMonth member function ensure that the month value is in the...

  • C++ Programming Step 1. Design a new ADT that implements a class named Date, add 3 private member variables month, day, year along with their appropriate public constructor / getter / setter member fu...

    C++ Programming Step 1. Design a new ADT that implements a class named Date, add 3 private member variables month, day, year along with their appropriate public constructor / getter / setter member functions inside Date.h and Date.cpp. Step 2. Design another ADT that implements a class named Watch, add 3 private member variables hour, minute, second, along with their appropriate public constructor / getter / setter member functions inside Watch.h and Watch.cpp. Step 3. Next, implement the additional SmartWatch...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT