Question

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 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 date (their age in days).
Example: You were born on 1990/11/30, which was a Friday.
You are 9952 days old.
To do this you will need to design, implement and test a Java class file, Date.java:
Date.java is a class of objects which you write, then utilize in DateClient.java to solve the problem above. The
Date.java class needs to have the following methods (the object calling the methods below is referenced by
this Date object in all the descriptions, as needed).
 public Date(int year, int month, int day)
//constructor to create new Date object with given year, month and day.
 public Date()
//constructor to create new default Date object with date January 1, 1753 (1753 1 1).
(As a side note, January 1, 1753 was a Monday, which will be used later).
 public int getYear()
//accessor method to return this Date object’s year (between 1753 & 9999).
 public int getMonth()
//accessor method to return this Date object’s month (between 1 & 12).
 public int getDay()
//accessor method to return this Date object’s day (between 1 & 31).
 public String toString()
//accessor method to return String of this Date object’s field in form Year/Month/Day (2018/2/28).
 public boolean equals(Date otherDate)
//accessor method to return true if this Date object equals otherDate object (false otherwise).
 public Boolean isLeapYear()
//accessor method to return true if this Date object’s year field is leap year (false otherwise).
Leap years are all years divisible by 4, except those also divisible by 100, but includes those that are
divisible by 400. [1756, 2004, 1600 & 2000 are leap years, but 1755, 1900 & 2100 are not].
 pubic void nextDay()
//mutator method that advances this Date object’s fields to represent the next date. If this Date
object’s fields are 2018/2/28, then calling nextDay() method will advance it to 2018/3/1. Similarly, if
starting with 2018/12/31, calling nextDay() method will advance it to 2019/1/1. In other words,
nextDay() advances this Date object’s day, month and/or year, as needed, to the next day.
 public int advanceTo(Date endDate)
//mutator method that advances this Date object’s fields to the endDate object’s fields and returns
the number of days between the two dates. Like nextDay(), advanceTo(Date endDate) can advance
days, months and/or years as needed. It may be assumed that the endDate object’s date is after this
Date object’s date.
 public String getDayOfWeek()
//accessor method that returns a string giving the day of the week this Date object’s date falls on
(Sunday thru Saturday). (We will need to know the day of a start date, for which we can use that
January 1, 1753 was a Monday.) This method should be written utilizing the other methods listed
above—no outside code from internet or elsewhere is needed.
None of the methods within the Date.java class should interact directly with the user or print
anything to the console. All interaction with the user (and the console) should be contained within
DateClient.java class.
You may not use any of Java’s date-related classes, nor should you use any code or fragments of
code obtained through outside or internet research. The concepts needed here rely on content
from chapters 1-8 in our text, and development of these ideas independent of outside resources is
the purpose of this assignment.
Development strategy:
o Write constructor and get*() methods in Date.java class.
o Write a DateTesting.java class to test all methods utilizing Date.java class.
o Write toString, equals & isLeapYear accessor methods in Date.java class.
o Write nextDay, (you may want to write an additional intermediate method that returns the
number of days in this Date object’s month. You will also need to call isLeapYear within this
method.)
o Write advanceTo by utilizing other methods written within Date.java class.
o Write getDaysOfWeek, again utilizing other methods written within the Date.java class.
Grading Rubric: See Canvas Rubric for score break-down, but good programming styles (commenting,
reducing redundancy) is expected. In addition, points will be awarded for each portion of Date.java
and DateClient.java classes that is developed.

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

PROGRAM :

Date.java

public class Date {
   private int year;
   private int month;
   private int day;

   // constructor to create new Date object with given year, month and day.
   public Date(int year, int month, int day) {
       this.year = year;
       this.month = month;
       this.day = day;
   }

   // constructor to create new default Date object with date January 1, 1753 (1753
   // 1 1).
   public Date() {
       this.year = 1753;
       this.month = 1;
       this.day = 1;
   }

   // accessor method to return this Date object’s year (between 1753 & 9999).
   public int getYear() {
       return year;
   }

   // accessor method to return this Date object’s month (between 1 & 12).
   public int getMonth() {
       return month;
   }

   // accessor method to return this Date object’s day (between 1 & 31).
   public int getDay() {
       return day;
   }

   // accessor method that returns a string giving the day of the week this Date
   // object’s date falls on (Sunday thru Saturday)
   public String getDayOfWeek() {
       final String[] weekDays = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
       return weekDays[new Date().advanceTo(this) % 7];
   }

   // accessor method to return String of this Date object’s field in form
   // Year/Month/Day.
   public String toString() {
       return year + "/" + month + "/" + day;
   }

   // accessor method to return true if this Date object’s year field is leap year
   // (false otherwise).
   public boolean isLeapYear() {
       return ((year % 4) == 0) && !((year % 100 == 0) && (year % 400 != 0));
   }

   // mutator method that advances this Date object’s fields to represent the next
   // date.
   public void nextDay() {
       if (day == numOfDaysInMonth(month)) {
           if (month == 12) {
               year++;
               month = 0;

           }
           month++;
           day = 0;
       }
       day++;
   }

   // mutator method that advances this Date object’s fields to the endDate
   // object’s fields and returns the number of days between the two dates.
   public int advanceTo(Date endDay) {
       int cnt = 0;
       while (!equals(endDay)) {
           nextDay();
           cnt++;
       }
       return cnt;
   }

   // private utility method to get number of days in the given month
   private int numOfDaysInMonth(int month) {
       if (month == 4 || month == 6 || month == 9 || month == 11)
           return 30;
       else if (month == 2)
           return isLeapYear() ? 29 : 28;
       else
           return 31;
   }

   // accessor method to return true if this Date object equals otherDate object
   // (false otherwise).
   public boolean equals(Date otherDate) {
       if (day != otherDate.day)
           return false;
       if (month != otherDate.month)
           return false;
       if (year != otherDate.year)
           return false;
       return true;
   }

}

DateClient.java


import java.util.Scanner;

public class DateClient {

   public static void main(String args[]) {
       Scanner sc = new Scanner(System.in);

       // Taking inputs from the user
       System.out.print("Enter Today’s date in the form (month day year): ");
       int month = sc.nextInt();
       int day = sc.nextInt();
       int year = sc.nextInt();

       System.out.print("Enter Birthday in the form (month day year): ");
       int month1 = sc.nextInt();
       int day1 = sc.nextInt();
       int year1 = sc.nextInt();
       sc.close();

       // creating today's date and date of birth objects
       Date todaysDate = new Date(year, month, day);
       Date dateOfBirth = new Date(year1, month1, day1);

       // temp object to do the date calculation
       Date tempDate = new Date(dateOfBirth.getYear(), dateOfBirth.getMonth(), dateOfBirth.getDay());

       // printing output
       System.out.printf("You were born on %s, which was a %s. You are %d days old.\n", dateOfBirth,
               tempDate.getDayOfWeek(), tempDate.advanceTo(todaysDate));
   }

}

Add a comment
Know the answer?
Add Answer to:
Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java,...
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
  • 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...

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

  • 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). Provide constructors that: 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:...

  • Use Java and creat proper classes to finish this problem Write a class called Date that...

    Use Java and creat proper classes to finish this problem 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...

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

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

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

  • (IN JAVA ) Given the class Date that can prints the date in numerical form. Some...

    (IN JAVA ) Given the class Date that can prints the date in numerical form. Some applications might require the date to be printed in another form, such as March 24, 2005. Please do the following: Derive the class ExtDate so that the date can be printed either form. Add a data member to the class ExtDate so that the month can also be stored in string form. Add a method to output the month in the string format followed...

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

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

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