Design and implement a class called DayType that implements the day of the week in a program. The class DayType should store the day, such as Sun for Sunday, Mon for Monday and so on and so forth. The program should be able to perform the following operations on an object of type of DayType: Set the day Print the day Return the day Return the next day Return the previous day Calculate and return the day by adding and subtracting certain days to the current day (i.e if current day is Monday and we add 6 days, the day to be returned is Sunday. Similarly is we subtract 3 days, the day to be return is Friday)
instruction - Define DayType class appropriate methods and constructor - Your program should get the input/output via the main method and pass appropriate arguments through the class methods. - Your program should continuously running until it receives a special signal (character/number) in which the program will exit.
Hi Friend,
Try this:
public class DayType{ final static int SUN = 0; final static int MON = 1; final static int TUE = 2; final static int WED = 3; final static int THU = 4; final static int FRI= 5; final static int SAT = 6; 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; 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 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(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 5 days: "); d.setDay(d.addDays(5)); d.print(); } }
Hope that it will be helpful for you.
Thanks
hi member
ask something haves one error in public static void and daytypes d=newtypes(sunday) ; program said cannot have static declared you know what the solution
Ads