I have the code, but I need comments added please. import java.util.Scanner; pub
ID: 3664565 • Letter: I
Question
I have the code, but I need comments added please.
import java.util.Scanner;
public class Day
{
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 CurrentDay)
{
String NextDay = null;
for (int i = 0; i < Days.length; i++)
{
if (CurrentDay.equalsIgnoreCase(Days[i]))
{
if (i == Days.length - 1)
{
NextDay = Days[0];
break;
}
else
{
NextDay = Days[++i];
break;
}
}
}
return NextDay;
}
public String GetPrevDay(String CurrentDay)
{
String PrevDay = null;
for (int i = 0; i < Days.length; i++)
{
if (CurrentDay.equalsIgnoreCase(Days[i]))
{
if (i == 0)
{
PrevDay = Days[Days.length - 1];
break;
}
else
{
PrevDay = Days[--i];
break;
}
}
}
return PrevDay;
}
public String calDay(String CurrentDay, int no)
{
String ResDay = null;
no = no % 7;
for (int i = 0; i < Days.length; i++)
{
if (CurrentDay.equalsIgnoreCase(Days[i]))
{
ResDay = Days[(i + no) % 7];
break;
}
}
return ResDay;
}
}
/**
* Tester for Day.java
*/
import java.util.*;
public class Tester
{
//scanner
static Scanner console = new Scanner(System.in);
public static void main(String args[])
{
System.out.println("Enter a day: Sun, Mon, Tue, Wed, Thur, Fri, or Sat");
Scanner sc = new Scanner(System.in);
String day = sc.nextLine();
Day day2 = new Day();
day2.setDay(day);
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, 12));
}
}
Explanation / Answer
import java.util.Scanner;
class Day{
private String day = null;
// Array of Days
String Days[] = {"Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat"};
// function to set the value of variable day
public void setDay(String day){
this.day = day;
}
// fucntion to print the current day
public void PrintDay(){
System.out.println("Given day is: " + GetDay());
}
// function that return current day
public String GetDay(){
return day;
}
// Function that return the next day (given current day as an argument)
public String GetNextDay(String CurrentDay){
String NextDay = null;
// go through the Days Array until we do not find the Current Day
for (int i = 0; i < Days.length; i++){
// if ith day Match with the current day
if (CurrentDay.equalsIgnoreCase(Days[i])){
if (i == Days.length - 1){ // if the day is Saturday
NextDay = Days[0]; // next day will be sunday
break; // break the for loop
}
else{ // if the day is other than Satuday
NextDay = Days[++i];
break; // break the for loop
}
}
}
return NextDay; // return the current day
}
// Function that return the previous day (given current day as an argument)
public String GetPrevDay(String CurrentDay){
String PrevDay = null;
// go through the Days Array until we do not find the Current Day
for (int i = 0; i < Days.length; i++){
// if ith day Match with the current day
if (CurrentDay.equalsIgnoreCase(Days[i])){
if (i == 0){ // if the current day is Sunday
PrevDay = Days[Days.length - 1]; // prev day will be Satuday
break; // break the for loop
}
else{
PrevDay = Days[--i]; // if the day is other than Sundayc2
break; // break the for loop
}
}
}
return PrevDay;
}
// Calculate the day number of days after Current Days
public String calDay(String CurrentDay, int no){
String ResDay = null;
no = no % 7;
// go through the Days Array until we do not find the Current Day
for (int i = 0; i < Days.length; i++){
// if ith day Match with the current day
if (CurrentDay.equalsIgnoreCase(Days[i])){
ResDay = Days[(i + no) % 7]; // Calculate the days that is number of days (no) after this day
break; // break the for loop
}
}
return ResDay;
}
}
import java.util.*;
public class Tester{
static Scanner console = new Scanner(System.in);
public static void main(String args[]){
// Ask the user for a Day input
System.out.println("Enter a day: Sun, Mon, Tue, Wed, Thur, Fri, or Sat");
//scanner for reading from Console
Scanner sc = new Scanner(System.in);
// read the user response
String day = sc.nextLine();
// Day CLass Object
Day day2 = new Day();
day2.setDay(day); // Set the Current Day to the day enter by the user
day2.PrintDay(); // print the Current Day
System.out.println("Previous Day Is: " + day2.GetPrevDay(day)); // Get previous Day
System.out.println("Next Day Is: " + day2.GetNextDay(day)); // Get Next Day
System.out.println("Calculated Day Is: " + day2.calDay(day, 12)); // Calculate day after 12 days the current day
}
}