Write a Java application program to generate a calendar for the year 2019 from January to December. Give users the option to display 1 month, 3 months, or 12 months at a time.
For 1 month - The user will need to be able to type what month they want to display
For 3 months - The user will need to be able to type in the starting month
For 12 months - All months will need to be visible on the user's screen
Include exit option to terminate the program
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Calendar2019 {
public static String[] months = {
"", // first element in the array is left blank so that
"January", "February", "March", //we can get January at index 1,
not 0
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
public static int[] days = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
public static String menuInput;
public static int menuChoice;
public static int startingMonth, endingMonth;
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
do
{
displayMenu();
menuInput = br.readLine(); //First, we take the input into a normal
string variable
while(!isDigit(menuInput)) //Then, we check whether the input is a
digit or not
{ //If not, the while loop continues till a valid digit is
entered
System.out.print("Enter your selection (Only numbers are
allowed):");
menuInput = br.readLine(); //Again taking input in a normal string
variable and checking for the isDigit(..) condition
}
menuChoice = Integer.parseInt(menuInput); //Finally, if a digit is
entered, we convert the input into integer
menuInput = ""; //Then we need to set the variable to null to make
it ready for further inputs and this procedure is followed through
the rest of the code!!
switch(menuChoice)
{
case 1:
System.out.print("\nEnter month index (1 for January and 12 for
December): ");
menuInput = br.readLine();
while(!isDigit(menuInput))
{
System.out.print("\nEnter month index (1 for January and 12 for
December): ");
menuInput = br.readLine();
}
startingMonth = Integer.parseInt(menuInput);
menuInput = "";
showCalForOneMonth(startingMonth);
break;
case 2:
System.out.print("\nEnter month index to start (1 for January and
12 for December): ");
menuInput = br.readLine();
while(!isDigit(menuInput))
{
System.out.print("\nEnter month index to start (1 for January and
12 for December): ");
menuInput = br.readLine();
}
startingMonth = Integer.parseInt(menuInput);
menuInput = "";
showCalForThreeMonths(startingMonth);
break;
case 3:
System.out.println("Displaying calendar for the whole
year:");
showCalForAllMonths();
break;
case 4:
System.out.println("Exiting with status code OK");
System.exit(0);
break;
default:
System.out.println("Invalid option selected, exiting
application!");
}
}while(menuChoice >= 1 && menuChoice <= 4);
}
public static void displayMenu()
{
System.out.print("Welcome to the Calendar for the year
2019\n-------------------------------------------------\n"
+ "1. Show Calendar for 1 month\n2. Show Calendar for 3 months\n3.
Show Calendar for the year\n"
+ "4. Exit\n"
+ "Enter your selection (Only numbers are allowed): ");
}
public static boolean isDigit(String s)
{
try
{
Integer.parseInt(s);
return true;
}catch(NumberFormatException e){
System.out.println(s + " is not a valid number!");
return false;
}
}
public static void showCalForOneMonth(int month)
{
if(month >= 1 && month <= 12)
{
System.out.println(" " + months[month] + " 2019");
System.out.println(" S M Tu W Th F S");
int day = daysOfTheMonth(month, 1, 2019);
for (int i = 0; i < day; i++)
{
System.out.print(" ");
}
for (int i = 1; i <= days[month]; i++)
{
System.out.printf("%2d ", i);
if (((i + day) % 7 == 0) || (i == days[month]))
System.out.println();
}
System.out.println();
}
else
{
System.out.println("Please enter a valid month index (1 -
12)!\n");
}
}
public static void showCalForThreeMonths(int startMonth)
{
if(startMonth >= 1 && startMonth <= 12)
{
//Displaying the 1st Month
System.out.println(" " + months[startMonth] + " 2019");
System.out.println(" S M Tu W Th F S");
int daysOfFirstMonth = daysOfTheMonth(startMonth, 1, 2019);
for (int i = 0; i < daysOfFirstMonth; i++)
{
System.out.print(" ");
}
for (int i = 1; i <= days[startMonth]; i++)
{
System.out.printf("%2d ", i);
if (((i + daysOfFirstMonth) % 7 == 0) || (i ==
days[startMonth]))
System.out.println();
}
System.out.println();
//Displaying the 2nd Month
startMonth += 1;
System.out.println(" " + months[startMonth] + " 2019");
System.out.println(" S M Tu W Th F S");
int daysOfSecondMonth = daysOfTheMonth(startMonth, 1, 2019);
for (int i = 0; i < daysOfSecondMonth; i++)
{
System.out.print(" ");
}
for (int i = 1; i <= days[startMonth]; i++)
{
System.out.printf("%2d ", i);
if (((i + daysOfSecondMonth) % 7 == 0) || (i ==
days[startMonth]))
System.out.println();
}
System.out.println();
//Displaying the 3rd Month
startMonth += 1;
System.out.println(" " + months[startMonth] + " 2019");
System.out.println(" S M Tu W Th F S");
int daysOfThirdMonth = daysOfTheMonth(startMonth, 1, 2019);
for (int i = 0; i < daysOfThirdMonth; i++)
{
System.out.print(" ");
}
for (int i = 1; i <= days[startMonth]; i++)
{
System.out.printf("%2d ", i);
if (((i + daysOfThirdMonth) % 7 == 0) || (i ==
days[startMonth]))
System.out.println();
}
System.out.println();
}
else
{
System.out.println("Please enter a valid month index (1 -
12)!\n");
}
}
public static void showCalForAllMonths()
{
int startMonth = 1;
while(startMonth <= 12)
{
System.out.println(" " + months[startMonth] + " 2019");
System.out.println(" S M Tu W Th F S");
int daysOfThirdMonth = daysOfTheMonth(startMonth, 1, 2019);
for (int i = 0; i < daysOfThirdMonth; i++)
{
System.out.print(" ");
}
for (int i = 1; i <= days[startMonth]; i++)
{
System.out.printf("%2d ", i);
if (((i + daysOfThirdMonth) % 7 == 0) || (i ==
days[startMonth]))
System.out.println();
}
startMonth++;
System.out.println();
}
}
public static int daysOfTheMonth(int month, int day, int
year)
{
int y = year - (14 - month) / 12;
int x = y + y/4 - y/100 + y/400;
int m = month + 12 * ((14 - month) / 12) - 2;
int d = (day + x + (31*m)/12) % 7;
return d;
}
}
Write a Java application program to generate a calendar for the year 2019 from January to...
java program
QUESTION 2: 1. Write a do-while loop that asks the user to select a task from the following menu to continue: 1. Option 1 2. Option 2 3. Option 3 4. Option 4 5. Exit Read the selection from the keyboard then write the switch statement: For option 1, display the message "Do the option 1" For option 2, display the message "Do the option 2" For option 3, display the message "Do the option 3" For option...
Write a C# program that prints a calendar for a given year. Call this program calendar. The program prompts the user for two inputs: 1) The year for which you are generating the calendar. 2) The day of the week that January first is on, you will use the following notation to set the day of the week: 0 Sunday 1 Monday 2 Tuesday 3 Wednesday 4 Thursday 5 Friday 6 Saturday Your program should...
Write a C# program that prints a calendar for a given year. Call this program calendar. This program needs to use Switch Case in order to call the methods and format to print each month. The program prompts the user for two inputs: 1) The year for which you are generating the calendar. 2) The day of the week that January first is on, you will use the following notation to set the day of the week: ...
Using Java IDE: Write an application that asks elementary students a set of 10 math problems ● First ask the user for a level and a problem type. ● You need to validate the level and problem type and loop until the user enters a correct one. ● There should be 3 levels. Level 1 operands would have values in the range of 0-9, level 2 operands would have values in the range of 0-99, and level 3 operands would...
what is the solution for this Java project?
Create the an application that can use different types of Linked List to manage either Checking Account or Saving Account. The application should allow users can select the type of Linked List to work on. After finishing one, users can select to work with other type of Linked List until they want to exit Singly Linked List Singly Linked List with Iterator Java Linked List with Iterator 1. 2. 3. You can...
In ruby Write a program that prompts the user to enter a year and first day of the year, and displays the calendar table of that year. For example, if the user entered the year 2019, and 2 for Tuesday, January 1, 2019, your program should display the calendar for each month in the year 2019.
Requirement Write pseudocode and translate it to ONE C-program for each the following problems. In your pseudocode and C-program, use only what you have learned in this class so far. (Menu) Design a menu for question 2 and 3. So far, you use one program to solve all lab questions. But some of you may feel awkward when you want to demo/test only one lab question. To overcome that, your program should show a menu so that the users of...
Please make a JAVA program for the following using switch structures Write a program that simulates a simple Calculator program. The program will display menu choices to the user to Add, Subtract, Multiply and Divide. The program will prompt the user to make a selection from the choices and get their choice into the program. The program will use a nested if….else (selection control structure) to determine the user’s menu choice. Prompt the user to enter two numbers, perform the...
Write a program for the management of a bookstore in java, WITHOUT THE USE OF BOOK CLASS, just arrays, strings, loops, etc. Overview; First menu 1. List all books 2. Search all books 3. Purchase books 4. Exit If option 1 is chosen: List all books by Title, Author, Price, Copies, Category within a group of arrays If option 2 is chosen: Search all books by Title, Author, Price, Copies, Category If option 3 is chosen: List books, List shopping...
Hello need assistance with Java program: You are asked to create a program that will be used to create quizzes and test users based on these quizzes. 1. Prompt a secret word and proceed if it matches a secret code. 2. Your program should have two modules: quiz creation mode and test mode. User will be prompted in the beginning to choose from these two modes. For example: “Welcome to the quiz master: please enter 1 if you want to...