Question

JAVA programing Question 1 (Date Class):                                   &nbsp

JAVA programing

Question 1 (Date Class):                                                                                                     5 Points

Create a class Date with the day, month and year fields (attributes).

  1. Provide constructors that:
    1. A constructor that takes three input arguments to initialize the day, month and year fields.

Note 1: Make sure that the initialization values for the day, month and year fields are valid where the day must be between 1 and 31 inclusive, the month between 1 and 12 inclusive and the year a positive number.

Note 2: Make sure to handle the case of a leap year properly.

Note 3: If the values are outside their range, the constructor must throw an exception of type IllegalArgumentExcepetion as follows:

throw new IllegalArgumentExcepetion(“INVALID INITIALIZATION VALUES!”);

  1. A no-argument (parameterless) constructor that calls the two-argument constructor to initialize the day, month and year fields to their default values.

  1. Provide a getter (accessor) and a setter (mutator) methods to each field.

Note 4: Make sure that the initialization values for the day, month and year fields in the setter methods are valid. If the values are invalid, the setter method must throw an exception of type IllegalArgumentExcepetion as follows:

        throw new IllegalArgumentExcepetion(“INVALID INITIALIZATION VALUES!”);

  1. Override the toString() method to print the data using the MM/DD/YYYY format:

February 02, 2020

Note 5: I am assuming that the Rectangle object contains day, month and year fields are set to 2, 2 and 2020 values, respectively.

Note 6: Make sure to keep the same method signature when overriding:

public String toString()

  1. Override the equals() method to return true if the Date object and the referenced Date object have the same values for day, month and year.

Note 5: Make sure to keep the same method signature when overriding:

public boolean equals(Date ref)

  1. Write a test class called: Test_Date where:
    1. You create two references of type Date.
    2. Prompt the user to enter the values for the day, month and year fields to create two Date objects.
    3. Print the content of the two Date objects by calling their toString() methods.
    4. Compare the two Date objects by calling the equals() method in the first Date object.

Hint: A leap year is:

  1. Every year that is evenly divisible by 4
  2. Except if it is evenly divisible by 100...
  3. Unless it is also divisible by 400

Examples:

  1. 1997 is not a leap year
  2. 1996 is a leap year
  3. 1900 is not a leap year
  4. 2000 is a leap year

You should write a method to test whether a year is leap or not:

public Boolean isLeapYear(int year)

// Put your Date class here!!!

// Put your Test_Class class here!!!

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

CODE IN JAVA:

Date.java file:


public class Date {
   private int day ;
   private int month ;
   private int year ;
  
   public Date(int day, int month, int year) {
       validateDate(day, month, year);
   }
   public Date() {
       this.day = 1;
       this.month = 1;
       this.year = 2020 ;
   }
  
   public int getDay() {
       return day;
   }
   public void setDay(int day) {
       validateDate(day, this.month, this.year);
   }
   public int getMonth() {
       return month;
   }
   public void setMonth(int month) {
       validateDate(this.day, month, this.year);
   }
   public int getYear() {
       return year;
   }
   public void setYear(int year) {
       validateDate(this.day, this.month, year);
   }
  
   @Override
   public String toString() {
       return day + "-" + month + "-" + year ;
   }
  
  
   public boolean equals(Date ref) {
       if(this.day == ref.day && this.month == ref.month && this.year == ref.year)
           return true;
       return false;
   }
   private void validateDate(int day, int month, int year) {

       if(month < 1 || month > 12) {
           throw new IllegalArgumentException("INVALID INITIALIZATION VALUES!");
       }
       else {
           if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
               if(day >= 1 && day <= 31) {
                   this.day = day ;
                   this.month = month ;
                   this.year = year ;
               }
               else {
                   throw new IllegalArgumentException("INVALID INITIALIZATION VALUES!");
               }
           }
           else if(month == 4 || month == 6 || month == 9 || month == 11) {
               if(day >= 1 && day <= 30) {
                   this.day = day ;
                   this.month = month ;
                   this.year = year ;
               }
               else {
                   throw new IllegalArgumentException("INVALID INITIALIZATION VALUES!");
               }
           }
           else {
               if(isLeapYear(year)) {
                   if(day >= 1 && day <= 29) {
                       this.day = day ;
                       this.month = month ;
                       this.year = year ;
                   }
                   else {
                       throw new IllegalArgumentException("INVALID INITIALIZATION VALUES!");
                   }
               }
               else {
                   if(day >= 1 && day <= 28) {
                       this.day = day ;
                       this.month = month ;
                       this.year = year ;
                   }
                   else {
                       throw new IllegalArgumentException("INVALID INITIALIZATION VALUES!");
                   }
               }
           }
       }
      
  
   }
   private boolean isLeapYear(int year) {
       return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
   }
}

