Question

Write a class named MonthDays. The class’s constructor should accept two arguments: l An integer for...

Write a class named MonthDays. The class’s constructor should accept two arguments:
l An integer for the month (1 = January, 2 February, etc).
l An integer for the year
The class should have a method named getNumberOfDays that returns the number of days
in the specified month. The method should use the following criteria to identify leap years:
1. Determine whether the year is divisible by 100. If it is, then it is a leap year if and
if only it is divisible by 400. For example, 2000 is a leap year but 2100 is not.
2. If the year is not divisible by 100, then it is a leap year if and if only it is divisible
by 4. For example, 2008 is a leap year but 2009 is not.
Demonstrate the class in a program that asks the user to enter the month (letting the user
enter an integer in the range of 1 through 12) and the year. The program should then display
the number of days in that month. Here is a sample run of the program:
Enter a month (1-12): 2 [Enter]
Enter a year: 2008 [Enter]
29 days


+++++Java code

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

Provided Java Code done as per your requirements.

  • "Screen shot program" code for better understanding.
  • "Code to copy" for the program execution.

Screen shot program:

Output 1:

Output 2:

Code to copy:

//Header file section
import java.util.Scanner;

//main class
public class MonthDays {
// Declare variables
int month;
int year;
int numDays;

// constructor accept two arguments
public MonthDays(int monthNum, int yearNum) {
  month = monthNum;
  year = yearNum;
}

// method named getNumberOfDays that returns the
// number of days
public int getNumberOfDays(int monthNums) {
  if (monthNums == 2) {
   if ((year % 100 == 0) && (year % 400 == 0) || (year % 4 == 0)
     && !(year % 100 == 0))
    numDays = 29;
   else
    numDays = 28;
  } else if (monthNums == 1 || monthNums == 3 || monthNums == 5
    || monthNums == 7 || monthNums == 8 || monthNums == 10
    || monthNums == 12)
   numDays = 31;
  else
   numDays = 30;
  return numDays;
}

// main method
public static void main(String[] args) {
  // Declare variables
  int month, year, days;

  // Create a Scanner class's object
  Scanner input = new Scanner(System.in);

  // Prompt and read input from the user
  System.out.print("Enter a month (1-12):");
  month = input.nextInt();
  System.out.print("Enter a year:");
  year = input.nextInt();
  // Call the MonthDays class's constructor object
  MonthDays md = new MonthDays(month, year);
  // Call the function
  days = md.getNumberOfDays(month);
  // Display output
  System.out.println(days + " days");

}
}

Add a comment
Know the answer?
Add Answer to:
Write a class named MonthDays. The class’s constructor should accept two arguments: l An integer 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
  • C++ code: Days in current month: Write a program that can determine the number of days...

    C++ code: Days in current month: Write a program that can determine the number of days in a month for a specified month and year. The program should allow a user to enter two integers responding a month and a year, and it should determine how many days are in the specified month. The integers 1 through 12 will be used to identify the months of January through December. The user indicates the end of days in the current month...

  • Has to be written in C++ Write a program that asks the user to enter the...

    Has to be written in C++ Write a program that asks the user to enter the month (1-12) and the year (0-2019). Validate the input and print an error and stop the program if an invalid month or year was entered. The program should then display the number of days in that month. Use the following criteria to identify leap years: Determine whether the year is divisible by 100. If it is, then it is a leap year if and...

  • Write a class named DartSector. The constructor should accept a sector number and position {Single, Double,...

    Write a class named DartSector. The constructor should accept a sector number and position {Single, Double, Treble, Outer & Inner} represented with the integers 1 – 5 respectively. The class should have 3 methods: • getSectorColor – method should return the pocket’s color (as a String) • singleThrow – method should generate 2 random numbers; one in the range (1..20) and another (1..5); and return the score for the throw. • throwThree – method should generate 3 sets of 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...

  • C++ 1. Start with a UML diagram of the class definition for the following problem defining a utility class named Month. 2. Write a class named Month. The class should have an integer field named month...

    C++ 1. Start with a UML diagram of the class definition for the following problem defining a utility class named Month. 2. Write a class named Month. The class should have an integer field named monthNumber that holds the number of the month (January is 1, February is 2, etc.). Also provide the following functions: • A default constructor that sets the monthNumber field to 1. • A constructor that accepts the number of month as an argument and sets...

  • Design an application with a single class and two static methods: method main and method isLeap....

    Design an application with a single class and two static methods: method main and method isLeap. Static method isLeap accepts year as integer and returns boolean value true or false depending whether year is leap or not. public static boolean isLeap ( int year ) The year is leap year if it is divisible by 4, but not divisible by 100 , except if it is divisible by 400. Examples 2003 is not a leap year 2020 is leap year...

  • Complete task using python code From definitions file: month = ‘February’ year = 1200 Problem 2:...

    Complete task using python code From definitions file: month = ‘February’ year = 1200 Problem 2: Leap Years (2 Points) Your task is to write a program that takes in both a month and year (defined in the definitions file) and outputs the number of days for that month. It should take into account whether the year is a leap year or not. The resulting number of days in a given month/year combo is to be assigned to the variable...

  • Write code in matlab Compute leap years. Write a function (leapyears) that takes two integer arguments...

    Write code in matlab Compute leap years. Write a function (leapyears) that takes two integer arguments (start year and end year) and prints a list of the leap years that occur between them (inclusive of the start and end year). Leap years are divisible by 4. Except that years divisible by 100 are not leap years unless they are also divisible by 400.

  • Design a function named max that accepts two integer values as arguments and returns the value...

    Design a function named max that accepts two integer values as arguments and returns the value that is the greater of the two. For example, if 7 and 12 are passed as arguments to the function, the function should return 12. Use the function in a program that prompts the user to enter two integer values. The program should display the value that is the greater of the two. Need Variable lists, Psuedocode, ipo chart, Flow Chart, and a python...

  • Assuming that a year has 365 days, write a class named DayOfYear that takes an integer...

    Assuming that a year has 365 days, write a class named DayOfYear that takes an integer representing a day of the year and translates it to a string consisting of the month followed by day of the month. For example, Day 2 would be January 2. Day 32 would be February 1. Day 365 would be December 31. The constructor for the class should take as parameter an integer representing the day of the year, and the class should have...

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