In this assignment, you will write a java program that repeatedly asks the user
ID: 3723884 • 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 (monthName day, year). here is a sample of the program User input is bold
Enter date (m/d/y): 2/27/2018
2/27/2018 is February 27, 2018
Go again (y/n)? y
Enter date (m/d/y): 4/31/2018
4/31/2018 is invalid.
Go again (y/n)? n
---end
Requirements
This program should contain a single class called Proj5
This project should contain several methods
public static String monthNumToName(int m)
public static String getLongFormat(int m, int d, int y)
public static boolean chechValidity(int m, int d, int y)
public static void main(String[] args)
Assumptions
The user will always enter the date in the form m/d/y, where m, d, y are all integerrs
The user will always enter either y, Y, n or N when asked if they want to go again
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
import java.util.Scanner;
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 "October";
else if(m == 11)
return "November";
else if(m == 12)
return "December";
else
return "UNKNOWN";
}
public static String getLongFormat(int m, int d, int y)
{
return monthNumToName(m) + " " + d + ", " + y;
}
public static boolean chechValidity(int m, int d, int y)
{
boolean valid = true;
if(m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
{
if(d < 1 || d > 31)
valid = false;
}
else if(m == 4 || m == 6 || m == 9 || m == 11)
{
if(d < 1 || d > 30)
valid = false;
}
else if(m == 2)
{
//check leap
int febDays = 28;
if(y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
febDays++;
if(d < 1 || d > febDays)
valid = false;
}
else //invalid month
valid = false;
return valid;
}
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String date;
String ans = "y";
int d, m, y;
int index1, index2;
while(ans.equalsIgnoreCase("y"))
{
System.out.print("Enter date(m/d/y): ");
date = keyboard.nextLine();
index1 = date.indexOf('/');
m = Integer.parseInt(date.substring(0, index1)); //extract month part
index2 = date.indexOf('/', index1+1);
d = Integer.parseInt(date.substring(index1+1, index2)); //extract day
y = Integer.parseInt(date.substring(index2+1)); //extract year
if(chechValidity(m, d, y))
System.out.println(date + " is " + getLongFormat(m, d, y));
else
System.out.println(date + " is invalid.");
System.out.print("Go again y/n ? ");
ans = keyboard.nextLine();
}
}
}
-output---
Enter date(m/d/y): 2/27/2018
2/27/2018 is February 27, 2018
Go again y/n ? y
Enter date(m/d/y): 4/31/2018
4/31/2018 is invalid.
Go again y/n ? n