Design and implement the class Day that implements the day ofthe week in a progr
ID: 3641246 • Letter: D
Question
Design and implement the class Day that implements the day ofthe week in a program. The class Day should store the day,such as Sun for Sunday. The program should be able to performthe following operations on the 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. calualte and return the day by adding certain days to thecurrent day.. Such as if today is monday and we add four daysit will return Friday
g. add the appropriate constructors
h.write the definitions of the methods to implement a -g
i.write a program to test the operations of class Day.
Explanation / Answer
Please rate...
class Day
{
private String d;
public Day()
{
}
public Day(String a)
{
d=a;
}
public void setDay(String s)
{
d=s;
}
public void PrintDay()
{
System.out.print(d);
}
public String getDay()
{
return d;
}
public String nextDay()
{
String ar[]={"Sun","Mon","Tues","Wednes","Thurs","Fri","satur"};
int i;
for(i=0;i<7;i++)
{
if(d.equalsIgnoreCase(ar[i]))return ar[(i+1)%7];
}
return d;
}
public String previousDay()
{
String ar[]={"Sun","Mon","Tues","Wednes","Thurs","Fri","satur"};
int i;
for(i=0;i<7;i++)
{
if(d.equalsIgnoreCase(ar[i])){
if(i>0)return ar[i-1];
else return ar[6];
}
}
return d;
}
public String calcDay(int a)
{
String ar[]={"Sun","Mon","Tues","Wednes","Thurs","Fri","satur"};
int i;
for(i=0;i<7;i++)
{
if(d.equalsIgnoreCase(ar[i]))return ar[(i+a)%7];
}
return d;
}
}
class TestDay
{
public static void main(String args[])
{
Day d=new Day();
d.setDay("Sun");
System.out.print("The day is ");
d.PrintDay();
System.out.println("day");
System.out.println("The next day is "+d.nextDay()+"day");
System.out.println("The previous day is "+d.previousDay()+"day");
System.out.println("The day after 4 days is "+d.calcDay(4)+"day");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.