Java program that will provide the user with a menu of choices that they will se
ID: 3731362 • Letter: J
Question
Java program that will provide the user with a menu of choices that they will select from that will
end up calling and thus utilizing the methods you've written in the code for the assignment.
This program will utilize an API, which is a set of method calls provided by a class that allow you to easily access useful tasks without
having to know how those tasks are implemented, much like the methods available in the
Math class. But someone ultimately has to implement the code behind methods like Math.sqrt() For this assignment,
you will be creating a collection of methods that could be loosely grouped together as an API for date-related
tasks.
The final code should have outputs all like the following dependend on the inputs of the code.
The first should be a menu with the following options, and depending on the input will output the code responses that are following.
*** Date / Time Menu ***
========================
1. Date report
2. Days between
3. Day of year
4. Days remaining
5. Days in month
6. Month name to number
7. Check date validity
8. Milliseconds left
9. Quit
Enter a positive number up to 9: 0
Enter a positive number up to 9: 12
Enter a positive number up to 9: 999
Enter a positive number up to 9: -5
Enter a positive number up to 9: 9
Farewell!
*** Date / Time Menu ***
========================
1. Date report
<<OTHER MENU OPTIONS>>
9. Quit
Enter a positive number up to 9: 1
Enter the month as a number: 1
Enter the day as a number: 1
Enter the year as a number: 2000
Your DATE REPORT for January 01, 2000:
- It is Day 001 and there are 365 Days Remaining.
- There are 31 days in January, 2000.
- The year 2000 is a leap year.
- There are 31536000000 milliseconds remaining.
*** Date / Time Menu ***
========================
1. Date report
<<OTHER MENU OPTIONS>>
9. Quit
Enter a positive number up to 9: 1
Enter the month as a number: 13
Enter the day as a number: 5
Enter the year as a number: 2000
Your date 13/05/2000 is invalid.
*** Date / Time Menu ***
========================
1. Date report
<<OTHER MENU OPTIONS>>
9. Quit
Enter a positive number up to 9: 2
Enter the month as a number: 1
Enter the day as a number: 1
Enter the year as a number: 2000
Enter the month as a number: 1
Enter the day as a number: 1
Enter the year as a number: 2000
There are 000 days between 01/01 and 01/01 in the year 2000.
*** Date / Time Menu ***
========================
1. Date report
<<OTHER MENU OPTIONS>>
9. Quit
Enter a positive number up to 9: 2
Enter the month as a number: 1
Enter the day as a number: 1
Enter the year as a number: 2000
Enter the month as a number: 12
Enter the day as a number: 31
Enter the year as a number: 2000
There are 365 days between 01/01 and 12/31 in the year 2000.
*** Date / Time Menu ***
========================
1. Date report
<<OTHER MENU OPTIONS>>
9. Quit
Enter a positive number up to 9: 2
Enter the month as a number: 12
Enter the day as a number: 31
Enter the year as a number: 2000
Enter the month as a number: 1
Enter the day as a number: 1
Enter the year as a number: 2000
There are 365 days between 12/31 and 01/01 in the year 2000.
*** Date / Time Menu ***
========================
1. Date report
<<OTHER MENU OPTIONS>>
9. Quit
Enter a positive number up to 9: 2
Enter the month as a number: 13
Enter the day as a number: 1
Enter the year as a number: 2000
Enter the month as a number: 5
Enter the day as a number: 4
Enter the year as a number: 1977
One or more of your dates are invalid.
*** Date / Time Menu ***
========================
1. Date report
<<OTHER MENU OPTIONS>>
9. Quit
Enter a positive number up to 9: 2
Enter the month as a number: 2
Enter the day as a number: 28
Enter the year as a number: 2018
Enter the month as a number: 2
Enter the day as a number: 28
Enter the year as a number: 2019
Days must be in the same calendar year.
*** Date / Time Menu ***
========================
1. Date report
<<OTHER MENU OPTIONS>>
9. Quit
Enter a positive number up to 9: 3
Enter the month as a number: 4
Enter the day as a number: 1
Enter the year as a number: 2002
04/01/2002 is day 091.
*** Date / Time Menu ***
========================
1. Date report
<<OTHER MENU OPTIONS>>
9. Quit
Enter a positive number up to 9: 4
Enter the month as a number: 4
Enter the day as a number: 1
Enter the year as a number: 2002
There are 274 days remaining after 04/01/2002.
*** Date / Time Menu ***
========================
1. Date report
<<OTHER MENU OPTIONS>>
9. Quit
Enter a positive number up to 9: 5
Enter the month as a number: 4
There are 30 days in month 4.
*** Date / Time Menu ***
========================
1. Date report
<<OTHER MENU OPTIONS>>
9. Quit
Enter a positive number up to 9: 5
Enter the month: 23
23 is not a month.
*** Date / Time Menu ***
========================
1. Date report
<<OTHER MENU OPTIONS>>
9. Quit
Enter a positive number up to 9: 6
Enter the month name: JUNE
JUNE is month 6.
*** Date / Time Menu ***
========================
1. Date report
<<OTHER MENU OPTIONS>>
9. Quit
Enter a positive number up to 9: 6
Enter the month name: Octember
"Octember" is not a month.
*** Date / Time Menu ***
========================
1. Date report
<<OTHER MENU OPTIONS>>
9. Quit
Enter a positive number up to 9: 7
Enter the month as a number: 2
Enter the day as a number: 29
Enter the year as a number: 2000
Your date 02/29/2000 is valid.
*** Date / Time Menu ***
========================
1. Date report
<<OTHER MENU OPTIONS>>
9. Quit
Enter a positive number up to 9: 7
Enter the month as a number: 2
Enter the day as a number: 29
Enter the year as a number: 2018
Your date 02/29/2018 is invalid.
*** Date / Time Menu ***
========================
1. Date report
<<OTHER MENU OPTIONS>>
9. Quit
Enter a positive number up to 9: 8
Enter the month as a number: 6
Enter the day as a number: 30
Enter the year as a number: 2000
There are 15897600000 milliseconds remaining after 06/30/2000.
*** Date / Time Menu ***
========================
1. Date report
<<OTHER MENU OPTIONS>>
9. Quit
Enter a positive number up to 9: 8
Enter the month as a number: 12
Enter the day as a number: 31
Enter the year as a number: 2000
There are 0 milliseconds remaining after 12/31/2000.
Explanation / Answer
package com.chegg.vinoth;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Year;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import org.omg.Messaging.SyncScopeHelper;
public class DateAPI {
static final String[] monthString = {"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AGUST",
"SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};
static final int[] daysInMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
public static void dateReport(int day,int month,int year) {
String date = month +"/"+day+"/"+year;
SimpleDateFormat sdfrmt = new SimpleDateFormat("mm/dd/yyyy");
Date d = null;
sdfrmt.setLenient(false);
try {
d = sdfrmt.parse(date);
//System.out.println("Your date " + date + " is valid");
}catch(ParseException e) {
System.out.println("Your date " + date + " is valid");
}
//String dd = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.SHORT).format(d);
System.out.println("Your DATE REPORT for "+ monthString[month-1] + " "+ day + " , " + year +":");
System.out.println("It's day " + dayOfYear(day, month, year)+ " and " + daysRemaining(day, month, year) +" Days remaining");
System.out.println("There are " + daysInMonth[month-1] + " days in " + monthString[month-1] +" " +year);
System.out.println("The year " + (Year.isLeap(year) ? " is a leap year" : " is not a leap year"));
System.out.println("There are " + millisecondsRemaining(day, month, year) + " remaining");
}
public static int daysBetween(int day1,int month1,int year1,int day,int month,int year) {
// Call day og year to fin out two day then subtract
int remaing = dayOfYear(day1, month1, year1) - dayOfYear(day, month, year);
return remaing;
}
public static int dayOfYear(int day,int month,int year) {
GregorianCalendar gc = new GregorianCalendar();
gc.set(Calendar.DAY_OF_MONTH, day);
gc.set(Calendar.MONTH, month-1); //
gc.set(Calendar.YEAR, year);
int numberofDaysPassed = gc.get(Calendar.DAY_OF_YEAR);
return numberofDaysPassed;
}
public static int daysRemaining(int day,int month,int year) {
String date = month +"/"+day+"/"+year;
SimpleDateFormat sdfrmt = new SimpleDateFormat("MM/dd/yyyy");
Date d = null;
sdfrmt.setLenient(false);
try {
d = sdfrmt.parse(date);
//System.out.println("Your date " + date + " is valid");
}catch(ParseException e) {
//System.out.println("Your date " + date + " is valid");
}
Calendar cal = Calendar.getInstance();
cal.setTime(d);
// Use Day of year to dind the no of days in the year then current no to subtract
int numOfDays = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
return numOfDays - dayOfYear(day, month, year);
}
public static void daysInMonth(int month) {
// Values stored in array. Retrun based on index. Since array starts @0 add 1 then find
if(month<=12 && month>0)
System.out.println("There are " + daysInMonth[month-1] +" days in month " + month);
else
System.out.println(month+" is not a valid month");
}
public static int monthToNumber(String month) {
// Iterate over he array to find out te index. of the month
List<String> m = Arrays.asList(monthString);
int number =0;
Iterator<String> it = m.iterator();
while(it.hasNext())
{
if(it.next().equalsIgnoreCase(month))
{
return number;
}
number++;
}
return number;
}
public static boolean dateValidity(int day,int month,int year) {
String date = month +"/"+day+"/"+year;
SimpleDateFormat sdfrmt = new SimpleDateFormat("MM/dd/yyyy");
sdfrmt.setLenient(false);
try {
// Use data formator to validate the date
Date d = sdfrmt.parse(date);
//System.out.println("Your date " + date + " is valid");
return true;
}catch(ParseException e) {
// If any exception then format is not valid
System.out.println("Your date " + date + " is valid");
return false;
}
}
public static BigInteger millisecondsRemaining(int day,int month,int year) {
//Use bigint since we willl have bigger number which cant put in int
BigInteger bi = new BigInteger((365*24*60*30)+"");
BigInteger bi1 = new BigInteger((365 - (dayOfYear(day, month, year))*24*60*30) + "");
return bi.subtract(bi1);
}
public static void main(String[] args) {
do {
//Menu for user to select
System.out.print(" ");
System.out.print(" ");
System.out.println("*** Date / Time Menu ***");
System.out.println("========================");
System.out.println("1. Date report");
System.out.println("2. Days between");
System.out.println("3. Day of year");
System.out.println("4. Days remaining");
System.out.println("5. Days in month");
System.out.println("6. Month name to number");
System.out.println("7. Check date validity");
System.out.println("8. Milliseconds left");
System.out.println("9. Quit");
System.out.println("Enter your choice: ");
Scanner scan = new Scanner(System.in);
int option = scan.nextInt();
switch(option) {
case 1:
System.out.print("Enter month as number: ");
int month = scan.nextInt();
System.out.print("Enter day as number: ");
int day = scan.nextInt();
System.out.print("Enter year as number: ");
int year = scan.nextInt();
if(!dateValidity(day, month, year))
{
System.out.println("Date is not valid");
}else
dateReport(day,month,year);
break;
case 2:
System.out.print("Enter month as number: ");
month = scan.nextInt();
System.out.print("Enter day as number: ");
day = scan.nextInt();
System.out.print("Enter year as number: ");
year = scan.nextInt();
System.out.print("Enter month as number: ");
int month1 = scan.nextInt();
System.out.print("Enter day as number: ");
int day1 = scan.nextInt();
System.out.print("Enter year as number: ");
int year1 = scan.nextInt();
if(!dateValidity(day, month, year) && !dateValidity(day1, month1, year1))
{
System.out.println("Date is not valid");
}else
System.out.println("There are " + Math.abs(daysBetween(day, month, year, day1, month1, year1)) + " days between " + month +"/" + day +"/" + year +" and " + month1+"/" + day1 +"/" +year);
break;
case 3:
System.out.println("Enter month as number: ");
month = scan.nextInt();
System.out.println("Enter day as number: ");
day = scan.nextInt();
System.out.println("Enter year as number: ");
year = scan.nextInt();
if(!dateValidity(day, month, year))
{
System.out.println("Date is not valid");
}else
System.out.println(month+"/"+day+"/"+year +" is " + dayOfYear(day, month, year) +"of the year");
break;
case 4:
//Scanner scan = new Scanner(System.in);
System.out.println("Enter month as number: ");
month = scan.nextInt();
System.out.println("Enter day as number: ");
day = scan.nextInt();
System.out.println("Enter year as number: ");
year = scan.nextInt();
if(!dateValidity(day, month, year))
{
System.out.println("Date is not valid");
}else
System.out.println("There are " + daysRemaining(day, month, year) + " days remaining");
break;
case 5:
System.out.print("Enter the month as a number: " );
DateAPI.daysInMonth(scan.nextInt());
break;
case 6:
System.out.print("Enter month as string: ");
String m = scan.next();
System.out.println(m + " is month " + (monthToNumber(m)+1));
break;
case 7:
System.out.println("Enter month as number: ");
month = scan.nextInt();
System.out.println("Enter day as number: ");
day = scan.nextInt();
System.out.println("Enter year as number: ");
year = scan.nextInt();
if(dateValidity(day, month, year))
System.out.println("Your date " + " is valid");
else
System.out.println("Your date " + " is not valid");
break;
case 8:
System.out.println("Enter month as number: ");
month = scan.nextInt();
System.out.println("Enter day as number: ");
day = scan.nextInt();
System.out.println("Enter year as number: ");
year = scan.nextInt();
if(!dateValidity(day, month, year))
{
System.out.println("Date is not valid");
}else
System.out.println("There are " + millisecondsRemaining(day, month, year) +" millisconds left");
break;
case 9:
System.exit(0);
break;
}
}while(true);
}
}
===================================
*** Date / Time Menu ***
========================
1. Date report
2. Days between
3. Day of year
4. Days remaining
5. Days in month
6. Month name to number
7. Check date validity
8. Milliseconds left
9. Quit
Enter your choice:
1
Enter month as number: 5
Enter day as number: 12
Enter year as number: 2018
Your DATE REPORT for MAY 12 , 2018:
It's day 132 and 233 Days remaining
There are 31 days in MAY 2018
The year is not a leap year
There are 21470035 remaining
*** Date / Time Menu ***
========================
1. Date report
2. Days between
3. Day of year
4. Days remaining
5. Days in month
6. Month name to number
7. Check date validity
8. Milliseconds left
9. Quit
Enter your choice:
2
Enter month as number: 3
Enter day as number: 18
Enter year as number: 2018
Enter month as number: 3
Enter day as number: 21
Enter year as number: 2018
There are 3 days between 3/18/2018 and 3/21/2018
*** Date / Time Menu ***
========================
1. Date report
2. Days between
3. Day of year
4. Days remaining
5. Days in month
6. Month name to number
7. Check date validity
8. Milliseconds left
9. Quit
Enter your choice:
3
Enter month as number:
5
Enter day as number:
21
Enter year as number:
2018
5/21/2018 is 141of the year
*** Date / Time Menu ***
========================
1. Date report
2. Days between
3. Day of year
4. Days remaining
5. Days in month
6. Month name to number
7. Check date validity
8. Milliseconds left
9. Quit
Enter your choice:
4
Enter month as number:
12
Enter day as number:
29
Enter year as number:
2018
There are 2 days remaining
*** Date / Time Menu ***
========================
1. Date report
2. Days between
3. Day of year
4. Days remaining
5. Days in month
6. Month name to number
7. Check date validity
8. Milliseconds left
9. Quit
Enter your choice:
5
Enter the month as a number: 5
There are 31 days in month 5
*** Date / Time Menu ***
========================
1. Date report
2. Days between
3. Day of year
4. Days remaining
5. Days in month
6. Month name to number
7. Check date validity
8. Milliseconds left
9. Quit
Enter your choice:
6
Enter month as string: APRIL
APRIL is month 4
*** Date / Time Menu ***
========================
1. Date report
2. Days between
3. Day of year
4. Days remaining
5. Days in month
6. Month name to number
7. Check date validity
8. Milliseconds left
9. Quit
Enter your choice:
7
Enter month as number:
2
Enter day as number:
29
Enter year as number:
2018
Your date 2/29/2018 is valid
Your date is not valid
*** Date / Time Menu ***
========================
1. Date report
2. Days between
3. Day of year
4. Days remaining
5. Days in month
6. Month name to number
7. Check date validity
8. Milliseconds left
9. Quit
Enter your choice:
8
Enter month as number:
3
Enter day as number:
21
Enter year as number:
2018
There are 19223635 millisconds left
*** Date / Time Menu ***
========================
1. Date report
2. Days between
3. Day of year
4. Days remaining
5. Days in month
6. Month name to number
7. Check date validity
8. Milliseconds left
9. Quit
Enter your choice:
9
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.