Assignment Goals: To learn to use class inheritance and/or composition to create
ID: 3813601 • Letter: A
Question
Assignment Goals: To learn to use class inheritance and/or composition to create new classes.
A. In Lab 4, you defined and implemented a class called Time. You will make a minor modification to the lab 4 program so that it meets the following specifications:
The class Time consists of three private member variables of type int: hour, minute, and second.
The class Time also includes the following public member functions:
1. print to print the hour, minute, and second in the format of HH-MM-SS. (Hint: consider using output manipulators setw and setfill).
2. setTime to accept three int arguments and use them to set the hour, minute and second member variables. A valid value for hour is between 0 and 23, inclusively. A valid value for minute and second is between 0 and 59. For any invalid values, use 0 instead.
3. getHour to return the value of hour.
4. getMinute to return the value of minute.
5. getSecond to return the value of minute.
6. An equalTime function that compares two Time objects. Return true if they are equal in hour, minute, and second, otherwise return false. equalTime has a formal parameter which is a Time object.
7. A constructor that accepts three int arguments and use them to initialize hour, minute and second. The three parameters use default value 0. Data validation should be considered. (Hints: Call the setTime member function with 3 arguments.)
8. A copy constructor that creates a Time object by initializing it with another Time object.
B. a class called Date that meets the following specifications:
The class Date consists of three private member variables: year of type int, month of type int, and day of type int.
The class Date also includes the following public member functions:
1. print to output the year, month, and day in the format of YYYY-MM-DD.
2. setDate to set the year, month and day according to the parameters.
Data validation should be provided. 1) A valid year should be between 1900 and 2017 inclusively, otherwise using 2001 for the year. 2) A valid month should be between 1 and 12 inclusively, otherwise using 1 for the month. 3) A valid day should be between 1 and 28 when the month is 2, i.e., simply assume there are 28 days in February. 1 and 31 when the month is 1, 3, 5, 7, 8, 10, or 12, which means there are 31 days in January, March, May, July, August, October, and December . 1 and 30 when the month is 4, 6, 9, or 11 for April, June, September, and November. Use 1 for any invalid value.
3. getYear to return the year.
4. getMonth to return the month.
5. getDay to return the day.
6. equalDate to compare two Date objects. Return true if they are the same, otherwise, return false.
7. A constructor to initialize year, month, and day with default parameters. The default values for the year, month, and day are 2001, 1, and 1, respectively.
8. A copy constructor to initialize year, month, and day with another Date object.
C. Define a new class called Event using class composition.
The class Event consists of four private member variables: string name string location Date date Time time
The class Event should also include the following public member functions:
1. print to print the even name, location, date, and time information.
2. setEvent to set the event name, location, date, and time using two string parameters, one Date parameter, and one Time parameter.
3. setEventName to set the event name according to the parameter.
4. getEventName to return the event name.
5. setLocation to set the event location according to the parameter.
6. getLocation to return the event location.
7. setDate to set the event date using a Date object.
8. getDate to return the date, a Date object.
9. setTime to set the event time using a Time object.
10. getTime to return the time, a Time object.
11. equalEventDate to compare two event’s dates. Return true if they are the same, otherwise, return false.
12. A default constructor to initialize name and location to “None” and invoke the default constructor of Date and Time.
13. An overloaded constructor that initializes name, location, date, and time according to the parameters (two string parameters, one Date parameter, and one Time parameter).
D.In the client program, you should write statements to test your class implementation including the following functions in the Event class: default constructor, overloaded constructor, mutator functions, accessor functions, and equalEventDate function. You can use the following sample data for the events, as well as other data at your preference.
Explanation / Answer
you did not provide any sample date. So, I have given my own data in Main.java(client program) to test those implemenations.
If my answer satisfies your need, please thumb up to my answer.Thankyou!!!
Main.java
class Main
{
public static void main(String a[])
{
//checking default constructor of Event class
Event newEvent = new Event();
newEvent.print();
System.out.println();
//checking overloaded constructor of Event class
Event event = new Event();
Date date = new Date();
date.setDate(1992,3,4);
Time time = new Time(12,4,4);
event = new Event("Movie Release","America",date,time);
event.print();
System.out.println();
//checking mutator functions of Event class
event.setEventName("Bahubali Release");
event.setEventLocation("India");
date = new Date();
date.setDate(2017,4,28);
event.setEventDate(date);
time = new Time();
time.setTime(5,0,0);
event.setEventTime(time);
event.print();
System.out.println();
//checking accessor functions of Event class
System.out.println("Event Name:"+event.getEventName());
System.out.println("Event Location:"+event.getEventLocation());
System.out.print("Event Date:");
event.getEventDate().print();
System.out.print("Event Time:");
event.getEventTime().print();
System.out.println();
//checking equalEventDate function of Event class
System.out.println("are those two event dates same?"+event.equalEventDate(newEvent));
System.out.println();
}
}
Event.java
class Event
{
private String name,location;
Date date;
Time time;
//Initializes with their default values(i.e default constructor)
public Event()
{
this.name = "None";
this.location = "None";
date = new Date();
time = new Time();
}
//initializes with passed parameters
public Event(String name,String location,Date date,Time time)
{
this.name = name;
this.location = location;
this.date = date;
this.time = time;
}
//prints Event's data
public void print()
{
System.out.println("Name:"+this.name+" Location:"+this.location);
System.out.print("Date:");
date.print();
System.out.print("Time:");
time.print();
}
//sets Event class data with passed parameters
public void setEvent(String name,String location,Date date,Time time)
{
this.name = name;
this.location = location;
this.date = date;
this.time = time;
}
//setters
public void setEventName(String name)
{
this.name = name;
}
public void setEventLocation(String location)
{
this.location = location;
}
public void setEventDate(Date date)
{
this.date = date;
}
public void setEventTime(Time time)
{
this.time = time;
}
//getters
public String getEventName()
{
return this.name;
}
public String getEventLocation()
{
return this.location;
}
public Date getEventDate()
{
return this.date;
}
public Time getEventTime()
{
return this.time;
}
//equalEventDate implementation
public boolean equalEventDate(Event event)
{
if(event.date.equalDate(this.date))
return true;
else return false;
}
}
Date.java
class Date
{
private int year,month,day;
//creates Date object with default values of year,month,day
public Date()
{
this.year = 2001;
this.month = 1;
this.day = 1;
}
//initializes this object parameters with newDate object's data that is passed
public Date(Date newDate)
{
this.year = newDate.year;
this.month = newDate.month;
this.day = newDate.day;
}
//prints data of this class
public void print()
{
System.out.println(this.year+"-"+this.month+"-"+this.day);
}
//setting parameters of class
public void setDate(int year,int month,int day)
{
if(year>=1900 && year<=2017)
this.year = year;
else this.year = 2001;
if(month>=1 && month<=12)
this.month = month;
else this.month = 1;
if(this.month==2 && day>=1 && day<=28)
this.day = day;
else if((this.month==1 || this.month==3 || this.month==5 || this.month==7 || this.month==8 || this.month==10 || this.month==12) && day>=1 && day<=31)
this.day = day;
else if(day>=1 && day<=30)
this.day = day;
else this.day = 1;
}
//getters
public int getYear()
{
return this.year;
}
public int getMonth()
{
return this.month;
}
public int getDay()
{
return this.day;
}
//equalDate implementation
public boolean equalDate(Date newDate)
{
if(newDate.year == this.year && newDate.month == this.month && newDate.day == this.day)
return true;
else return false;
}
}
Time.java
class Time
{
private int hour,minute,second;
//creates Time object with default values of hour,minute,second that are passed
public Time(int hour,int minute,int second)
{
setTime(hour,minute,second);
}
//creates Time object with default values of hour,minute,second
public Time()
{
this.hour = 0;
this.minute = 0;
this.second = 0;
}
//initializes this object parameters with newTime's data object that is passed
public Time(Time newTime)
{
this.hour = newTime.hour;
this.minute = newTime.minute;
this.second = newTime.second;
}
//prints data of this class
public void print()
{
System.out.println(this.hour+"-"+this.minute+"-"+this.second);
}
//setting parameters of class
public void setTime(int hour,int minute,int second)
{
if(hour>=0 && hour<=23)
this.hour = hour;
else this.hour = 0;
if(minute>=0 && minute<=59)
this.minute = minute;
else this.minute = 0;
if(second>=0 && second<=23)
this.second = second;
else this.second = 0;
}
//getters
public int getHour()
{
return this.hour;
}
public int getMinute()
{
return this.minute;
}
public int getSecond()
{
return this.second;
}
//equalTime function implementation
public boolean equalTime(Time newTime)
{
if(newTime.hour == this.hour && newTime.minute == this.minute && newTime.second == this.second)
return true;
else return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.