In: Computer Science
Summary In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you. Write the simulated housekeeping() method that contains input statements to retrieve a year, a month, and a day from the user. Add statements to the simulated housekeeping() method that convert the String representation of the year, month, and day to ints. Include the output statements in the simulated endOfJob() method. The format of the output is as follows: month/day/year is a valid date. or month/day/year is an invalid date. Execute the program entering the following date: month = 5, day = 32, year =2014 Observe the output of this program. Execute the program entering the following date: month = 9, day = 21, year = 2002 Observe the output of this program.
The current coding I have worked for the valid input portion, but not the invalid portion or the prompting for the user inputs section... help if you can!
/* 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; public class BadDate { //Made this static to accessible houseKeeping and endOfJob method static int year; static int month; static int day; static boolean validData = true; //this is main method which start program execution public static void main(String[] args) { houseKeeping(); endOfJob(); } /** * This method take user input using scanner class and validate as per requirement given */ public static void houseKeeping() { String yearString; String monthString; String dayString; final int MIN_YEAR =0,MIN_MONTH=1,MAX_MONTH=12,MIN_DAY=1,MAX_DAY=31; Scanner sc = new Scanner(System.in); System.out.print("Please enter Year - "); yearString = sc.next(); year = Integer.parseInt(yearString); if(year <= MAX_MONTH) { validData = false; } System.out.print("Please enter Month - "); monthString = sc.next(); month = Integer.parseInt(monthString); if(month < MIN_MONTH || month > MAX_MONTH) { validData = false; } System.out.print("Please enter Day - "); dayString = sc.next(); day = Integer.parseInt(dayString); if(day < MIN_DAY || day > MAX_DAY) { validData = false; } } /** * This method prints whether inputed date is valid or invalid */ public static void endOfJob() { System.out.println(month+"/"+day+"/"+year+ " is a "+ (validData ? "valid": "invalid") + " date."); } }
Below is the answer. If you have any doubts, let me know in the comments section. Please Upvote:)
Given a month date and year, print the output as of they were valid or not. You have completed the code for valid section. I have added a condition to check for invalid input. Generally, some months consists of 30 days and some months consists of 31 days. And days in february were changed according to leap year. Condition that check leap year is added in the program. Below is the code.
/* 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;
public class BadDate {
//Made this static to accessible houseKeeping and endOfJob method
static int year;
static int month;
static int day;
static boolean validData = true;
//this is main method which start program execution
public static void main(String[] args) {
houseKeeping();
endOfJob(); }
/** * This method take user input using scanner class and validate as per requirement given */
public static void houseKeeping() {
String yearString;
String monthString;
String dayString;
final int MIN_YEAR =0,MIN_MONTH=1,MAX_MONTH=12,MIN_DAY=1,MAX_DAY=31;
int validDateInMonth[] = new int[]{-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Scanner sc = new Scanner(System.in);
System.out.print("Please enter Year - ");
yearString = sc.next();
year = Integer.parseInt(yearString);
System.out.print("Please enter Month - ");
monthString = sc.next();
month = Integer.parseInt(monthString);
System.out.print("Please enter Day - ");
dayString = sc.next();
day = Integer.parseInt(dayString);
//this checks if a year is leap year. And change the days in month of february in validDateInMonth array
if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
validDateInMonth[2] = 29;
}
if(year <= MIN_YEAR || month < MIN_MONTH || month > MAX_MONTH || day < MIN_DAY || day > validDateInMonth[month])
{
validData = false;
}
}
/** * This method prints whether inputed date is valid or invalid */
public static void endOfJob() {
System.out.println(month+"/"+day+"/"+year+ " is a "+ (validData ? "valid": "invalid") + " date.");
} }
Below pasting the code screenshot for better understanding of indentation.
Output screenshots:
Consider a leap year 2000 and month as 2. In leap year, 2nd month have only 29 days. If day is given as 31, then it is invalid date.