Question

In: Computer Science

Write a java program MyCalendar that outputs a monthly calendar for a given month and year....

Write a java program MyCalendar that outputs a monthly calendar for a given month and year.
the output should be looks like this one at the bottom:( I don't know why it doesn't show right but the first day of the month is not Sunday it starts from Wednesday)

F. Last’s My Calendar
Enter month year? 10 2019
10/2019
Sun Mon Tue Wed Thu Fri Sat
---------------------------------
1 2 3 4
5 6 7 8 9 10  11
12 13 14 15 16 17 18
19 20 21   22 23 24 25
26 27 28 29 30 31

Solutions

Expert Solution

I think your sample output in question have problem that first day of month should be Tuesday not Wednesday. Below is correct code if you want any modification or have any problems let me know.

MyCalendar.java file:

package calendar;

import java.util.Calendar;
import java.util.Scanner;

public class MyCalendar {
  
   // main method to run program
   public static void main(String args[]) {
       // create a Scanner object to read input from user
       Scanner s = new Scanner(System.in);
       // prompt user for input date and year
       System.out.print("Enter month year? ");
       String input = s.nextLine(); // read input from user
       s.close(); // close the scanner as done reading input
       String[] data = input.split(" "); // Separate month from year
       int month = Integer.parseInt(data[0]);
       int year = Integer.parseInt(data[1]);
       // create a calendar object
       Calendar c = Calendar.getInstance();
       // set given date to calendar
       c.set(year,month-1,1); // remove 1 from month as months are 0 indexed
       // get the 1st day of the month
       int day = c.get(Calendar.DAY_OF_WEEK);
       // get maximum days of month
       int max_days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
      
       // print calendar
       System.out.println(month + "/" + year); // print month
       // Format string for output days name
       String days_name = String.format("%s %s %s %s %s %s %s", " Sun","Mon","Tue","Wed","Thu","Fri","Sat");
       // print name of days
       System.out.println(days_name);
       System.out.println("----------------------------");
       //print days
       for(int i=1;i<max_days+day+1;i++) {
           int date = i - day;
           // get formated output for days
           String days = "";
           if(date > 0) {
               days = String.format("%4s", date);
           }
           else {
               days = String.format("%4s", " ");
           }
           System.out.print(days);
           //print new line at end of week
           if(i%7==0) {
               System.out.println();
           }
       }  
   }

}



Related Solutions

write a program on c++ that outputs a calendar for a given month in a given...
write a program on c++ that outputs a calendar for a given month in a given year, given the day of the week on which the 1st of the month was. The information in numeric format (months are: 1=Januay, 2=February 3=March, 4= April, 5= May, 6= June, 7= July... etc days are 1=Sunday, 2=Monday, 3= Tuesday, 4= Wednesday, 5= Thursday, 6= Friday and 7= Saturday ). The program output that month’s calendar, followed by a sentence indicating on which day...
Write a C# program that prints a calendar for a given year. Call this program calendar....
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 Java program for RSA encryption that has the following inputs and outputs: Given a...
Write a Java program for RSA encryption that has the following inputs and outputs: Given a message and an integer n = pq where p and q are odd primes and an integer e > 1 relatively prime to (p − 1)(q − 1), encrypt the message using the RSA cryptosystem with key (n, e).
Write a program in Java that reads in a set of positive integers and outputs how...
Write a program in Java that reads in a set of positive integers and outputs how many times a particular number appears in the list. You may assume that the data set has at most 100 numbers and -999 marks the end of the input data. The numbers must be output in increasing order. For example, for the data 15 40 28 62 95 15 28 13 62 65 48 95 65 62 65 95 95 -999 The output is...
Write a C++ program that prints a calendar for a given year. ONLY USING "#include<iostream>" and...
Write a C++ program that prints a calendar for a given year. ONLY USING "#include<iostream>" and "#include<cmath>" 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 Java program that prompts a user for 10 integers. When completed it outputs the...
Write a Java program that prompts a user for 10 integers. When completed it outputs the highest number, the lowest number, the number of odd number, and the average of the numbers
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
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...
PYTHON PROGRAM: Write a program that determines the day of the week for any given calendar...
PYTHON PROGRAM: Write a program that determines the day of the week for any given calendar date after January 1, 1900, which was a Monday. This program will need to account for leap years, which occur in every year that is divisible by 4, except for years that are divisible by 100 but are not divisible by 400. For example, 1900 was not a leap year, but 2000 was a leap year.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT