Question

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 date (their age in days). Example: You were born on 1990/11/30, which was a Friday. You are 9952 days old.

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:

In order to this I've created a Java class but there's an error with the last three methods in the class:

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

   public Date(int yr, int mnth, int dy) {
       this.year = yr;
       this.month = mnth;
       this.day = dy;
   }

   public Date() {
       this.year = 1753;
       this.month = 1;
       this.day = 1;
   }

   public int getYear() {
       return this.year;
   }

   public int getMonth() {
       return this.month;
   }

   public int getDay() {
       return this.day;
   }

   public String toString() {
       return "" + this.year + "/" + this.month + "/" + this.day;
   }

   public boolean equals(Date otherDate) {
       if ((otherDate.getDay() == this.day) && (otherDate.getMonth() == this.month)
               && (otherDate.getYear() == this.year)) {
           return true;
       } else {
           return false;
       }
   }

   public boolean isLeapYear(int year) {
       return ((year % 4) == 0) && !((year % 100 == 0) || (year % 400 != 0));
   }

   public void nextDay() {

       //mutator method that advances this Date object’s fields to represent the next date

       //nextDay() advances this Date object’s day, month and/or year, as needed

      

       //Months that have 31 days

       if(this.month==10 || this.month==8 || this.month==7 || this.month==5

               || this.month==3 || this.month==1) {

           if(this.day==31) {

               this.month++;

               this.day=1;

           } else {

               this.day++;

           }

       }

       //Months that have 30 days

       if(this.month==11 || this.month==9 || this.month==6 || this.month==4) {

           if(this.day==30) {

               this.month++;

               this.day=1;

           }else {

               this.day++;

           }

       }

       //Only for month of December

       if(this.month==12) {

           if(this.day==31) {

               this.year++;

               this.month=1;

               this.day=1;

           } else {

               this.day++;

           }

       }

       //Only for month of Feb.

           if(this.month==2) {

               if(isLeapYear(year)==true) {

                   if(this.day==29) {

                       this.month++;

                       this.day=1;

                   }else {

                       this.day++;

                   }

               }else if(isLeapYear(year)==false) {

                   if(this.day==28) {

                       this.month++;

                       this.day=1;

                   }else {

                       this.day++;

                   }

               }

           }

       }
   public int advanceTo(Date endDate){
   //Initializes the increment to 0
   int counterDate = 0;
   //Increments the counter and uses nextDay() until the counterdate equals the end date.
while (!this.equals(endDate)) {
nextDay();
counterDate++;
}
//Returns the Counter amount
return counterDate;
   }
  
   public String getDayOfWeek(){
   //Creates an array with the weekdays in it.
   String[] weekDays = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
   // returns whichever day that the day is advanced to, modulo 7 because
   // that'll give us an appropriate index in the array
   Date refernceDate = new Date(1753,1,1);
   return weekDays[refernceDate.advanceTo(this) % 7];
   }

}

Can you help me fix these last three methods so that I get the correct input?

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

Please find correct days of week function: I have tested it is working.

String getDayOfWeek(){
    //Creates an array with the weekdays in it.
    String[] weekDays = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
    int[] t = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
    int y = this.year;
    int m = this.month;
    y -= (m < 3) ? 1 : 0;

    int dayCode = ( y + y/4 - y/100 + y/400 + t[m-1] + this.day) % 7; //0 for Sunday, 1 for Monday, etc.
    return weekDays[dayCode];
}
Add a comment
Know the answer?
Add Answer to:
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...
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...

  • My values are not storing into my object and I keep returning zero. please explain my...

    My values are not storing into my object and I keep returning zero. please explain my mistake thanks public class CalanderDate { int year = 0 ; int day = 0; int month= 0;    public static void main(String[] args) { CalanderDate date; date = new CalanderDate( 1, 10 ,1999); System.out.print(date); }          public CalanderDate(int month, int day , int year ) { if (month < 1){ month = 1; } if (month> 12){ month = 12; }...

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

  • this needs to be in Java: use a comparator class which is passed into the sort...

    this needs to be in Java: use a comparator class which is passed into the sort method that is available on the ArrayList. import java.util.Scanner; public class Assign1{ public static void main(String[] args){ Scanner reader = new Scanner (System.in); MyDate todayDate = new MyDate(); int choice = 0; Library library = new Library(); while (choice != 6){ displayMainMenu(); if (reader.hasNextInt()){ choice = reader.nextInt(); switch(choice){ case 1: library.inputResource(reader, todayDate); break; case 2: System.out.println(library.resourcesOverDue(todayDate)); break; case 3: System.out.println(library.toString()); break; case 4: library.deleteResource(reader,...

  • java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the...

    java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the below classes: public class Date {    private String month;    private String day;    private String year;    public Date(String month, String day, String year) {       this.month = month;       this.day = day;       this.year = year;    }    public String getMonth() {       return month;    }    public String getDay() {       return day;    }    public String...

  • Problem Description: Expand on 2 by implementing Insertion Sort and displaying the dates sorted after adding...

    Problem Description: Expand on 2 by implementing Insertion Sort and displaying the dates sorted after adding one to all dates. Run your code using the attached input files outputlong.txtoutputlab3.txt. Please note that your the dates that are sorted will be the ones after 1 day has been added to them. Sample Output: (green is user input) Run #2 using sample file outputlab3.txt Enter name of file to import or the word null to bypass: outputlab3.txt How many assessments in this...

  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

  • Hello, I have a bug in my code, and when I run it should ask the...

    Hello, I have a bug in my code, and when I run it should ask the user to enter the month and then the year and then print out the calendar for the chosen month and year. My problem with my code is that when I run it and I enter the month, it doesn't ask me for the year and it prints all the years of the month I chose. Please help! Code: #include "calendarType.h" #include <iostream> using namespace...

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

  • Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical...

    Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical form. Some applications might require the date to be printed in another form, such as March 24, 2019. Derive the class extDateType so that the date can be printed in either form. Add a member variable to the class extDateType so that the month can also be stored in string form. Add a member function to output the month in the string format, followed...

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