For the following code: Add notations to this code explaining the process. Add t
ID: 3661210 • Letter: F
Question
For the following code:
Add notations to this code explaining the process.
Add the appropriate constructors.
Write the definitions of the methods to implement the operations for the class Day
Write a program to test various operations on the class Day
import java.util.Scanner;
public class Day {//class
private String day = null;
String days[] = { "Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat" };//
public void setDay(String day)
{
this.day = day;
}
public void printDay()
{
System.out.println("Given day is " + getDay());
}
public String getDay()
{
return day;
}
public String getNextDay(String currDay)
{
String nextDay = null;
for (int i = 0; i < days.length; i++)
{
if (currDay.equalsIgnoreCase(days[i]))
{
if (i == days.length - 1)
{
nextDay = days[0];
break;
} else
{
nextDay = days[++i];
break;
}
}
}
return nextDay;
}
public String getPrevDay(String currDay)
{
String prevDay = null;
for (int i = 0; i < days.length; i++)
{
if (currDay.equalsIgnoreCase(days[i]))
{
if (i == 0)
{
prevDay = days[days.length - 1];
break;
}
else
{
prevDay = days[--i];
break;
}
}
}
return prevDay;
}
public String calDay(String currDay, int no)
{
String resDay = null;
no = no % 7;
for (int i = 0; i < days.length; i++)
{
if (currDay.equalsIgnoreCase(days[i]))
{
resDay = days[(i + no)%7];
break;
}
}
return resDay;
}
public static void main(String args[]) {
System.out.println("Enter the Day");
Scanner sc = new Scanner(System.in);
String day = sc.nextLine();
Day day2 = new Day();
day2.setDay(day);
System.out.print("Enter number of calculated days: ");
int calculated = sc.nextInt();
day2.printDay();
System.out.println("Previous Day is " + day2.getPrevDay(day));
System.out.println("Next Day is " + day2.getNextDay(day));
System.out.println("Calculated Day is " + day2.calDay(day, calculated));
}
}
Explanation / Answer
A small change fixes this, indicated in red . jus remove that line it works perfect!!
public String calDay(String currDay, int no) {
String resDay = null;
no = no % 7; remove this line
for (int i = 0; i < days.length; i++) {
if (currDay.equalsIgnoreCase(days[i])) {
resDay = days[(i + no) % 7];
break;
}
}
return resDay;
}