Java programming 5th edition chapter 8 User-Defined Classes and ADTs using Strin
ID: 3783523 • Letter: J
Question
Java programming 5th edition chapter 8 User-Defined Classes and ADTs using Strings
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:
a. Set the day.
b. Print the day.
c. Return the day.
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
// Day.java
import java.util.*;
//util basically for all basic data type opeerations performing
import javax.swing.*;
//for gui incase of calculating the number of days
import java.awt.*;
//creating a class Day . So that we can use this blue print to create any type of
//this day object in future and use. and this blue print is used to instantiate new instances of this
//class is basically a blue print of what we want to produce
//so for every object we need to create only one blue print and we can use any number of objects of this type
//its like hving one car model designed initially by BMW and all real cars or you can say instances are made of this
//so this is a blue print . So whenever you want use it you have to use it as an object similar to real car
//class is just a blue print .
class Day
{
//we are creating two basic data types to hold in day class . one is day number of int type
//another days .i.e. a day name as a string type in java
public static int day;
public static String days;
//public constructor 1.a class may have any number of constructors but minimum one is needed . If you dont write one for a class
//java it self creates a constructor by itself a default one .
//cosntructor is always called when you create an instance of this blue print or class.
//its like when you make a car out of a model all the basic thngs are made out of it and extra things
//are added later by buyers requirement right.
//so if you dont have any basic type means car comapany sellers makes a basic one. So atleast one should be there to
//make it as an instance . So every time you create an object constructor is called.
//constructor name is same as the class name and should be public as it needs to be called from outside
//It depends on the cosntructor we are calling based on parameters passed it gets identified.
public Day()
{
//this default constructor sets day to 1 using setDay method inside the day class.
//its like you dont specify colour of car . so company it self sets default some colour
setDay(1);
}
//another constructor which sets a given day when you are creating instance of this class.i.e. object of this class
//its like you specify which colour you want and pass it as an argument.
public Day (int day)
{
//since day must be between 1 to 7 checking whether its between 1 to 7 or not
//if there setting it as it is .
if( (day >0) &&(day <8))
{
this.day=day;
setDay(day);
}
//if day is more than 7 then converting it into 1 to 7 using modulus % operator.
//this operator % gives the remainder of the division.
//so when you divide by 7 it gives value between 1 to 7
else
{
//if remainder is 0 it means its divisible by 7 so its 7th day
day =(day%7);
if(day ==0)
day =7;
//or else set the remainder day to day in day class to the object we created.
this.day =day;
//calling setDay method to set the days in class of object created.
setDay(day);
}
}
//method to print the day name in string format or readable output
//returns a string so that we can use it to print
public String toString()
{
return (days);
}
//method to setDay of class Day from outside of this class since its public
//you can change set day by using this method from outside using this method as its public
public void setDay(int daycal)
{
//setting apropriate days based on the day number
//if 1 setting it to sunday
if (daycal == 1)
days = "Sunday";
//if 2 setting it to monday
if (daycal ==2)
days = "Monday";
//if 3 setting it to tuesday
if (daycal ==3)
days = "Tueday";
//if 4 setting it to wednusday
if (daycal ==4)
days = "Wedday";
//if 5 setting it to thursday
if (daycal ==5)
days = "Thurday";
//if 6 setting it to friday
if (daycal ==6)
days = "Friday";
//if 7 setting it to saturday
if (daycal ==7)
days = "Satday";
}
//public setter for days in day class
//this method sets the days name in day object created.
//since its puclic is accessible from outside
//takes string as input and sets it as days .and returns a day object.
public Day setNameDay(String day)
{
days = day;
return this;
}
//this method prints out the day set.
//checks whether day is less than 8 or not then only prints or else it wont
//but we made sure that days get changed to in between 1 to 7
//its public method so you can call from outside
public void printDay()
{
//checking whether day is in between 1 to 7 or not
if (day <8)
//printing statement
System.out.print(days);
}
//method to give the next day following this day
//public method can be called from anywhere
public void nextDay()
{
//creating a temporary variable to store current day
int daynext =day;
//incrementing the temporary variable to next day using increment operator
daynext++;
//and checking if its less than 8 then directly printing out the values or
if (daynext <8)
//setting the the current day to next day using setday method of day class
setDay(daynext);
//if nextday is out of bounds then bringing it back into the 1 to 7 bounds so as to get correct day
else
{
//setting the next day based on conversion i.e. day - 7 gives the exact day and setting day using setDay method
setDay(daynext-7);
}
}
//method to set current day to previous day
//public method so you can call it from any class
public void previousDay()
{
//creating a temporary storage for storing current day
//and decreaseing the current day
int dayprev=day;
dayprev--;
//checkin gif it falls beyond the range and adjusting it into range so that correct day gets printed out
if(dayprev <1)
{
dayprev = 7;
}
//setting the day of current day clas to previous day
setDay(dayprev);
}
//method to calculate the day if given the number of days after which you want to calculate
public void calculateDay()
{
//initialializing 3 temporary variables for local use
int calc = 0;
String str;
int dayAdd =0;
//creating a joption pane for taking input from user
str =JOptionPane.showInputDialog("Enter number of days to add: ");
///parsing the input from dialog box to our temporary variable
//parsing from string to int
calc =Integer.parseInt(str);
//addign the number of day entered by the user to the day stored in the class
dayAdd = day +calc;
//calculating the resulting day using the modulus operator
dayAdd = dayAdd %7;
//making day to 7 if it evenly divided by 7
if(dayAdd ==0)
dayAdd = 7;
//setting day of the day class using setDay method
setDay(dayAdd);
//printing the current day using the writeen public mehod printDay
printDay();
}
}
// TestProgDay.java
import java.util.Scanner;
//class name TestProDay . file name should be same as the class name
public class TestProgDay
{
//creating a scanner object to take input through standard input
static Scanner console = new Scanner(System.in);
//starting of main for our program .
//arguments are **args
public static void main(String[] args)
{
//creating an object of day class with day initialized to 2
//using the second constructor of day class
Day myDay = new Day(2);
System.out.print("The day of the week is ");
//using printDay method to printing out the days name
myDay.printDay();
//just a new line
System.out.println();
//printing next day
System.out.print("The next day is ");
//printing next day using public method nextDay method of Day class
myDay.nextDay();
//printing out the present day of the day object created after modifications
//printDay method public and called from this to print out
myDay.printDay();
System.out.println();
System.out.print("The previous day is ");
//making it to previous day using public method previousDay
myDay.previousDay();
//printing out the current day stored in day class
myDay.printDay();
System.out.println();
//taking input from user and calculating the resulatant day and printing out
//using printDay method used in calculateDay method of day clas
myDay.calculateDay();
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.