# Thread: Homework Assignment, Need some help

1. Member
Join Date
Mar 2012
Posts
34
Rep Power
0

## 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. Senior Member
Join Date
Feb 2012
Posts
117
Rep Power
0

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

4. Member
Join Date
Apr 2012
Posts
5
Rep Power
0

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

#### Posting Permissions

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