In this assignment, you will write a Java program that repeatedly asks the user
ID: 3723126 • Letter: I
Question
In this assignment, you will write a Java program that repeatedly asks the user for a date (of the form m/d/y), and then either prints that the date is invalid or prints the date in "long" format (monthName day, year). Here is a sample run of the program (user input is in red):
Requirements
This project contains several methods, each of which contains documentation on what that method does or is supposed to do. You will need to complete the following methods:
public static String monthNumToName(int m)
public static String getLongFormat(int m, int d, int y)
public static boolean checkValidity(int m, int d, int y)
public static void main(String[] args)
I suggest completing the methods in the order listed above.
Assumptions
You may make the following assumptions in your program:
The user will always enter the date in the form m/d/y, where m, d, y are all integers
The user will always enter either y, Y, n, or N when asked if they want to go again
................................
Proj 5
import java.util.*;
public class Proj5 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
char more = 'y';
do {
//This code is done -- it gets the date and breaks it into month,day,year
System.out.print("Enter date (m/d/y): ");
String date = s.nextLine();
StringTokenizer st = new StringTokenizer(date, "/");
int month = Integer.parseInt(st.nextToken());
int day = Integer.parseInt(st.nextToken());
int year = Integer.parseInt(st.nextToken());
//Now if the user typed 2/27/2018, then month=2, day=27, and year=2018
//YOU DO THIS
//Call checkValidity to see if the date is valid
//If it isn't, print an error
//Otherwise
//Call getLongFormat to get the long format of this date
//Print the result from getLongFormat
//This code asks if the user wants to go again. It will loop while they enter y/Y.
System.out.print(" Go again (y/n)? ");
more = (s.nextLine()).charAt(0);
System.out.println();
} while (more == 'y' || more == 'Y');
}
/**
* Gets the month name for a given month number
*
* @param m The month number (1-12)
* @return The corresponding month name (January-December)
*/
public static String monthNumToName(int m) {
//YOU DO THIS
//return "January" if m is 1, "February" if m is 2, etc.
//leave this here in case m is not between 1-12
return "Invalid Month";
}
/**
* Gets the long format for a date (Month day, year)
*
* @param m The month
* @param d The day
* @param y The year
* @return The corresponding long format (Month day, year)
*/
public static String getLongFormat(int m, int d, int y) {
//YOU DO THIS
//Call monthNumToName to convert m to a String
//return the given date in long format
//(e.g., if m=1,d=1,y=2018, you would return "January 1, 2018")
//remove this return line after you finish the method
return "";
}
/**
* Gets whether the given year is a leap year
*
* @param y The year
* @return Whether y is a leap year
*/
public static boolean isLeapYear(int year) {
//This method is DONE!
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
return true;
}
else return false;
}
/**
* Gets whether a date is a valid date
*
* @param m The month
* @param d The day
* @param y The year
* @return Whether m/d/y is a valid date
*/
public static boolean checkValidity(int m, int d, int y) {
//YOU DO THIS
//if m is not between 1-12 - return false
//if y is negative - return false
//Use if/else if statements to see which month you are in (m)
//make sure the day (d) is >= 1 and <= the number of days in that month (see below)
//if it is - return true
//if not - return false
//month numbers with 31 days: 1,3,5,7,8,10,12
//month numbers with 30 days: 4,6,9,11
//February (m=2) usually has 28 days, unless it is a leap year (call isLeapYear to check)
//delete this return line after you are done
return false;
}
}
Explanation / Answer
//create project with name Proj5 and create Proj5.java class
import java.util.Scanner;
import java.util.StringTokenizer;
/**
*
* @author SDinesh
*/
public class Proj5 {
public static String monthNumToName(int m) {
if (m == 1) {
return "January";
} else if (m == 2) {
return "February";
} else if (m == 3) {
return "March";
} else if (m == 4) {
return "April";
} else if (m == 5) {
return "May";
} else if (m == 6) {
return "June";
} else if (m == 7) {
return "July";
} else if (m == 8) {
return "August";
} else if (m == 9) {
return "September";
} else if (m == 10) {
return "Ocotber";
} else if (m == 11) {
return "November";
} else if (m == 12) {
return "December";
}
return "Invalid Month";
}
public static String getLongFormat(int m, int d, int y) {
String date = m + "/" + d + "/" + y;
String month = monthNumToName(m);
String newFormat = month + " " + d + ", " + y;
if (!month.equals("Invalid Month")) {
return newFormat;
} else {
return date + " is invalid";
}
}
/**
*
* Gets whether the given year is a leap year
*
*
*
* @param y The year
*
* @return Whether y is a leap year
*
*/
public static boolean isLeapYear(int year) {
//This method is DONE!
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
return true;
} else {
return false;
}
}
/**
*
* Gets whether a date is a valid date
*
*
*
* @param m The month
*
* @param d The day
*
* @param y The year
*
* @return Whether m/d/y is a valid date *
*/
public static boolean checkValidity(int m, int d, int y) {
//YOU DO THIS
//if m is not between 1-12 - return false
if (m > 12 || m < 1) {
return false;
}
//if y is negative - return false
if (y < 0) {
return false;
}
//Use if/else if statements to see which month you are in (m)
if ((m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) && d <=31) {
return true;
} else if ((m == 4 || m == 6 || m == 9 || m == 11) && d <=30) {
return true;
}
if (m == 2 && isLeapYear(y) && d == 29) {
return true;
} else if (m == 2 && d == 28) {
return true;
} else {
return false;
}
//make sure the day (d) is >= 1 and <= the number of days in that month (see below)
//if it is - return true
//if not - return false
//month numbers with 31 days: 1,3,5,7,8,10,12
//month numbers with 30 days: 4,6,9,11
//February (m=2) usually has 28 days, unless it is a leap year (call isLeapYear to check)
//delete this return line after you are done
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
char more = 'y';
do {
//This code is done -- it gets the date and breaks it into month,day,year
System.out.print("Enter date (m/d/y): ");
String date = s.nextLine();
StringTokenizer st = new StringTokenizer(date, "/");
try {
int month = Integer.parseInt(st.nextToken());
int day = Integer.parseInt(st.nextToken());
int year = Integer.parseInt(st.nextToken());
//Now if the user typed 2/27/2018, then month=2, day=27, and year=2018
//YOU DO THIS
//Call checkValidity to see if the date is valid
if (checkValidity(month, day, year)) {
System.out.println("" + getLongFormat(month, day, year));
} else {
System.out.println(date + " is invalid");
}
//If it isn't, print an error
//Otherwise
//Call getLongFormat to get the long format of this date
//Print the result from getLongFormat
//This code asks if the user wants to go again. It will loop while they enter y/Y.
System.out.print(" Go again (y/n)? ");
more = (s.nextLine()).charAt(0);
System.out.println();
} catch (Exception e) {
System.out.println(date + " is invalid");
System.out.print(" Go again (y/n)? ");
more = (s.nextLine()).charAt(0);
}
} while (more == 'y' || more == 'Y');
}
}
//output
Enter date (m/d/y): 2/28/2018
February 28, 2018
Go again (y/n)? y
Enter date (m/d/y): 2/29/2018
2/29/2018 is invalid
Go again (y/n)? y
Enter date (m/d/y): 13/08/2018
13/08/2018 is invalid
Go again (y/n)? n