Question

Write a class called Date that represents a date consisting of a year, month, and day. A Date object should have the followinUse Java and creat proper classes to finish this problem

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

class Date{

private int day;

private int month;

private int year;

//constructor

public Date(){

day=1;

month=1;

year=1500;

}

// parameterize constructor

public Date(int Year,int Month,int Day){

day=Day;

month=Month;

year=Year;

}

// getNumber of days in month

public int NumberOfDaysInMonth(int m){

  switch (m)

  {

  case 1:

  {

       return 31; }

  case 2:

  {

       return 28; }

  case 3:

  {

       return 31; }

  case 4:

  {

       return 30;

  }

  case 5:

  {

       return 31; }

  case 6:

  {

       return 30; }

  case 7:

  {

       return 31; }

  case 8:

  {

       return 31; }

  case 9:

  {

       return 30; }

  case 10:

  {

       return 31; }

  case 11:

  {

       return 30; }

  case 12:

  {

       return 31; }

  default:

    break;

  }

return 0;

}

// setting days

void setDay(int d){

  if (d>NumberOfDaysInMonth(getMonth()))

  {

    while (d>NumberOfDaysInMonth(getMonth()))

    {

      d = d - NumberOfDaysInMonth(getMonth());

      setMonth(getMonth() + 1);

    }

    day = d;

  }

  else if (d >= 1 && d <= NumberOfDaysInMonth(getMonth()))

  {

    day = d;

  }

  else

  {

    day = 1;

  }

}

public void setMonth(int m){

  if (m > 12)

  {

    while (m > 12)

    {

      m = m - 12;

      setYear(getYear() + 1);

    }

    month = m;

  }

  else if (m >= 1 && m <= 12)

  {

    month = m;

  }

  else

  {

    month = 1;

  }

}

public void setYear(int y){

  if (y>1500)

  {

    year = y;

  }

  else

  {

    year = 1500;

  }

}

public int getYear()

{

return year;

}

public int getMonth()

{

return month;

}

public int getDayOfMonth()

{

return day;

}

public void addDays(int numberOfDays)

{

setDay(day+numberOfDays);

}

public void addWeeks(int weeks)

{

day=day+7*weeks;

if(day>31)

{

int daydiff=day-31;

month++;

day=daydiff;

}

}

public int daysTo(Date other)

{

return other.day - this.day;

}

public String toString()

{

return ""+year+"/"+month+"/"+day;

}

public boolean isLeapYear() {

return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);

}

}

class Main {

public static void main(String[] args) {

Date d= new Date(1956,5,1);

System.out.println("Current Day: "+d.getDayOfMonth());

System.out.println("Current Month: "+d.getMonth());

System.out.println("Current Year: "+d.getYear());

System.out.println("Current Year is leap year ? "+d.isLeapYear());

int num=40;

System.out.println("Adding "+num+" days in days");

d.addDays(40);

System.out.println(d.toString());

d.addWeeks(3);

}

}

OUTPUT:

OPENJDK Runtime Environment (build 10.C u0.18.04.4) javac -d . Main.java : java Main Current Day: 1 Current Month: 5 Current

IF HAVE IMPLEMENTED SOME EXTRA FUNCTIONS TO MAKE IT EASIER I HOPE IT HELPS YOU

IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW
PLEASE GIVE A THUMBS UP

Add a comment
Know the answer?
Add Answer to:
Use Java and creat proper classes to finish this problem Write a class called Date that...
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
  • Write a class called Date that represents a date consisting of a year, month, and day....

    Write a class called Date that represents a date consisting of a year, month, and day. A Date object should have the following methods: public Date(int year, int month, int day) Constructs a new Date object to represent the given date. public void addDays(int days) Moves this Date object forward in time by the given number of days. public void addWeeks(int weeks) Moves this Date object forward in time by the given number of seven-day weeks. public int daysTo( Date...

  • C++ problem 11-6 In Programming Exercise 2, the class dateType was designed and implemented to keep...

    C++ problem 11-6 In Programming Exercise 2, the class dateType was designed and implemented to keep track of a date, but it has very limited operations. Redefine the class dateType so that it can perform the following operations on a date, in addition to the operations already defined: Set the month. Set the day. Set the year. Return the month. Return the day. Return the year. Test whether the year is a leap year. Return the number of days in...

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

  • The class dateType is designed to implement the date in a program, but the member function...

    The class dateType is designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the data members. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and the year are checked before storing the date into the data members. Add a function member, isLeapYear, to check whether a year is a leap...

  • #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;...

    #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;        int dday;        int dyear;       public:       void setdate (int month, int day, int year);    int getday()const;    int getmonth()const;    int getyear()const;    int printdate()const;    bool isleapyear(int year);    dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) {    int numofdays;    if (year<=2008)    {    dyear=year;...

  • I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the fo...

    I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the form (month day year): 2 28 2018  Ask user to enter Birthday in the form (month day year): 11 30 1990  Output the following: The date they were born in the form (year/month/day). The day of the week they were born (Sunday – Saturday). The number of days between their birthday and today’s...

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

  • 12.2 LAB 12.2: Classes (Date) C++ Rewrite the class definition of Date class (From Lab 12.1)...

    12.2 LAB 12.2: Classes (Date) C++ Rewrite the class definition of Date class (From Lab 12.1) including 4 added member functions: bool isLeapYear(): this function will return true if the year of the date is a leap year. int daysPassed(): This function will return how many days has been passed in this year. int daysLeft(): This function will return how many days has left of this year. int diff(Date AnotherDate): This function will return the difference between the calling object...

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

  • Examine the following class definition: public class Date private int year; private int month; private int...

    Examine the following class definition: public class Date private int year; private int month; private int day; public Date() { ...) public void set (int x, int y, int z) { ...) public int getYear() { ...) // returns year public int getMonth() { } // returns month public int get Day () { ...) // returns day //more methods here -- 1 Which of the following statements in a client program correctly prints out the day of the object...

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