Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java programming 5th edition chapter 8 User-Defined Classes and ADTs using Strin

ID: 3783916 • Letter: J

Question

Java programming 5th edition chapter 8 User-Defined Classes and ADTs using Strings

Use an if statement to convert word back into a number

and case to convert number back to word

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:

Hint: there is one attribute and to calculate it's totalDaysNum = currentDaysNum + addOnDays;

newDay = totalDaysNum % 7;

a. Set the day.

b. Print the day.

c. Return the day. is a get method

d. Return the next day.

e. Return the previous day.

f. 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 turned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.

g. Add the appropriate constructors.

h. Write the definitions of the methods to implement the operations for the class Day, as defined in a through g.

i. Write a program to test various operations on the class Day.

output:

Output for day1:

The current day is

Output for day2:

The current day is Tuesday

New output for day1:

The current day is Friday

Output for day2: Tuesday

The day after Tuesday is Wednesday

The day before Tuesday is Monday

Enter the number of days from Friday: 3

A Monday is 3 days past Friday

Enter the number of days from Tuesday: 12

A Sunday is 12 days past Tuesday

Explanation / Answer

Create test.java file and paste below code in it:

public class test
{
public static void main(String[] args)
{
day today = new day();
today.set_the_day(3); // "Sun":1,"Mond":2,"Tue":3,"Wend":4,"Thu":5,"Fri":6,"Sat":7   
today.print_the_day();
String s = today.get_the_day();
System.out.println("Today is " + s);
s= today.return_next_day();   
System.out.println("Next day will be " + s);   
s= today.return_previous_day();
System.out.println("Previous day was " + s);   
int n = 13;
s = today.get_day_after_n_days(n);   
System.out.println("After " + n + " days it will be " + s);   
}
}

Create day.java file and paste below code in it:

public class day
{
public String myarray[] = new String[7] ;
public int cur_day;

public day() {
this.myarray[0] = "Sun";
this.myarray[1] = "Mond";
this.myarray[2] = "Tue";
this.myarray[3] = "Wend";
   this.myarray[4] = "Thu";
   this.myarray[5] = "Fri";
   this.myarray[6] = "Sat";
this.cur_day = 0;
}

   public void set_the_day(int n)
   {
   this.cur_day = (n-1)%7;
   }

   public void print_the_day()
   {
System.out.println("Today is " + this.myarray[this.cur_day]);
   }

   public String get_the_day()
   {
   return this.myarray[this.cur_day] ;
   }

   public String return_next_day()
   {
   return this.myarray[(this.cur_day+1)%7];
   }

   public String return_previous_day()
   {
if(this.cur_day == 0)
{
   return "Sat";
}
else
{
   return this.myarray[(this.cur_day - 1)%7];
   }
   }

   public String get_day_after_n_days(int n)
   {
   return this.myarray[(this.cur_day + n)%7];
   }

}

Sample output:

Today is Tue
Today is Tue
Next day will be Wend
Previous day was Mond
After 13 days it will be Mond

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote