Question

In: Computer Science

You will write a Java Application program to perform the task of generating a calendar for...

You will write a Java Application program to perform the task of generating a calendar for the year 2020. You are required to modularize your code, i.e. break your code into different modules for different tasks in the calendar and use method calls to execute the different modules in your program. Your required to use arrays, ArrayList, methods, classes, inheritance, control structures like "if else", switch, compound expressions, etc. where applicable in your program. Your program should be interactive and able to display the calendar for 1 month, 3 months and all 12 months at the same time depending on the choice picked by the user.

Write a Java application program to generate the calendar for the year 2019 from January to December. The program will be menu driven, The menu items will include the following:

  • Year for the calendar of interest (2020)
  • How many Months do you want to display (1 month, 3 months, 12 months).
    • For 1 month
      • Display calendar for 1 month
      • The user will need to be able to type what month they want to display
    • For 3 months
      • Display calendar 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
  • Exit
    • This will terminate the program

Your solution should follow the format below:

  1. Modularize your code by using different methods for different tasks in the program like checking for leap year, number of days in a month, first day of the week for a month, printing each month, etc.
  2. You may use Arrays 1-dimensional, 2-dimensional arrays and ArrayList where appropriate
  3. Write a method to test for leap year
  4. Write a method to find the first day of the week for January
  5. Use method calls and/or parameter passing
  6. Use other methods where appropriate
  7. Add comments to your code (e.g at the beginning of every method and any other part of the program for documentation purposes and to make your code readable)

BELOW IS THE CODE:

import java.util.Scanner;

   public class PrintCalendar {

     /** Main method */

     public static void main(String[] args) {
     Scanner scan = new Scanner (System.in);
      int month=4;
     //Prompt the user to enter year
     System.out.print("Year for the calendar of interest : ");
     int year = scan.nextInt();
   
      //Prompt the user to enter choice for month
     System.out.print("How many months u want to display :Enter 1. for One Month, 2. for Three Months, 3. for Twelve Months, 4. for Exit : ");
     int ch = scan.nextInt();

   
     // Prompt the user to enter month if choice is 1
     if (ch == 1){
     System.out.print("Enter month in number between 1 and 12: ");
     month = scan.nextInt();}
   
     // Prompt the user to enter month if choice is 1
     if (ch == 2){
     System.out.print("Enter starting month in number between 1 and 12: ");
     month = scan.nextInt();}
   

     // Print calendar for the month of the year
      if (month < 1 || month > 12 || year < 1980)
       System.out.println("Wrong input!");
       else
           {
             if (ch ==1)        
             printMonth(year, month);
             if (ch == 2)
             {
               printMonth(year, month);
               printMonth(year, month+1);
               printMonth(year, month+2);
             }
             if (ch == 3)
             {
               for(int k=1;k<=12;k++)
               printMonth(year, k);
             }
             if (ch == 4)
             {
               System.out.println("Exiting");
             }
           }
   }
    /** Print the calendar for a month in a year */

     static void printMonth(int year, int month) {

     //Print the headings of the calendar
      printMonthTitle(year, month);

     //Print the body of the calendar
      printMonthBody(year, month);
    }

    /** Print the month title, e.g., May, 1999 */

    static void printMonthTitle(int year, int month) {

    System.out.println("         " + getMonthName(month) + " " + year + " Calendar");
        System.out.println(" Sun Mon Tue Wed Thu Fri Sat ");

    }

    /** Get the English name for the month */
    static String getMonthName(int month) {
      String monthName = null;
      switch (month) {
        case 1: monthName = "January"; break;
        case 2: monthName = "February"; break;
        case 3: monthName = "March"; break;
        case 4: monthName = "April"; break;
        case 5: monthName = "May"; break;
        case 6: monthName = "June"; break;
        case 7: monthName = "July"; break;
        case 8: monthName = "August"; break;
        case 9: monthName = "September"; break;
        case 10: monthName = "October"; break;
        case 11: monthName = "November"; break;
        case 12: monthName = "December";
      }
      return monthName;
    }

    /** Print month body */
    static void printMonthBody(int year, int month) {

       // Get start day of the week for the first date in the month
      int startDay = getStartDay(year, month);

      // Get number of days in the month
      int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);

      // Pad space before the first day of the month
      int i = 0;
      for (i = 0; i < startDay; i++)
        System.out.print("    ");
      for (i = 1; i <= numberOfDaysInMonth; i++) {
        if (i < 10)
          System.out.print("   " + i);
        else
          System.out.print(" " + i);
        if ((i + startDay) % 7 == 0)
          System.out.println();
      }
      System.out.println();
    }

    /** Get the start day of the first day in a month */

   static int getStartDay(int year, int month) {

      //Get total number of days since 1/1/1800
      int startDay1800 = 3;
      int totalNumberOfDays = getTotalNumberOfDays(year, month);

      //Return the start day
      return (totalNumberOfDays + startDay1800) % 7;
    }

    /** Get the total number of days since January 1, 1800 */

    static int getTotalNumberOfDays(int year, int month) {
     int total = 0;

     //Get the total days from 1800 to year - 1
     for (int i = 1800; i < year; i++)
     if (isLeapYear(i))
        total = total + 366;
      else
        total = total + 365;

      //Add days from January to the month prior to the calendar month
      for (int i = 1; i < month; i++)
        total = total + getNumberOfDaysInMonth(year, i);

      return total;
    }

    /** Get the number of days in a month */

    static int getNumberOfDaysInMonth(int year, int month) {
      if (month == 1 || month == 3 || month == 5 || month == 7 ||
        month == 8 || month == 10 || month == 12)
        return 31;

      if (month == 4 || month == 6 || month == 9 || month == 11)
        return 30;

      if (month == 2) return isLeapYear(year) ? 29 : 28;

      return 0; // If month is incorrect
    }

    /** Determine if it is a leap year */
    static boolean isLeapYear(int year) {
      return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
    }
}

The solution for the problem is available AS ABOVE but AFTER RUNNING IT the menu doesn't look good and after running a selection, it's not going back to the menu for the USER To INPUT next choice until user chooses to exit. RUN IT AND SEE so Can you HELP modify it to output like below:

OR help with a fresh code. Thanks

include these on the menu as per question:

  • Year for the calendar of interest (2020)
  • How many Months do you want to display (1 month, 3 months, 12 months).

Welcome to the Calendar for the year 2019
-------------------------------------------------
1. Show Calendar for 1 month
2. Show Calendar for 3 months
3. Show Calendar for the year
4. Exit
Enter your selection (Only numbers are allowed):

Solutions

Expert Solution

If you have any doubts, please give me comment...

import java.util.Scanner;

public class PrintCalendar {

/** Main method */

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

int month = 4;

// Prompt the user to enter year

System.out.print("Year for the calendar of interest : ");

int year = scan.nextInt();

int ch;

// Prompt the user to enter choice for month

System.out.println("How many Months do you want to display (1 month, 3 months, 12 months).");

do {

System.out.println("Welcome to the Calendar for the year " + year);

System.out.println("-------------------------------------------------");

System.out.println("1. Show Calendar for 1 month");

System.out.println("2. Show Calendar for 3 months");

System.out.println("3. Show Calendar for the year");

System.out.println("4. Exit");

System.out.println("Enter your selection (Only numbers are allowed):");

ch = scan.nextInt();

// Prompt the user to enter month if choice is 1

if (ch == 1) {

System.out.print("Enter month in number between 1 and 12: ");

month = scan.nextInt();

}

// Prompt the user to enter month if choice is 1

else if (ch == 2) {

System.out.print("Enter starting month in number between 1 and 12: ");

month = scan.nextInt();

}

// Print calendar for the month of the year

if (month < 1 || month > 12 || year < 1980)

System.out.println("Wrong input!");

else {

if (ch == 1)

printMonth(year, month);

else if (ch == 2) {

printMonth(year, month);

printMonth(year, month + 1);

printMonth(year, month + 2);

} else if (ch == 3) {

for (int k = 1; k <= 12; k++)

printMonth(year, k);

} else if (ch == 4) {

System.out.println("Exiting");

}

}

} while (ch != 4);

}

/** Print the calendar for a month in a year */

static void printMonth(int year, int month) {

// Print the headings of the calendar

printMonthTitle(year, month);

// Print the body of the calendar

printMonthBody(year, month);

}

/** Print the month title, e.g., May, 1999 */

static void printMonthTitle(int year, int month) {

System.out.println(" " + getMonthName(month) + " " + year + " Calendar");

System.out.println(" Sun Mon Tue Wed Thu Fri Sat ");

}

/** Get the English name for the month */

static String getMonthName(int month) {

String monthName = null;

switch (month) {

case 1:

monthName = "January";

break;

case 2:

monthName = "February";

break;

case 3:

monthName = "March";

break;

case 4:

monthName = "April";

break;

case 5:

monthName = "May";

break;

case 6:

monthName = "June";

break;

case 7:

monthName = "July";

break;

case 8:

monthName = "August";

break;

case 9:

monthName = "September";

break;

case 10:

monthName = "October";

break;

case 11:

monthName = "November";

break;

case 12:

monthName = "December";

}

return monthName;

}

/** Print month body */

static void printMonthBody(int year, int month) {

// Get start day of the week for the first date in the month

int startDay = getStartDay(year, month);

// Get number of days in the month

int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);

// Pad space before the first day of the month

int i = 0;

for (i = 0; i < startDay; i++)

System.out.printf("%4c", ' ');

for (i = 1; i <= numberOfDaysInMonth; i++) {

if (i < 10)

System.out.printf("%4d", i);

else

System.out.printf("%4d", i);

if ((i + startDay) % 7 == 0)

System.out.println();

}

System.out.println("\n");

}

/** Get the start day of the first day in a month */

static int getStartDay(int year, int month) {

// Get total number of days since 1/1/1800

int startDay1800 = 3;

int totalNumberOfDays = getTotalNumberOfDays(year, month);

// Return the start day

return (totalNumberOfDays + startDay1800) % 7;

}

/** Get the total number of days since January 1, 1800 */

static int getTotalNumberOfDays(int year, int month) {

int total = 0;

// Get the total days from 1800 to year - 1

for (int i = 1800; i < year; i++)

if (isLeapYear(i))

total = total + 366;

else

total = total + 365;

// Add days from January to the month prior to the calendar month

for (int i = 1; i < month; i++)

total = total + getNumberOfDaysInMonth(year, i);

return total;

}

/** Get the number of days in a month */

static int getNumberOfDaysInMonth(int year, int month) {

if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)

return 31;

if (month == 4 || month == 6 || month == 9 || month == 11)

return 30;

if (month == 2)

return isLeapYear(year) ? 29 : 28;

return 0; // If month is incorrect

}

/** Determine if it is a leap year */

static boolean isLeapYear(int year) {

return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);

}

}


Related Solutions

Write a program fragment (not a complete program) which will perform the following task: The int...
Write a program fragment (not a complete program) which will perform the following task: The int variable m currently contains the number of minutes a basketball player played in their last game. Use an IF statement(s) to print out an appropriate message based on the following: If m is from 35 to 48, print the message "very tired" If m is from 10 to 34, print the message "little tired" If m is from 1 to 9, print the message...
I am using Java and what do you mean samples? Creation of Program Application (Development Task...
I am using Java and what do you mean samples? Creation of Program Application (Development Task 2) This program should use a class file you create called: SavingsAccount. This class file should store the following information: Annual Interest Rate (this can be hardcoded) Starting Balance (use a constructor to accept this information) Create method to add all Deposits from Deposits.txt file. Create method to subtract all Withdrawals from Withdrawals.txt file. Create method to calculate the interest rate which is the...
Write a JAVA program that emulates a Magic Eight Ball. Start by generating a random number...
Write a JAVA program that emulates a Magic Eight Ball. Start by generating a random number and then use a switch statement to display the message. Use this website as a resource on possible answers a Magic Eight Ball gives: https://en.wikipedia.org/wiki/Magic_8-Ball
Write a program to perform the following actions: the language is Java – Open an output...
Write a program to perform the following actions: the language is Java – Open an output file named “Assign14Numbers.txt” – Using a do-while loop, prompt the user for and input a count of the number of random integers to produce, make sure the value is between 35 and 150, inclusive. – After obtaining a good count, use a for-loop to generate the random integers in the range 0 ... 99, inclusive, and output each to the file as it is...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple The class Apple DOES NOT HAVE a main method Some of the attributes of Apple are Type: A string that describes the apple.  It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith Weight: A decimal value representing...
Write a program in java that can perform encryption/decryption. In the following let the alphabet A...
Write a program in java that can perform encryption/decryption. In the following let the alphabet A be A={A, a, B, b, . . ., “ ”, “.”,“’ ”}. The encoding is A→0, a→1, B→2, b→4, . . ., Z→50, z→51, “ ”→52, “.”→53 and “’”→54.
This is a Java program Problem Description Write an application that inputs five digits from the...
This is a Java program Problem Description Write an application that inputs five digits from the user, assembles the individual digits into a five-digit integer number (the first digit is for one’s place, the second digit is for the ten’s place, etc.) using arithmetic calculations and prints the number. Assume that the user enters enough digits. Sample Output Enter five digits: 1 2 3 4 5 The number is 54321 Problem-Solving Tips The input digits are integer, so you will...
Write a Java program to simulate the rolling of two dice. The application should use an...
Write a Java program to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12. Your application should roll the dice 36,000,000 times. Store the results of each roll...
Suppose the interface and the class of stack already implemented, Write application program to ( java)...
Suppose the interface and the class of stack already implemented, Write application program to ( java) 1- insert 100 numbers to the stack                         2- Print the even numbers 3- Print the summation of the odd numbers
Write the program in java Write a program that does basic encrypting of text. You will...
Write the program in java Write a program that does basic encrypting of text. You will ask the user the filename of a text file which contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT