Results 1 to 4 of 4
  1. #1
    xdrazkalnytex is offline Member
    Join Date
    Mar 2012
    Posts
    34
    Rep Power
    0

    Default Homework Assignment, Need some help

    I need help on a homework assignment. The problem that I am stuck on is returning the day of the week corresponding to this date.
    I got the formula and everything finish. But I don't know how to return the date, or a string.





    // Date.java

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

    // Construct a Date given the year, month, and day of month.
    public Date(int year, int month, int day)
    {
    // TBD
    this.year = year;
    this.month = month;
    this.day = day;
    }

    // Return the day of week corresponding to this date.
    public String dayOfWeek()
    {
    // TBD
    int y0 = year - (14 - month) / 12;;
    int x = y0 + y0 / 4 - y0 / 100 + y0 / 400;
    int m0 = month + 12 * ((14 - month) / 12 ) - 2;
    int d0 = (day + x + (31 * m0) / 12 ) % 7;

    return dayOfWeek();
    }

    // Is this date same as b?
    public boolean equals(Date b)
    {
    // TBD
    if (b == this)
    return true;
    Date a = this;
    return (this.month == b.month) && (this.day == b.day) && (this.year == b.year);
    }

    // Return a String representation of this date.
    public String toString()
    {
    // TBD
    return " " + year + "-" + month + "-" + day;
    }

    // Test driver.
    public static void main(String[] args)
    {
    Date a = new Date(1974, 7, 8);
    Date b = new Date(1879, 3, 14);
    Date c = new Date(1879, 3, 14);
    System.out.println("Should print 1974-7-8: " + a);
    System.out.println("Should print Monday: " + a.dayOfWeek());
    System.out.println("Should print 1879-3-14: " + b);
    System.out.println("Should print Friday: " + b.dayOfWeek());
    System.out.println("Should print false: " + a.equals(b));
    System.out.println("Should print true: " + b.equals(c));
    }
    }

  2. #2
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Homework Assignment, Need some help

    Java Code:
    // Return a String representation of this date.
    public String toString()
    {
    return " " + year + "-" + month + "-" + day;
    }
    You're returning a String right there. You might have some issues with converting from int to String though. If you do, you can wrap each int like this: (String)(int_variable_name). This is known as "Type Casting"

    In your .equals method, what does this line do? You never reference a again.
    Date a = this;

    Java Code:
    public String dayOfWeek()
    ...
    int d0 = (day + x + (31 * m0) / 12 ) % 7;
    return dayOfWeek();
    Looks like d0 is the day of the week, right? So the first day of the week (d0==0) would make the function return "Sunday". An array of Strings called dayNames could come in handy here.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Homework Assignment, Need some help

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    trevMD is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: Homework Assignment, Need some help

    > You're returning a String right there. You might have some issues with converting from int to String though.
    no issue here since you are using String concat between each integer.



    Java Code:
    // Return the day of week corresponding to this date.
    public String dayOfWeek(){
    // TBD
    int y0 = year - (14 - month) / 12;;
    int x = y0 + y0 / 4 - y0 / 100 + y0 / 400;
    int m0 = month + 12 * ((14 - month) / 12 ) - 2;
    int d0 = (day + x + (31 * m0) / 12 ) % 7;
    
    return dayOfWeek();
    }
    > return dayOfWeek()
    this cause the function to call itself again and again and again (recursion). It will keep calling itself until you exhausted the Stack and get a StackOverFlowException.

    what you want is to return a DAY OF WEEK String..like "MONDAY", "TUESDAY". Since your algorithm return 0 as SUNDAY, you wanna do something like this

    if (d0 == 0) return "SUNDAY";
    else if (d0 == 1) return "MONDAY"
    etc.

Similar Threads

  1. Homework Assignment, Need some help
    By xdrazkalnytex in forum New To Java
    Replies: 17
    Last Post: 04-11-2012, 05:56 PM
  2. Replies: 7
    Last Post: 02-29-2012, 01:15 PM
  3. Need help with homework
    By bkim33 in forum New To Java
    Replies: 9
    Last Post: 02-11-2011, 04:50 AM
  4. Need some help with my homework.
    By JacobM in forum New To Java
    Replies: 11
    Last Post: 02-04-2011, 02:42 AM
  5. Calculator Program HELP NEEDED FAST! Homework assignment
    By SteroidalPsycho in forum New To Java
    Replies: 3
    Last Post: 03-05-2009, 04:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •