Hello, Previously I was asked to post a specific question.. so I hope this one specific enough
This is in concern to the exercise mentioned here
http://www.javaprogrammingforums.com...ogramming.html
Now, here is my code:
public class DayType { final static int SUN = 1; final static int MON = 2; final static int TUE = 3; final static int WED = 4; final static int THU = 5; final static int FRI= 6; final static int SAT = 7; private int day; public DayType(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; if (day<7) { next = (day + 1); } else { next = 1; } return next; } public int previousDay(){ int prevDay; if (day>1) { prevDay = (day - 1); } else { prevDay = 7; } return prevDay; } public int addDays(int days) { return (day + days) % 7; } public String toString() { switch (this.day) { case SUN: return "Sunday"; case MON: return "Monday"; case TUE: return "Tuesday"; case WED: return "Wednesday"; case THU: return "Thursday"; case FRI: return "Friday"; case SAT: return "Saturday"; } return ""; } public static void main(String[] args) { System.out.println("******Test Day******"); System.out.println(); System.out.print("Set day: "); DayType d = new DayType(SUN); 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 5 days: "); d.setDay(d.addDays(5)); d.print(); } }
The output of this code is as follows:
******Test Day****** Set day: Sunday Next day: Monday Previous day: Sunday After 5 days: Friday
First of all, the problem is that my previous day comes out to be Sunday instead of Saturday, even though I don't see any problem with the code.
Secondly, I don't know what overloading means in JAVA. I want to overload the methods for items A and C (from the question). The overloaded methods should accept and return an int value representing the day of week (e.g. Sunday = 1, Monday =2, …, Saturday = 7).
Please help me!
Thanks