0

I need to create a boolean method called comesBefore that checks to see if a date comes before another date while only passing 1 variable.

public static void test(Date d1, Date d2)
{
    if (d1.comesBefore(d2))
    {
        System.out.println(d1 + " comes before " + d2);
    }
    else
    {
        System.out.println(d1 + " doesn't come before " + d2);
    }
}

I'm trying to figure out how to do a comparison with only 1 variable is being passed.

 d1.comesBefore(d2)

Calls this method

public boolean comesBefore(Date d2)
{

}

I've been wracking my brain trying to figure out how to accomplish this. And knowing me, it's probably something incredibly simple.

Thanks in advance.

Here is the full code.

DateDemo.java (cannot be edited/changed)

public class DateDemo
{
    public static void test(Date d1, Date d2)
    {
        if (d1.comesBefore(d2))
        {
            System.out.println(d1 + " comes before " + d2);
        }
        else
        {
            System.out.println(d1 + " doesn't come before " + d2);
        }
    }

    public static void main(String[] args)
    {
        test(new Date(3, 23, 1900), new Date(3, 23, 1900));

        test(new Date(3, 23, 1899), new Date(3, 23, 1900));
        test(new Date(2, 23, 1900), new Date(3, 23, 1900));
        test(new Date(3, 22, 1900), new Date(3, 23, 1900));

        test(new Date(3, 27, 1900), new Date(3, 23, 1900));
        test(new Date(5, 23, 1900), new Date(3, 23, 1900));
        test(new Date(3, 22, 1901), new Date(3, 23, 1900));
    }
}

Date.java (class I am creating and need to submit to hypergrade)

private String month = "";
private int day, year;

public Date(int date, int day, int year)
{
    if(date == 1)
        this.month = "January";
    else if(date == 2)
        this.month = "February";
    else if(date == 3)
        this.month = "March";
    else if(date == 4)
        this.month = "April";
    else if(date == 5)
        this.month = "May";
    else if(date == 6)
        this.month = "June";
    else if(date == 7)
        this.month = "July";
    else if(date == 8)
        this.month = "August";
    else if(date == 9)
        this.month = "September";
    else if(date == 10)
        this.month = "October";
    else if(date == 11)
        this.month = "November";
    else if(date == 12)
        this.month = "December";
    else
        this.month = "Invalid this.month";

    this.day = day;
    this.year = year;
}

public Date(String month, int day, int year)
{
    this.month = month;
    this.day = day;
    this.year = year;
}

public String toString()
{
    return month + " " + day + ", " + year;
}

public boolean comesBefore(Date d2)
{
    //>>>>>Trying to figure out what goes here<<<<<
}
Snoopy101x
  • 19
  • 1
  • 3
  • If it's homework, why not ask the person who assigned it? Shouldn't they be the ones ensuring you understand this stuff? Questions about homework are fine as long as they're formatted in a way where viewers can't tell it's your homework; the post must appear to be an actual, independent question, including your own attempts and research effort.. As for putting "*Homework*" in the title, you're basically asking for downvotes. – Vince Sep 19 '17 at 04:10
  • @VinceEmigh I didn't want to be presumptions or misleading. I just wanted to be straight forward. I will be asking my teacher about it, I was just hoping I could find some help. Like I said it's probably something so simple I've overlooked it. – Snoopy101x Sep 19 '17 at 04:13
  • One thing to consider is whether storing month as the *name* of the month is really what you want. If you're doing comparisons between months, consider how hard that will be when you just have the name! – Mark Peters Sep 19 '17 at 04:15
  • `comesBefore` is an instance method. Simply compare against the argument's state: `return year < d2.year && ...`. Although you may wanna take my advice and remove any traces of "homework", as it's frowned upon on this site, and could lead to others downvoting before they even read your post. – Vince Sep 19 '17 at 04:18
  • @MarkPeters well in the demo, an integer month is being passed and converted to a string. But I suppose I could just save the int and use that to compare. – Snoopy101x Sep 19 '17 at 04:22
  • Sounds like you need to check three different conditions. (1) The first date comes in an earlier year than the second one. (2) Same year, but the first date comes in an earlier month than the second one. (3) Same year and month, but the first date is an earlier day than the second one. Write some code, and ask again if you get stuck. – Dawood ibn Kareem Sep 19 '17 at 05:10
  • @DawoodibnKareem, that's what I figured. I originally tried to concat the whole thing into strings and .compareTo each of them. But for some reason the last one being compared didn't work. So I broke the whole thing down and had the same problem. All but that last one come out correct. For some reason the 1901 is throwing a monkey wrench in the works. And I'm not sure why. – Snoopy101x Sep 19 '17 at 05:15

0 Answers0