Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PURPOSE: This assignment demonstrates the use of both conditional and loop state

ID: 3629828 • Letter: P

Question

PURPOSE: This assignment demonstrates the use of both conditional and loop statements.
PROCEDURES: You will write a program in which you enter a date from the keyboard and the program will print out the number of days that have elapsed since 1 January 1800. Be sure to include a program ID block. Make sure that you test your programs thoroughly.
PROGRAM: Your program is to be case insensitive. The calculation is to include the count of both 1 January 1800 and the entered date. The program will have a loop that allows multiple calculations to be run.

The program should give a description of the program operation to the user.

Below is a sample of the program’s input and output.

This program computes the number of days from 1 January 1800 to a given input date. Program description

Enter the day of the month (i.e. 1 - 31): 1
Enter the month (i.e. January, June, etc.): marCH
Enter the year (i.e. 1999, 2011, etc.): 1809
The date entered is 1 marCH 1809
The number of days since and including 1 January 1800 is 3347

More years to try (Yes or No): y

Enter the day of the month (i.e. 1 - 31): 23
Enter the month (i.e. January, June, etc.): JUNE
Enter the year (i.e. 1999, 2011, etc.): 2011
The date entered is 23 JUNE 2011
The number of days since and including 1 January 1800 is 77240

More years to try (Yes or No): nO

You are not allowed to use anything such as converting the it into milliseconds or anything like that. You can't use calendar.input. He said all of it is to be done with if else statements and counting.

Explanation / Answer

import java.io.*;
import java.util.Scanner;


public class Calendar
{
public static int findMonthNumber(String month)
{
if(month.equals("january"))
{
return 1;
}
if(month.equals("february"))
{
return 2;
}
if(month.equals("march"))
{
return 3;
}
if(month.equals("april"))
{
return 4;
}
if(month.equals("may"))
{
return 5;
}
if(month.equals("june"))
{
return 6;
}
if(month.equals("july"))
{
return 7;
}
if(month.equals("august"))
{
return 8;
}
if(month.equals("september"))
{
return 9;
}
if(month.equals("october"))
{
return 10;
}
if(month.equals("november"))
{
return 11;
}
if(month.equals("december"))
{
return 12;
}
return 0;
}
public static int calculateDays(int day, String month, int year)
{
int monthNumber = 0;
int totalDays = 0;
int leapDays = 0;
month = month.toLowerCase();
monthNumber = findMonthNumber(month);
int differenceYears = year - 1800 - 1;
while(differenceYears % 4 != 0) // years that don't give leap days
{
differenceYears--;
}
while(differenceYears > 0)
{
if(differenceYears >= 100) // 1900, 2000, 2100, 2200... check if leap year (2000, 2400...) or not leap year (1900, 2100...)
{
if(differenceYears % 200 == 0) // might be a non-leap year
{
if(differenceYears % 400 == 0) // 2200, 2600...
{
leapDays = leapDays; // no leap years every 100 years not divisible by 400
}
else // 2000, 2400...
{
leapDays++;
}
}
}
else // not a year ending in 00
{
leapDays++;
}
differenceYears = differenceYears - 4;
}
totalDays = leapDays + (year - 1800) * 365; // accumulate all days from previous years except current one; evaluate separately
leapDays = 0;
if(year % 4 == 0 && (year % 100 == 0 && year % 400 == 0)) // not 1900, 2100, 2200, etc....
{
leapDays = 1;
}
if(monthNumber == 1)
{
totalDays = day + totalDays;
}
else if(monthNumber == 2)
{
totalDays = 31 + day + totalDays;
}
else if(monthNumber == 3)
{
totalDays = 31 + 28 + day + leapDays + totalDays;
}
else if(monthNumber == 4)
{
totalDays = 31 + 28 + 31 + day + leapDays + totalDays;
}
else if(monthNumber == 5)
{
totalDays = 31 + 28 + 31 + 30 + day + leapDays + totalDays;
}
else if(monthNumber == 6)
{
totalDays = 31 + 28 + 31 + 30 + 31 + day + leapDays + totalDays;
}
else if(monthNumber == 7)
{
totalDays = 31 + 28 + 31 + 30 + 31 + 30 + day + leapDays + totalDays;
}
else if(monthNumber == 8)
{
totalDays = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day + leapDays + totalDays;
}
else if(monthNumber == 9)
{
totalDays = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day + leapDays + totalDays;
}
else if(monthNumber == 10)
{
totalDays = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day + leapDays + totalDays;
}
else if(monthNumber == 11)
{
totalDays = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day + leapDays + totalDays;
}
else if(monthNumber == 12)
{
totalDays = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day + leapDays + totalDays;
}
return totalDays;
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int day = 0;
String month = "";
int year = 0;
String date = "";
String again = "yes";
while(again.equalsIgnoreCase("yes"))
{
System.out.println("Enter the day of the month (i.e. 1 - 31): ");
day = in.nextInt();
in.nextLine();
System.out.println("Enter the month (i.e. January, June, etc.): ");
month = in.nextLine();
System.out.println("Enter the year (i.e. 1999, 2011, etc.): ");
year = in.nextInt();
System.out.println("The date entered is " + day + " " + month + " " + year);
System.out.println("The number of days since and including 1 January 1800 is " + calculateDays(day, month, year));
day = 0;
date = "";
year = 0;
System.out.println();
System.out.println("More years to try (Yes or No): ");
in.nextLine();
again = in.nextLine();
}

}
}