Question

In: Computer Science

Coding in Java private int getLastDayOfMonth(int m, int y)– return the last day of the month...

Coding in Java

  • private int getLastDayOfMonth(int m, int y)– return the last day of the month and year entered as input parameter. Remember to invoke the isLeapYear method when appropriate. If the month entered as input parameter is 2 (February) and the year passed as input parameter is a leap year this method should return 29.
  • private boolean isDateValid (int m, int d, int y) – return true if day, month, and year entered as input parameters form a valid date, return false otherwise. To be able to write the logic for this method you need to know the number of days for each month of the year, whether a year is a leap year (leap years have 29 days for the month of February, not leap years have 28 days for the month of February). Remember to invoke the isLeapYear and getLastDayOfMonth methods when appropriate. Valid months are between 1 and 12 (inclusive) and consider a positive number as a valid year.

Examples:

  • Invalid dates:
    • 2/29/2019 – 2019 is not a leap year, the number of days in February is 28
    • 13/17/2019 – 13 is not a valid month
    • 11/31/2019 – the month of November only has 30 days
    • 6/23/-2019 – the year cannot be negative
  • Valid dates:
    • 2/29/2020 – 2020 is a leap year, February has 29 days
    • 11/30/2019 – nothing wrong, November has 30 days
    • 6/22/1992 – nothing wrong here.

Solutions

Expert Solution

I have implemented these method under a single class Test , so in order to use them(for getting output) i have made them static and used them under our main method.

Since there is no information about input, I have asked user input to enter day, month ,year separately and then perform the tasks.(Please see output screenshots to get clarity).

Please go through the code, test run it for various inputs, Open for suggestions, Thanks!

import java.util.Scanner; //to prompt for user input

public class Test {

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);// to prompt user input
       System.out.print("Enter month: ");
       int month = sc.nextInt();// stores month
       System.out.print("Enter day: ");
       int day = sc.nextInt();// stores day
       System.out.print("Enter year:");
       int year = sc.nextInt();// stores year
       /*
       * method call isDateValid(m,d,y) to check if the date entered is valid or not
       */
       if (isDateValid(month, day, year)) {
           System.out.println(month + "/" + day + "/" + year + " Valid Date");// prints if the date is valid
       } else {
           System.out.println(month + "/" + day + "/" + year + " Invalid Date");// prints if the date is invalid
       }
       sc.close();
   }
   /*month, day, year are passed as argument to below function to test whether it forms a valid date or not*/
   private static boolean isDateValid(int month, int day, int year) {
       boolean isDateValid = false;//boolean to validate the date
       int lastDayOfMonth = getLastDayOfMonth(month, year);/*stores the last day of the month(passed as arg)*/
       /*checks if month, day, year are positive values*/
       if (month > 0 && day > 0 && year > 0) {
           /*if month entered is invalid getLastDayOfMonth returns 0(look for method implementation)*/
           if (lastDayOfMonth == 0) {
               /*returns false here since isValidDate was initialized as false*/
               return isDateValid;
           }
           /*if its a valid month then check for entered day as valid or not should be between 1 and lastDayOfMonth*/
           if (day >= 1 && day <= lastDayOfMonth) {
               isDateValid = true;
               return isDateValid;//returns true here since date is found valid
           }
       }
       return isDateValid;//returns false here for negative values of either of day, month, year
   }
   /*returns the last day of month and year passed as argument*/
   private static int getLastDayOfMonth(int month, int year) {
       /*if month is valid i.e between 1 and 12*/
       if ((month >= 1 && month <= 12)) {
           /*For months January,March,May,July,August,October,December last day is 31st*/
           if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
               return 31;
           }
           /*For months September,April,June,November last day is 30th*/
           else if (month == 9 || month == 4 || month == 6 || month == 11) {
               return 30;
           }
           /*For month February we need to check if its a leap year or not*/
           else {
               /*checks for year being a leap year or not and return 29 or 28 accordingly*/
               if (isLeapYear(year)) {
                   return 29;
               }
               return 28;
           }
       }
       /*returns 0 for invalid month entered*/
       return 0;
   }
   /*returns true if its a leap year and false if not*/
   private static boolean isLeapYear(int year) {
       /*a leap year should be either be divisible by 400 or divisible by 4 and not divisible by 100*/
       if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
           return true;//returns true for a leap year
       }
       return false;//returns false if not leap year
   }

}

screenshot of the code:

output when you run above code(multiple runs):

to format your code Ctrl+a then Ctrl+Shift+f(for eclipse IDE)


Related Solutions

Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year:...
Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year: int) +setDate(month: int, day: int, year: int): void -setDay(day: int): void -setMonth(month: int): void -setYear(year: int): void +getMonth():int +getDay():int +getYear():int +isLeapYear(): boolean +determineSeason(): string +printDate():void Create the class Constructor with no arguments sets the date to be January 1, 1900 Constructor with arguments CALLS THE SET FUNCTIONS to set the Month, then set the Year, and then set the Day - IN THAT ORDER...
package applications; public class Matrix { private int[][] m; public Matrix(int x, int y) { m...
package applications; public class Matrix { private int[][] m; public Matrix(int x, int y) { m = new int[x][y]; } public Matrix(int x, int y, int z) { m = new int[x][y]; for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { m[i][j] = z; } } } public int rowsum(int i) throws IndexOutOfBoundsException { if (i < 0 || i > m.length-1) { throw new IndexOutOfBoundsException("Invalid Row"); } int sum =...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to...
   private static void merge(int arr[], int l, int m, int r) {        //...
   private static void merge(int arr[], int l, int m, int r) {        // Find sizes of two subarrays to be merged        int n1 = m - l + 1;        int n2 = r - m;        /* Create temp arrays */        int L[] = new int[n1];        int R[] = new int[n2];        /* Copy data to temp arrays */        for (int i = 0; i...
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the...
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the follwing is true: public static void main(String[] args) { A System.out.println(test ( 7, 14, 23)) ; B System.out.println(test ( test ( 7,9, 14) , 23 ); C System.out.println( test ( 14, 23 ) ; D System.out.println(test(1,2,4), 14, 23)) ;
static int product(int x,int y){ if(x==0||y==0){//checking if x or y is 0 return 0;//if x or...
static int product(int x,int y){ if(x==0||y==0){//checking if x or y is 0 return 0;//if x or y is 0, then the return value and x*y will be zero. }else if(y<0&&x<0){ x=-x;//Changing the sign of x y=-y;//Changing the sign of y }else if(x>=1){ return (y+product(x-1,y)); } return (x+product(x,y-1)); } find the space complexity and the time complexity of the above algorithm.
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int type, private and the following methods: one default constructor which will create a fraction of 1/1. one constructor that takes two parameters which will set the values of numerator and denominator to the specified parameters. int getNum() : retrieves the value of numerator int getDenom(): retrieves the value of the denominator Fraction add(Fraction frac): adds with another Fraction number and returns the result in...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** * Create a point with coordinates <code>(0, 0)</code>. */ public Point2() { complete JAVA code this.set(0.0, 0.0); COMPLETE CODE }    /** * Create a point with coordinates <code>(newX, newY)</code>. * * @param newX the x-coordinate of the point * @param newY the y-coordinate of the point */ public Point2(double newX, double newY) { complete Java code this.set(newX, newY); }    /** * Create a...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT