I need help, would you please use Java to write the code and run the output for
ID: 3704426 • Letter: I
Question
I need help, would you please use Java to write the code and run the output for me.
Objective:
Write a program that keeps track of a schedule for a day. Create the following.
A class ActivityInstance 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 DayScheduleInstance 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.
Explanation / Answer
Below is your code: -
Activity.java
// Activity class implementation
public class Activity {
// instance variables
private String name;
private int startHour;
private int endHour;
// default constructor implementation
public Activity() {
this("", 0, 0);
} // end of constructor
// parameterized constructor implementation
public Activity(String aName, int aStartHour, int aEndHour) {
setName(aName);
setStartHour(aStartHour);
setEndHour(aEndHour);
} // end of constructor
// getName method implementation
public String getName() {
return name;
} // end of getName method
// getStartHour method implementation
public int getStartHour() {
return startHour;
} // end of getStartHour method
// getEndHour method implementation
public int getEndHour() {
return endHour;
} // end of getEndHour method
// setName method implementation
public void setName(String aName) {
name = aName;
} // end of setName method
// setStartHour method implementation
public void setStartHour(int aStartHour) {
if (aStartHour >= 0 && aStartHour <= 23)
startHour = aStartHour;
else
startHour = -1;
} // end of setStartHour method
// setEndHour method implementation
public void setEndHour(int aEndHour) {
if (aEndHour >= 0 && aEndHour <= 23)
endHour = aEndHour;
else
endHour = -1;
} // end of setEndHour method
// toString method implementation
public String toString() {
return name + " " + startHour + " " + endHour;
} // end of toString method
} // end of Activity class
DaySchedule.java
// DaySchedule class implementation
public class DaySchedule {
// instance variables
private Activity[] activities;
private int count;
// default constructor implementation
public DaySchedule() {
activities = new Activity[20];
count = 0;
} // end of constructor
// getCurrentSchedule method implementation
public Activity[] getCurrentSchedule() {
return activities;
} // end of getCurrentSchedule method
// setActivities method implementation
public void setActivities(Activity[] aActivities) {
activities = aActivities;
} // end of setActivities method
// addActivity method implementation
public void addActivity(Activity activity) {
if (count == activities.length) {
System.out.println("Array of activities is full.");
return;
}
boolean conflict = false;
for (int i = 0; i < count; i++) {
if ((activity.getStartHour() >= activities[i].getStartHour()
&& activity.getStartHour() < activities[i].getEndHour())
|| (activity.getEndHour() > activities[i].getStartHour()
&& activity.getEndHour() <= activities[i].getEndHour())) {
conflict = true;
break;
}
}
if (conflict) {
System.out.println(
"The activity to be added conflicts with an existing activity. This activity will not be added.");
return;
}
activities[count] = activity;
count++;
sortActivities();
} // end of addActivity method
// sortActivities method implementation
private void sortActivities() {
for (int i = 0; i < count - 1; i++) {
int minPos = i;
for (int j = i + 1; j < count; j++) {
if (activities[j].getStartHour() < activities[minPos].getStartHour()) {
minPos = j;
}
}
if (i != minPos) {
Activity temp = activities[i];
activities[i] = activities[minPos];
activities[minPos] = temp;
}
}
} // end of sortActivities method
// removeActivity method implementation
public void removeActivity(String aName) {
boolean found = false;
for (int i = 0; i < count; i++) {
if (activities[i].getName().equalsIgnoreCase(aName)) {
for (int j = i; j < count - 1; j++) {
activities[j] = activities[j + 1];
}
count--;
found = true;
break;
}
}
if (!found) {
System.out.println(aName + " does not exist in the activities.");
}
} // end of removeActivity method
// printActivities method implementation
public void printActivities() {
for (int i = 0; i < count; i++) {
System.out.println(activities[i]);
}
} // end of printActivities method
} // end of DaySchedule class
DayScheduleFrontEnd.java
// DayScheduleFrontEnd class implementation
public class DayScheduleFrontEnd {
// start main method
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DaySchedule schedule = new DaySchedule();
Activity activity;
String aName;
int aStartHour;
int aEndHour;
int choice;
System.out.println("Welcome to the day planner system");
do {
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");
choice = Integer.parseInt(input.nextLine());
switch (choice) {
case 1:
System.out.println("Enter the activity’s name");
aName = input.nextLine();
System.out.println("Enter its start hour in military time");
aStartHour = Integer.parseInt(input.nextLine());
System.out.println("Enter its end hour in military time");
aEndHour = Integer.parseInt(input.nextLine());
activity = new Activity(aName, aStartHour, aEndHour);
schedule.addActivity(activity);
schedule.printActivities();
break;
case 2:
System.out.println("Enter the activity’s name");
aName = input.nextLine();
schedule.removeActivity(aName);
schedule.printActivities();
break;
case 9:
schedule.printActivities();
System.out.println("Good bye!");
break;
default:
System.out.println("Invalid choice!");
}
} while (choice != 9);
} // end of main method
} // end of DayScheduleFrontEnd class
Sample Run
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!