TestDate.java file:

import java.util.Scanner ;
public class TestDate {

   public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
       int day , month, year;
       System.out.print("Enter day of Date1 : ");
       day = sc.nextInt();
       System.out.print("Enter month of Date1 : ");
       month = sc.nextInt();
       System.out.print("Enter year of Date1 : ");
       year = sc.nextInt();
       Date date1 = new Date(day, month, year);
      
       System.out.print("Enter day of Date2 : ");
       day = sc.nextInt();
       System.out.print("Enter month of Date2 : ");
       month = sc.nextInt();
       System.out.print("Enter year of Date2 : ");
       year = sc.nextInt();
       Date date2 = new Date(day, month, year);
       System.out.println("Date1 : " + date1);
       System.out.println("Date2 : " + date2);
       if(date1.equals(date2))
           System.out.println("Both the dates are equal");
       else
           System.out.println("Both the dates are not equal");

   }

}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
JAVA programing Question 1 (Date Class):                                   &nbsp
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
  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

  • Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java,...

    Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java, which will contain code that utilizes your Date.java class. You will submit both files (each with appropriate commenting). A very similar project is described in the Programming Projects section at the end of chapter 8, which you may find helpful as reference. Use (your own) Java class file Date.java, and a companion DateClient.java file to perform the following:  Ask user to enter Today’s...

  • SaffSpring 2019 Professor Question 1: Answer ALL of the following questions based on this provide...

    I need sub-questions e, f, g, h. SaffSpring 2019 Professor Question 1: Answer ALL of the following questions based on this provided int daya int montha Date int dnt int y day-d year class Event ring locatson eventbat Event (5tring , Dated ocatlon- event Date-d pubiie String toStringt String detalla-Locatlons"LocatlonDatesevent Date.day return detalia eventDate-year publie statie weld mai arg Event 2ew EventCBayan" hew Date1.3,20191 Systen.out peintil The event n (11.201,as scheduled-ee . 내 1 eentDate.yeari ystem.st-printeinte yatem.ost-printcin(e2) a) What is...

  • QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members:...

    QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members: • A private String data field named name for the name of the ship. • A private String data field named yearBuilt for the year that the ship was built. • A constructor that creates a ship with the specified name and the specified year that the ship was built. • Appropriate getter and setter methods. A toString method that overrides the toString method...

  • Part I: (The Myate class) Design a class named MyDate. The class contains: • The data...

    Part I: (The Myate class) Design a class named MyDate. The class contains: • The data fields year, month, and day that represent a date. Month is 0-based, i.e., 0 is for January. • A no-arg constructor that creates a MyDate object for the current date. • A constructor that constructs a MyDate object with a specified elapsed time since midnight, January 1, 1970, in milliseconds. • A constructor hat constructs a MyDate object with the specified year, month, and...

  • Use "Eclipse IDE for Java EE Developers" Design a class named MyDate. The class contains: The...

    Use "Eclipse IDE for Java EE Developers" Design a class named MyDate. The class contains: The Date fields year, month, and day that represent a date. month is 0-based, i.e., 0 is for January. A no argument constructor that creates a MyDate object for the current date. A constructor that constructs a MyDate object with a specified elapsed time since midnight, January 1, 1970, in milliseconds. A constructor that constructs a MyDate object with the specified year, month, and day....

  • Create a class called Date212 to represent a date. It will store the year, month and...

    Create a class called Date212 to represent a date. It will store the year, month and day as integers (not as a String in the form yyyymmdd (such as 20161001 for October 1, 2016), so you will need three private instance variables. Two constructors should be provided, one that takes three integer parameters, and one that takes a String. The constructor with the String parameter and should validate the parameter. As you are reading the dates you should check that...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • Design and implement a C++ class called Date that has the following private member variables month...

    Design and implement a C++ class called Date that has the following private member variables month (int) day (nt) . year (int Add the following public member functions to the class. Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day is...

  • You are to create a class Appointment.java that will have the following: 5 Instance variables: private...

    You are to create a class Appointment.java that will have the following: 5 Instance variables: private String month private int day private int year private int hour private int minute 1 default constructor public Appointment() 1 non-default constructor that accepts arguments for all instance variables, your constructor must call the setter methods below to set the values of the instance variables public Appointment(String monthPassed, int dayPassed, int yearPassed, int hourPassed, int minutePassed) 5 setter methods (one for each instance variable)...

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