Question

In the following program, declare an integer array daysInMonth that stores the number of days in January, February, March, April, and so on

# JAVA 

In the following program, declare an integer array daysInMonth that stores the number of days in January, February, March, April, and so on. Fill it with the appropriate values (31, 28, 31, 30, ...).

The program reads a month and year from the user.

When the year is a leap year, change the entry for February to 29.

Print out how many days the given month has.

CODE:

import java.util.Scanner;

public class NumberOfDays
{
   public static void main(String[] args)
   {
      // Declare and initialize daysOfMonth
      . . . daysOfMonth = . . .

      Scanner in = new Scanner(System.in);
      System.out.print("Month (1 - 12): ");
      int month = in.nextInt();
      System.out.print("Year: ");
      int year = in.nextInt();
    
      if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
         // It's a leap year. Adjust the entry for February
         . . .
      }

      // Get the number of days in the given month
      int days = daysOfMonth[. . .];

      System.out.println("Number of days: " + days);             
   }
}

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

Program:

package sample;

import java.util.Scanner;

public class NumberOfDays

{

public static void main(String[] args)

{

// Declare and initialize daysOfMonth

int daysOfMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};

Scanner in = new Scanner(System.in);

System.out.print("Month (1 - 12): ");

int month = in.nextInt();

System.out.print("Year: ");

int year = in.nextInt();

  

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

// It's a leap year. Adjust the entry for February

daysOfMonth[1]=29;//Here array index starts from 0 thats why seting 1st index to 29

}

// Get the number of days in the given month

int days = daysOfMonth[month-1];//same hoolds here

System.out.println("Number of days: " + days);

}

}

Output:

Add a comment
Know the answer?
Add Answer to:
In the following program, declare an integer array daysInMonth that stores the number of days in January, February, March, April, and so on
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
  • Extend this date validation program, so that it checks for valid leap years (in which the...

    Extend this date validation program, so that it checks for valid leap years (in which the month February has 29, instead of 28, days). A leap year is any year that is divisible by 4 but not divisible by 100, unless it is also divisible by 400. Do not allow February to have 29 days on years that are not leap years. **Please view both photos for the entire code and post new code with the leap year modification. Thank...

  • Write a while loop that prints all positive numbers that are divisible by 10 and less...

    Write a while loop that prints all positive numbers that are divisible by 10 and less than a given number n. For example, if n is 100, print 10 20 30 40 50 60 70 80 90. import java.util.Scanner; public class PositiveLess {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        System.out.print("n: ");        int n = in.nextInt();        ...        while (...)        {           ...

  • Please put both of this loops into one program. Set it up so that there is...

    Please put both of this loops into one program. Set it up so that there is one main function and both loops are within it. Thank You. import java.util.Scanner; public class WhileDavidMungomaLab12 { public static void main(String[] args) { int aScore = -1; while (aScore < 0 || aScore > 100) { Scanner sc = new Scanner(System.in); System.out.println("Please enter a valid number that is within the specified range(0 and 100): "); aScore = sc.nextInt(); } } } AND import java.util.Scanner;...

  • I need to output Number of rooms : , Occupied rooms:, Vacant rooms:, and Occupancy rate:,...

    I need to output Number of rooms : , Occupied rooms:, Vacant rooms:, and Occupancy rate:, I've written the java program but I can not get the output I need. What am I doing wrong here? This is a java program and I'm not to use anything advanced, just beginner java programming. Any help would be appreciated import java.util.Scanner; // Needed for the Scanner class /** * This program will read in the number of floors the resort * contains...

  • Create a Java application named Problem14 using the two files Dr. Hanna provided to you—Date.java andProblem14.java—then change his code to add two non-trivial public, non-static methods to the Date class.

    Create a Java application named Problem14 using the two files Dr. Hanna provided to you—Date.java andProblem14.java—then change his code to add two non-trivial public, non-static methods to the Date class.1. Increment a Date to the next day (for example, 1-31-2011 increments to 2-1-2011; 12-31-2010 increments to 1-1-2011).2. Decrement a Date to the previous day (for example, 2-1-1953 decrements to 1-31-1953; 1-1-1954 decrements to 12-31-1953).. Extra Credit (up to 5 points) Re-write the Date member functions Input() and Output() to usedialog boxes to...

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • Problem #1 : write a Month class that contains methods to determine the number of days...

    Problem #1 : write a Month class that contains methods to determine the number of days in a month given the numeric equivalent of the month and year; your program must take into account leap years. To help facilitate a result, the following are sample runs and a driver program import java util.Scanner This is a test driver class for the Nonth class pablic class namDays ic statie void main (3teingt1 args Scanner in new Scanner (System.in) System.out print ("Please...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the...

    Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the following data entry error conditions: A number less than 1 or greater than 12 has been entered for the month A negative integer has been entered for the year utilizes a try and catch clause to display an appropriate message when either of these data entry errors exceptions occur. Thank you let me know if you need more info import java.util.*; //Class definition public...

  • Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util...

    Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util.*; import java.lang.*; /** * * @author STEP */ public class ShiftCipher { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); String plainText; System.out.print("Please enter your string: "); // get message plainText = input.nextLine(); System.out.print("Please enter your shift cipher key: "); // get s int s = input.nextInt(); int...

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