Question

Overview: In this lab you are to write a Java program to prompt the user for...

Overview: In this lab you are to write a Java program to prompt the user for an integer indicating a year and then print the calendar for that year.

Objective: This lab's objective is to exercise you with the use of Java’s control statements. You are required to use exactly one while statement, one for statement and one switch statement. You will also practice on how to use some basic input methods of the Scanner class and some formatting techniques of method printf().

Notes:

1. No arrays are allowed in this program.

2. To determine whether a year is a leap year or not:

a. If the year is a century year, the year must be divisible by 400.

b. If the year is not a century year, the year only needs to be divisible by 4.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

import java.util.Scanner;

public class Calendar {

public static void main(String[] args) {

Scanner scnr = new Scanner(System.in);

int year, start_day;

String month_name = "";

int days = 0;

System.out.printf("Enter year: ");

year = scnr.nextInt();

start_day = 0;

for (int month = 1; month <= 12; month++) {

switch (month) {

case 1:

month_name = "January";

days = 31;

break;

case 2:

month_name = "February";

days = 28;

break;

case 3:

month_name = "March";

days = 31;

break;

case 4:

month_name = "April";

days = 30;

break;

case 5:

month_name = "May";

days = 31;

break;

case 6:

month_name = "June";

days = 30;

break;

case 7:

month_name = "July";

days = 31;

break;

case 8:

month_name = "August";

days = 31;

break;

case 9:

month_name = "September";

days = 30;

break;

case 10:

month_name = "October";

days = 31;

break;

case 11:

month_name = "November";

days = 30;

break;

case 12:

month_name = "December";

days = 31;

break;

}

System.out.println("\t" + month_name + " " + year);

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

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

if (month == 2 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))

days += 1;

int j=0;

while(j<days+start_day){

if(j-start_day<0)

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

else

System.out.printf("%5d", j-start_day+1);

if ((j+1) % 7 == 0)

System.out.println();

j++;

}

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

start_day = (days + start_day) % 7;

}

}

}

Add a comment
Know the answer?
Add Answer to:
Overview: In this lab you are to write a Java program to prompt the user for...
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
  • 3. Using no date-specific Java libraries, write a program that computes the number of days between...

    3. Using no date-specific Java libraries, write a program that computes the number of days between any two dates (inclusive). You must take into account leap years. The rule for computing a leap year is as follows: If the year is divisible by 4 it is a Leap Year ... Unless the year is divisible by 100, then it is _not_ a Leap Year ... Unless the year is divisible by 400, then it _is_ a Leap Year. During a...

  • 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. 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:      ...

  • 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...

  • In Java You are to write a program that determines the day of the week for...

    In Java You are to write a program that determines the day of the week for New Year's Day in the year 3000. To do this, you must create your own date class (MyDate) and use the following interface and main program: /** Interface for Date objects to be used by the Year3000 driver program. @author Jon Sorenson */ public interface DateInterface { /** @return the day of the month (1-31) */ public int getDay(); /** @return the day of...

  • Create a java program which will prompt a user to input a product and its cost....

    Create a java program which will prompt a user to input a product and its cost. Using a while loop that will run until the sentinel value of “stop” (any case) is entered, the program will look for items with a cost greater than or equal to $100.00. The program will find the average cost of those items. Note: immediately after your statement containing input.nextDouble() you will need to add the following input.nextLine() statement. This will cause the input scanner...

  • A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes...

    A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year to be a leap year are: 1) The year must be divisible by 4 2) If the year is a century year...

  • We use bluej for our JAVA class. If you can help me id greatly appreciate it....

    We use bluej for our JAVA class. If you can help me id greatly appreciate it. Create a new Java class called AverageWeight. Create two arrays: an array to hold 3 different names and an array to hold their weights. Use Scanner to prompt for their name and weight and store in correct array. Compute and print the average of the weights entered using printf command. Use a loop to traverse and print the elements of each array or use...

  • We are using blueJ for my java class, if you can help me id greatly appreciate...

    We are using blueJ for my java class, if you can help me id greatly appreciate it. Create a new Java class called AverageWeight. Create two arrays: an array to hold 3 different names and an array to hold their weights. Use Scanner to prompt for their name and weight and store in correct array. Compute and print the average of the weights entered using printf command. Use a loop to traverse and print the elements of each array or...

  • In Java please: Write a program that will prompt the user to enter GPA values one...

    In Java please: Write a program that will prompt the user to enter GPA values one per line, stopping when the user enters a negative Input: value. Print the following on separate lines: . The number of vaild GPAs entered » The sum of the GPAs 4.0 3.7 2.9 3.5 -1 The average GPA

  • Please use simple java code and comments no arrays Write a program that displays all the...

    Please use simple java code and comments no arrays Write a program that displays all the numbers from 100 to 1,000. ten per line, that are divisible by 5 and 6. Numbers are separated by exactly one space.

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
ADVERTISEMENT