Question

In JAVA Write a program that takes an integer between 1 and 12 inclusive, and a...

In JAVA

Write a program that takes an integer between 1 and 12 inclusive, and a String of three letters (representing the day of the week) from the user, and prints a calendar for the month of the year represented by the integer. For example, if the user enters 1 and "Mon", the program should print the calendar for January, starting the first day of the month on Monday.

Enter the month (1 - 12): 1
Which day of the week is the first day of the month (Mon, Tue, etc.): Mon
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

.

Specific requirements include the following.

  • Create an array to store number of days for each month for current year
  • Create an array to store the seven days (Monday, Tuesday, ect.)
  • Ask the user which day of the week the first day of the month is.
  • Print the calendar as a table starting with Sunday
  • You should check for user errors when taking the input and ask the user to enter valid input
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
        int i,month,x=0,ct;
        int[] months=new int[31];
        String day,str;
        String[] days = new String[7];
             days[0] = "sun";
             days[1] = "mon";
             days[2] = "tue";
             days[3] = "wed";
             days[4] = "thu";
             days[5] = "fri";
             days[6] = "sat";
           
        Scanner in = new Scanner(System.in);
       System.out.println("Enter Month , Day:");
       month= in.nextInt();
       day= in.nextLine();
      
        for(i=0;i<7;i++){
            str=days[i];
           if(str == day)
                x=i;
        }
      
      
      
        for(i=0;i<7;i++)
            System.out.print(days[i]+"\t");
        System.out.println("");
      
        for(i=0;i<x;i++)
           System.out.print("\t");
          
          
         ct=x;
        if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
            x=31;
        else if(month==2)
            x=28;
        else
            x=30;
     
     
        for(i=0;i<x;i++){
            System.out.print(" "+(i+1)+"\t");
            ct++;
            if(ct%7==0){
                System.out.println("");
                continue;
            }
        }
          
          
   }
}

output:-

Add a comment
Know the answer?
Add Answer to:
In JAVA Write a program that takes an integer between 1 and 12 inclusive, and a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT