In Java: Write a program that keeps track of a schedule for a day. Create the fo
ID: 3764092 • Letter: I
Question
In Java: Write a program that keeps track of a schedule for a day. Create the following. A class Activity Instance Variables Name Start Hour (Assumed to be in military time 0-23 hours) End Hour (Assumed to be in military time 0-23 hours) Constructors Default Parameterized with error checking Accessors and Mutators for each instance variable. DON’T FORGET TO CHECK FOR VALID VALUES Other methods toString: takes in the value and returns a string with the name, start hour, and end hour A class DaySchedule Instance Variables Activities: an array of Activity that represents the current schedule. Constructors Default Accessors and Mutators for the instance variable Other methods addActivity: This method takes in an instance of an activity and attempts to add it to the current array of activities. It only adds this activity if the array is not full, and this activity doesn’t conflict with another one. An activity conflicts if the start and the end of one activity cannot fall at the same time as another. If this were to occur the user should be notified and the activity is not added. Also this method should finally sort the activities based on their starting time. removeActivity: This method takes in a string that represents the name of an activity and then searches for that activity in the array. If it is found then that activity is removed, but if it’s not found then the user is prompted that it does not exist. printActivities: This method simply prints out the activities including their name and their times. A class DayScheduleFrontEnd This class contains the main method which has an instance of DaySchedule. From there it prompts the user for whether or not they want to add a new activity, remove an activity, and quit. Also it should display the current list of activities in that instance. See the example dialog below for further guidance. Example Dialog: Welcome to the day planner system Enter 1 to add an activity Enter 2 to remove an activity Enter 9 to quit 1 Enter the activity's name class Enter its start hour in military time 13 Enter its end hour in military time 14 class 13 14 Enter 1 to add an activity Enter 2 to remove an activity Enter 9 to quit 1 Enter the activity's name breakfast Enter its start hour in military time 8 Enter its end hour in military time 9 breakfast 8 9 class 13 14 Enter 1 to add an activity Enter 2 to remove an activity Enter 9 to quit 1 Enter the activity's name lab exam Enter its start hour in military time 15 Enter its end hour in military time 18 breakfast 8 9 class 13 14 lab exam 15 18 Enter 1 to add an activity Enter 2 to remove an activity Enter 9 to quit 1 Enter the activity's name breakfast part 2 Enter its start hour in military time 8 Enter its end hour in military time 10 The activity to be added conflicts with an existing activity. This activity will not be added. breakfast 8 9 class 13 14 lab exam 15 18 Enter 1 to add an activity Enter 2 to remove an activity Enter 9 to quit 2 Enter the activity's name breakfast class 13 14 lab exam 15 18 Enter 1 to add an activity Enter 2 to remove an activity Enter 9 to quit 9 class 13 14 lab exam 15 18 Good bye!
Explanation / Answer
Activity class:
public class Activity
{
private String name;
private int startHour;
private int endHour;
public Activity()
{
this("",0,0);
}
public Activity(String name,int startHour,int endHour)
{
this.setName(name);
this.setStartHour(startHour);
this.setEndHour(endHour);
}
public String getName() {
return name;
}
public int getStartHour() {
return startHour;
}
public int getEndHour() {
return endHour;
}
public void setName(String name) {
this.name = name;
}
public void setStartHour(int startHour) {
if(startHour >=0 && startHour <= 24)
this.startHour = startHour;
else this.startHour = -1;
}
public void setEndHour(int endHour) {
if(endHour >=0 && endHour <= 24)
this.endHour = endHour;
else this.endHour = -1;
}
public String toString()
{
return this.name+" "+this.startHour+" "+this.endHour;
}
}
//###########################################################################
DaySchedule class:
public class DaySchedule
{
private Activity[] currentSchedule;
private int scheduleLength;
public DaySchedule()
{
this.currentSchedule = new Activity[20];
this.scheduleLength = 0;
}
public Activity[] getCurrentSchedule() {
return currentSchedule;
}
public void setCurrentSchedule(Activity[] currentSchedule) {
this.currentSchedule = currentSchedule;
}
public void addActivity(Activity activity)
{
//checking for full
if(this.scheduleLength < 20)
{
//check for conflict
boolean hasConflict = false;
for(int i=0;i<this.scheduleLength;i++)
{
if((activity.getStartHour() >= this.currentSchedule[i].getStartHour() && activity.getStartHour() <= this.currentSchedule[i].getEndHour()) || (activity.getEndHour() >= this.currentSchedule[i].getStartHour() && activity.getEndHour() <= this.currentSchedule[i].getEndHour()))
{
hasConflict = true;
break;
}
}
if(!hasConflict)
{
currentSchedule[this.scheduleLength] = activity;
this.scheduleLength += 1;
//sort
sortActivities();
}
else
System.out.println("The activity to be added conflicts with an existing activity. This activity will not be added.");
}
else
System.out.println("array full!");
}
public void removeActivity(String activityName)
{
boolean found = false;
for(int i=0;i<this.scheduleLength;i++)
{
if(this.currentSchedule[i].getName().equals(activityName))
{
this.currentSchedule[i] = this.currentSchedule[this.scheduleLength-1];
this.scheduleLength--;
found = true;
sortActivities();
break;
}
}
if(!found)
{
System.out.println("No activity found!");
}
}
private void sortActivities()
{
for(int i=0;i<this.scheduleLength;i++)
{
for(int j=i;j<this.scheduleLength;j++)
{
if(this.currentSchedule[i].getStartHour() > this.currentSchedule[j].getStartHour())
{
Activity temp = this.currentSchedule[i];
this.currentSchedule[i] = this.currentSchedule[j];
this.currentSchedule[j] = temp;
}
}
}
}
public void printActivities()
{
for(int i=0;i<this.scheduleLength;i++)
{
System.out.println(this.currentSchedule[i]);
}
}
}
//################################################################################
DayScheduleFrontEnd class:
import java.util.*;
public class DayScheduleFrontEnd
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
DaySchedule daySchedule = new DaySchedule();
System.out.println("Welcome to the day planner system");
int option = -1;
while(option != 9)
{
printMenu();
option = Integer.parseInt(sc.nextLine());
if(option == 1)
{
System.out.println("Enter the activity's name: ");
String name = sc.nextLine();
System.out.println("Enter its start hour in military time: ");
int startHour = Integer.parseInt(sc.nextLine());
System.out.println("Enter its end hour in military time: ");
int endHour = Integer.parseInt(sc.nextLine());
Activity activity = new Activity(name,startHour,endHour);
if(activity.getStartHour() != -1 && activity.getEndHour() != -1)
{
daySchedule.addActivity(activity);
}
}
else if(option == 2)
{
System.out.println("Enter the activity's name: ");
String name = sc.nextLine();
daySchedule.removeActivity(name);
}
daySchedule.printActivities();
}
System.out.println("Good bye!");
}
public static void printMenu()
{
System.out.println("Enter 1 to add an activity");
System.out.println("Enter 2 to remove an activity");
System.out.println("Enter 9 to quit");
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.