Using jGrasp and the Software Development Kit, write a program in response to th
ID: 3762193 • Letter: U
Question
Using jGrasp and the Software Development Kit, write a program in response to the following prompt:
Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type Day:
Set the day.
Print the day.
Return the day.
Return the next day.
Return the previous day.
Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add four days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.
Add the appropriate constructors.
Write the definitions of the methods to implement the operations for the class Day, as defined in A through G.
Write a program to test various operations on the class Day.
To submit your compiled Java code for this assignment, compress the .java file into a single .zip file.
For additional details, refer to the Programming Problem Rubric in the Assignment Guidelines and Rubrics folder.
Sample user input and output might be
Enter the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat): Mon
You picked Mon
Previous day is Sun
Next day is Tue
Enter Num: 2
2 day(s) from Mon is Wed
or
Enter the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat): wed
You picked Wed
Previous day is Tue
Next day is Thu
Enter Num: 10
10 day(s) from wed is Sat
Explanation / Answer
Complete Program:
// File: Day.java
public class Day
{
final static int DAYS_IN_WEEK = 7;
private String day;
public Day()
{
day = "Sun";
}
public Day(String strDay)
{
setDay(strDay);
}
public void setDay(String strDay)
{
day = strDay;
}
public void printDay()
{
System.out.println(day);
}
public String getDay()
{
return day;
}
public String nextDay()
{
int dayNum = getIntDay(day);
int next = (dayNum + 1) % DAYS_IN_WEEK;
return getStrDay(next);
}
public String previousDay()
{
int dayNum = getIntDay(day);
int previous = (dayNum - 1 + DAYS_IN_WEEK) % DAYS_IN_WEEK;
return getStrDay(previous);
}
public void addDays(int days)
{
int dayNum = getIntDay(day);
dayNum = (dayNum + days) % DAYS_IN_WEEK;
day = getStrDay(dayNum);
}
private int getIntDay(String strDay)
{
if(strDay.equalsIgnoreCase("Sun"))
return 0;
else if(strDay.equalsIgnoreCase("Mon"))
return 1;
else if(strDay.equalsIgnoreCase("Tue"))
return 2;
else if(strDay.equalsIgnoreCase("Wed"))
return 3;
else if(strDay.equalsIgnoreCase("Thu"))
return 4;
else if(strDay.equalsIgnoreCase("Fri"))
return 5;
else if(strDay.equalsIgnoreCase("Sat"))
return 6;
else
return 0;
}
private String getStrDay(int d)
{
if(d == 0)
return "Sun";
else if(d == 1)
return "Mon";
else if(d == 2)
return "Tue";
else if(d == 3)
return "Wed";
else if(d == 4)
return "Thu";
else if(d == 5)
return "Fri";
else if(d == 6)
return "Sat";
else
return "Sun";
}
}
// File: DayTest.java
import java.util.Scanner;
public class DayTest
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Day dayObj = new Day();
System.out.print("Enter the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat): ");
String strDay = input.next();
dayObj.setDay(strDay);
System.out.println("You picked " + dayObj.getDay());
System.out.println("Previous day is " + dayObj.previousDay());
System.out.println("Next day is " + dayObj.nextDay());
System.out.print("Enter Num: ");
int more = input.nextInt();
dayObj.addDays(more);
System.out.println(more + " day(s) from " + strDay + " is " + dayObj.getDay());
}
}
Sample Output 1:
Sample Output 2:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.