Ok so I wrote this with some help of a friend and it compiles fine and everything, however when I try and run it I get this error message.
"Static Error: No constructor in Day matches this invocation
Arguments: ()
Candidate signatures: Day(int)"
I am not sure what this means or how to fix it to run. Any help would be appreciated. Also this program is supposed to do the following.

Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type Day:
a) Set the day.
b) Print the day.
c) Return the day.
d) Return the next day.
e) Return the previous day.
f) Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add four days, the day to be returned is Friday.
g) Add the appropriate constructors.
h) Write a program to test various operations on the class Day.

-thanks-

public class Day 
{
final static int SUNDAY = 0;
final static int MONDAY = 1;
final static int TUESDAY = 2;
final static int WEDNESDAY = 3;
final static int THURSDAY = 4;
final static int FRIDAY = 5;
final static int SATURDAY = 6;

public int day;

public Day(int day) 
{
this.day = day;
}
public void setDay(int day) 
{
this.day = day;
}
public int getDay() 
{
return day;
}
public void print() 
{
System.out.println(this.toString());
}
public int nextDay() 
{
int next;
next = day + 1;
return next;
}
public int previousDay() {
int prevDay;
prevDay = day - 1;
return prevDay;
}
public int addDays(int days) 
{
return (day + days) % 7;
}
public String toString() 
{
switch (this.day) 
{
case SUNDAY:
return "Sunday";
case MONDAY:
return "Monday";
case TUESDAY:
return "Tuesday";
case WEDNESDAY:
return "Wednesday";
case THURSDAY:
return "Thursday";
case FRIDAY:
return "Friday";
case SATURDAY:
return "Saturday";
}
return "";
}
public static void main(String[] args) 
{
System.out.println("Test Day");
System.out.println();
System.out.print("Set day: ");
Day d = new Day(SUNDAY);
d.print();
System.out.print("Next day: ");
d.setDay(d.nextDay());
d.print();
System.out.print("Previous day: ");
d.setDay(d.previousDay());
d.print();
System.out.print("After 4 days: ");
d.setDay(d.addDays(4));
d.print();
}
}

Recommended Answers

All 6 Replies

Copied it to Eclipse and ran it - didn't get an Error message, but the output seems off:

Test Day
Set day: Sunday
Next day: Monday
Previous day: Sunday
After 4 days: Thursday

Your previousDay and nextDay are not modulo 7, meaning that they can get values that are not in the range 0-6, so the toString() method returns "".

thanks for the fast reply, sorry it took me so long to reply, work has been crazy the last couple days... Hmm.. Well I am glad you could run it.. I use Dr.Java and it gives me that error and wont let me run the program, so I can't test to see that it works of if I change something if that change works... Any idea why it would give me that error?

Since for me it works I can only try and guess - it seems like the compiler can't find the constructor Day(), which is bizarre since you don't use this constructor but the Day(int) which does exist. Try creating a constructor with no arguments

public Day() 
{
  //nothing here.
}

If this code works, then try to make the constructor private, so no user would be able to construct a Day object without a day.

public int nextDay() 
{
int next;
next = day + 1;
return next;
}

What if the day is Saturday (number = 6) ?

Then you'll just increase it to 7, but what you really want is to reset the number to 0.

Same happens in reverse (previous day).

Ok so for some reason if I use your code apines, I can run the program.. I tried to set the constructor to private and then it gave me the same error again!
I updated some of the code, and since I could run it I noticed it was setting the previous day based off of the next day.. I wanted it to go of the set date. So I updated the little chunk of code for next/previous/add days. It works perfectly for every day but Saturday and Sunday, in which both cases return a blank for previous day. Cant seem to get it to work for those two days.

public int nextDay() 
{
int next;
next = (day + 1) % 7;
return next;
}
public int previousDay() 
{
int prevDay;
prevDay = (day - 2) % 7;
return prevDay;
}
public int addDays(int days) 
{
return (day + days + 1) % 7;
}

Your problem occurs because (-1) % 7 = -1 and not 1. The modulo makes the addition cyclic (since we are always adding only non-negative numbers), but the subtraction is not cyclic in the way you want it. One way to make it cyclic is with if statements:

public int previousDay() 
{
   int prevDay;
   if(day > 0)
   {
      prevDay = (day - 1);	
   }
   else
   {
      prevDay = 6;
   }
   return prevDay;
}

Also, your addDays method adds one more to the given days, you just need the add modulo 7:

public int addDays(int days) 
{
   return (day + days) % 7;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.