Question
Create the AllDayEvent class, a subclass of the Event class to help you store an AllDayEvent. This will keep the Event class functionalities, with one
Create the AllDayEvent class, a subclass of the Event class to help you store an AllDayEvent.
This will keep the Event class functionalities, with one exception:
The constructor will receive the following parameters:
date - String format yyyy-MM-dd is the date when the event occurs;
name - String representing the name of the event;
When we call method EventDuration returns 24.
When we call getStartDate method returns the start date of the event - at 00:00:00.
To solve this problem you can use any class in java.util and java.text
import java.text.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
class Event{
private Date startDate, endDate;
private String name;
public Event(String startDate, String endDate, String name) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
this.startDate= format.parse(startDate);
this.endDate= format.parse(endDate);
} catch (Exception e) {
System.out.println("Data wrong format");
System.out.println(e.getMessage());
}
this.name= name;
}
public Date getStartDate() {
return startDate;
}
public Date getEndDate() {
return endDate;
}
public String getName() {
return name;
}
// Return the hourly data of an event
public final long eventDuration() {
long differenceInMs = Math.abs(endDate.getTime() - startDate.getTime());
return TimeUnit.HOURS.convert(differenceInMs, TimeUnit.MILLISECONDS);
}
}
// Your class here...
public class prog {
public static void main(String[] args) throws Exception {
Event = new AllDayEvent("2019-04-22", "asd");
System.out.println(e.eventDuration()); // 24
}
}
Step by Step Solution
3.50 Rating (150 Votes )
There are 3 Steps involved in it
Step: 1
Heres the implementation of the AllDayEvent class which is a subclass of the Event class import java...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started