C++
/* Program Name: BadDate.cpp
Function: This program determines if a date entered by the user is valid.
Input: Interactive
Output: Valid date is printed or user is alerted that an invalid date was entered
*/
#include <iostream>
bool validateDate(int, int, int);
void housekeeping(int* year, int* month, int* day);
void endOfJob(bool);
using namespace std;
void housekeeping(int* year, int* month, int* day)
{
cout << "Please enter the year." << endl;
cin >> *year;
cout << "Please enter the month." << endl;
cin >> *month;
cout << "Please enter the day." << endl;
cin >> *day;
}
bool validateDate(int year, int month, int day)
{
bool validDate = true;
const int MIN_YEAR = 0, MIN_MONTH = 1, MAX_MONTH = 12, MIN_DAY = 1, MAX_DAY = 31;
if(year <= MIN_YEAR) // invalid year
validDate = false;
else if (month < MIN_MONTH || month > MAX_MONTH) // invalid month
validDate = false;
else if (day < MIN_DAY || day > MAX_DAY) // invalid day
validDate = false;
return validDate;
}
void endOfJob(bool validDate)
{
if(validDate)
{
// Output statement
cout << "is a valid date." << endl;
}
else
{
// Output statement
cout << "is an invalid date." << endl;
}
}
int main()
{
// Declare variables
int year;
int month;
int day;
const int MIN_YEAR = 0, MIN_MONTH = 1, MAX_MONTH = 12, MIN_DAY = 1, MAX_DAY = 31;
bool validDate = true;
// This is the work of the housekeeping() method
// Get the year, then the month, then the day
housekeeping(&year, &month, &day);
// This is the work of the detailLoop() method
// Check to be sure date is valid
validateDate(year, month, day);
// This is the work of the endOfJob() method
// test to see if date is valid and output date and whether it is valid or not
endOfJob(validateDate(year, month, day));
} // end of main() function
JAVA
/* Program Name: BadDate.java
Function: This program determines if a date entered by the user is valid.
Input: Interactive
Output: Valid date is printed or user is alerted that an invalid date was entered.
*/
import java.util.Scanner;
import java.util.Map;
public class BadDate
{
static boolean detailLoop(int year, int month, int day)
{
boolean validDate = true;
final int MIN_YEAR = 0, MIN_MONTH = 1, MAX_MONTH = 12, MIN_DAY = 1, MAX_DAY = 31;
// This is the work of the detailLoop() method
// Check to be sure date is valid
if( year <= MIN_YEAR ) // invalid year
validDate = false;
else if ( month < MIN_MONTH || month > MAX_MONTH ) // invalid month
validDate = false;
else if ( day < MIN_DAY || day > MAX_DAY ) // invalid day
validDate = false;
return validDate;
}
static void endOfJob(boolean validDate)
{
// This is the work of the endOfJob() method
// Test to see if date is valid and output date and whether it is valid or not
if(validDate)
{
// Output statement
System.out.println("is a valid date.");
}
else
{
// Output statement
System.out.println("is an invalid date.");
}
}
public static void main(String args[])
{
// Declare variables
String yearString;
String monthString;
String dayString;
int year=1;
int month=1;
int day=1;
boolean validDate = true;
final int MIN_YEAR = 0, MIN_MONTH = 1, MAX_MONTH = 12, MIN_DAY = 1, MAX_DAY = 31;
// This is the work of the housekeeping() method
// This is the work of the housekeeping() method
// Get the year, then the month, then the day
Scanner input= new Scanner(System.in);
// variable = input.nextLine();
// Convert Strings to integers
System.out.println("Enter year.");
yearString = input.nextLine();
year = Integer.parseInt(yearString);
System.out.println("Enter month.");
monthString = input.nextLine();
month = Integer.parseInt(monthString);
System.out.println("Enter day.");
dayString = input.nextLine();
day = Integer.parseInt(dayString);
// This is the work of the detailLoop() method
// Check to be sure date is valid
validDate = detailLoop( year, month, day);
// This is the work of the endOfJob() method
// Test to see if date is valid and output date and whether it is valid or not
endOfJob(validDate);
} // end of main() method
} // end of BadDate class
Python
# Program Name: BadDate.py
# Function: This program determines if a date entered by the user is valid.
# Input: Interactive
# Output: Valid date is printed or user is alerted that an invalid date was entered.
MIN_YEAR = 0
MIN_MONTH = 1
MAX_MONTH = 12
MIN_DAY = 1
MAX_DAY = 31
year = None
month = None
day = None
# Get the year, then the month, then the day
# housekeeping()
# def housekeeping(year, month, day):
year = input("Enter year.")
month = input("Enter month.")
day = input("Enter day.")
# Check to be sure date is valid
def check(year, month, day):
validDate = True
if int(year) <= MIN_YEAR: # invalid year
validDate = False
elif int(month) < MIN_MONTH or int(month) > MAX_MONTH: # invalid month
validDate = False
elif int(day) < MIN_DAY or int(day) > MAX_DAY: # invalid day
validDate = False
return validDate
# Test to see if date is valid and output date and whether it is valid or not
# endOfJob()
validDate = check(year, month, day)
if validDate:
# Output statement
print("is a valid date.")
else:
# Output statement
print("is an invalid date.")