Question

I'm having trouble with the daysSince() Method in bold at the end of the program public...

I'm having trouble with the daysSince() Method in bold at the end of the program

public class Date {
        static final  int [] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        private int day, month, year;
        public Date() {
                day = 1;
                month = 1;
                year = 2000;
        }
        public Date(int d, int m, int y) {
                day = d;
                month = m;
                year = y;
        }
        public Date (String s) {
                setDate(s);
        }
        public void setDate(String s){
                //two cases / format or - format
                //2019-1-23 or  1/23/2019
                if(s.contains("/")){
                        //"/" format use split on "/"
                        String[] st = s.split("/");
                        if(st.length != 3){
                                System.out.println("Too many ot too little slashes");
                        }else {
                                month = Integer.parseInt(st[0]);
                                day = Integer.parseInt(st[1]);
                                year = Integer.parseInt(st[2]);
                        }
                }else if(s.contains("-")){
                        // "-" format
                        String[] st = s.split("-");
                        if(st.length != 3){
                                System.out.println("Too many ot too little slashes");
                        }else {
                                year = Integer.parseInt(st[0]);
                                month = Integer.parseInt(st[1]);
                                day = Integer.parseInt(st[2]);
                                
                        }
                }else{
                        System.out.println("Invalid format");
                }
                
        }
        public int getDay() {
                return day;
        }
        public void setDay(int day) {
                this.day = day;
        }
        public int getMonth() {
                return month;
        }
        public void setMonth(int month) {
                this.month = month;
        }
        public int getYear() {
                return year;
        }
        public void setYear(int year) {
                this.year = year;
        }
        public String toString() {
                String st = month+"/" + day+ "/" + year;
                return st;
        }
        public boolean equals(Date d){
                return (this.day == d.day && this.month == d.month && this.year == d.year);
        }
        public boolean isBefore(Date d) {
                //returns true if d is before 
                if(d.year > this.year) 
                        return true;
                else if(year == d.year) {
                        if(d.month > this.month) 
                                return true;
                        else if(d.month == this.month) {
                                if(d.day > this.day )
                                        return true;
                        }
                }
                return false;//equal will be in the false
        }
        public int daysSince (Date d) {
                int days = 0;
                //your code to compute days goes here

            //daysSince(Date d) returns how many days from this date to date d 
                return days;
        }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

public class Date {

        static final int[] daysInMonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

        private int day, month, year;

        public Date() {

                day = 1;

                month = 1;

                year = 2000;

        }

        public Date(int d, int m, int y) {

                day = d;

                month = m;

                year = y;

        }

        public Date(String s) {

                setDate(s);

        }

        public void setDate(String s) {

                // two cases / format or - format

                // 2019-1-23 or 1/23/2019

                if (s.contains("/")) {

                        // "/" format use split on "/"

                        String[] st = s.split("/");

                        if (st.length != 3) {

                                System.out.println("Too many ot too little slashes");

                        } else {

                                month = Integer.parseInt(st[0]);

                                day = Integer.parseInt(st[1]);

                                year = Integer.parseInt(st[2]);

                        }

                } else if (s.contains("-")) {

                        // "-" format

                        String[] st = s.split("-");

                        if (st.length != 3) {

                                System.out.println("Too many ot too little slashes");

                        } else {

                                year = Integer.parseInt(st[0]);

                                month = Integer.parseInt(st[1]);

                                day = Integer.parseInt(st[2]);

                        }

                } else {

                        System.out.println("Invalid format");

                }

        }

        public int getDay() {

                return day;

        }

        public void setDay(int day) {

                this.day = day;

        }

        public int getMonth() {

                return month;

        }

        public void setMonth(int month) {

                this.month = month;

        }

        public int getYear() {

                return year;

        }

        public void setYear(int year) {

                this.year = year;

        }

        public String toString() {

                String st = month + "/" + day + "/" + year;

                return st;

        }

        public boolean equals(Date d) {

                return (this.day == d.day && this.month == d.month && this.year == d.year);

        }

        public boolean isBefore(Date d) {

                // returns true if d is before

                if (d.year > this.year)

                        return true;

                else if (year == d.year) {

                        if (d.month > this.month)

                                return true;

                        else if (d.month == this.month) {

                                if (d.day > this.day)

                                        return true;

                        }

                }

                return false;// equal will be in the false

        }

        public int daysSince(Date d) {

                int days = 0;

                // calculating days

                long n1 = year * 365 + day;

                for (int i = 1; i < month; i++)

                        n1 += daysInMonth[i];

                //calculating leap years;

                int yrs = year;

                if (month <= 2)

                        yrs--;

                n1 += yrs / 4 - yrs / 100 + yrs / 400;

                //calculating days

                long n2 = d.year * 365 + d.day;

                for (int i = 1; i < d.month; i++)

                        n2 += daysInMonth[i];

                //calculating leap years;

                yrs = d.year;

                if (d.month <= 2)

                        yrs--;

                n2 += yrs / 4 - yrs / 100 + yrs / 400;

                days = (int)(n2 - n1);

                return days;

        }

}

Add a comment
Know the answer?
Add Answer to:
I'm having trouble with the daysSince() Method in bold at the end of the program public...
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
  • 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; }...

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

  • Need help figuring out what would be the all 12 days output public class Date {...

    Need help figuring out what would be the all 12 days output public class Date {    private int day;    private int month;    private int year;       private static int tracker;       public Date()    {        this(1, 1, 2000);        System.out.println("4");    }       public Date(int year)    {        this(1, 1, year);        System.out.println("3");    }       public Date(int month, int year)    {        this(1, month,...

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

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

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

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

  • Extend this date validation program, so that it checks for valid leap years (in which the...

    Extend this date validation program, so that it checks for valid leap years (in which the month February has 29, instead of 28, days). A leap year is any year that is divisible by 4 but not divisible by 100, unless it is also divisible by 400. Do not allow February to have 29 days on years that are not leap years. **Please view both photos for the entire code and post new code with the leap year modification. Thank...

  • Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then...

    Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then generate their documentation. Examine the documentation to see the logic used in creating each class. (Code given below) import java.util.ArrayList; import java.util.Iterator; class Calendar { private Year year; public Calendar() { year = new Year(); } public void printCalendar() { year.printCalendar(); } } class Year { private ArrayList<Month> months; private int number; public Year() { this(2013); } public Year(int number) { this.number = number;...

  • Need Help finishing up MyCalendar Program: Here are the methods: Modifier and Type Method Desc...

    Need Help finishing up MyCalendar Program: Here are the methods: Modifier and Type Method Description boolean add​(Event evt) Add an event to the calendar Event get​(int i) Fetch the ith Event added to the calendar Event get​(java.lang.String name) Fetch the first Event in the calendar whose eventName is equal to the given name java.util.ArrayList<Event> list() The list of all Events in the order that they were inserted into the calendar int size() The number of events in the calendar java.util.ArrayList<Event>...

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