I need help, would you please use Java to write the code and run the output for
ID: 3704427 • 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
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Activity{
private String name;
private int startHour;
private int endHour;
Activity()
{
this.name="";
this.startHour=this.endHour=0;
}
Activity(String name,int startHour,int endHour)
{
this.name=name;
if(startHour>=0 && startHour<=23)
this.startHour=startHour;
else
{
System.out.println("Error!");
this.startHour=0;
}
if(endHour>=0 && endHour<=23 && endHour>=startHour)
this.endHour=endHour;
else
{
System.out.println("Error!");
this.endHour=0;
}
}
public void setName(String name)
{
this.name=name;
}
public void setStartHour(int startHour)
{
if(startHour>=0 && startHour<=23)
this.startHour=startHour;
else
System.out.println("Error!");
}
public void setEndHour(int endHour)
{
if(endHour>=0 && endHour<=23 && endHour>=this.startHour)
this.endHour=endHour;
else
System.out.println("Error!");
}
public String getName()
{
return this.name;
}
public int getStartHour()
{
return this.startHour;
}
public int getEndHour()
{
return this.endHour;
}
public String toString()
{
return "Name: "+this.name+" Start Hour: "+this.startHour+" End Hour: "+this.endHour;
}
}
class DaySchedule{
private Activity[] activities;
private int numberOfActivities;
private final int ACTIVITY_ARRAY_SIZE=100; //Can be modified
DaySchedule()
{
activities=new Activity[ACTIVITY_ARRAY_SIZE];
numberOfActivities=0;
}
public Activity[] getActivities()
{
return this.activities;
}
public void setActivities(Activity[] activities)
{
this.activities=activities;
}
public int getNumActivities()
{
return this.numberOfActivities;
}
public void addActivity(Activity activity)
{
if(numberOfActivities<ACTIVITY_ARRAY_SIZE)
{
boolean conflict=false;
for(int i=0;i<numberOfActivities;i++)
{
if(activities[i].getStartHour()==activity.getStartHour() && activities[i].getEndHour()==activity.getEndHour())
{
conflict=true;
break;
}
}
if(!conflict)
{
activities[numberOfActivities++]=activity;
for(int i=0;i<numberOfActivities-1;i++)
{
for(int j=1;j<numberOfActivities;j++)
{
if(activities[j].getStartHour()<activities[j-1].getStartHour())
{
Activity temp=activities[j];
activities[j]=activities[j-1];
activities[j-1]=temp;
}
}
}
}
}
}
public void removeActivity(String name)
{
boolean found=false;
for(int i=0;i<numberOfActivities;i++)
{
if(activities[i].getName().equals(name))
{
found=true;
for(int j=i;j<numberOfActivities-1;j++)
activities[j]=activities[j+1];
numberOfActivities--;
break;
}
}
if(!found)
{
System.out.println("No activity with this name found!");
}
}
public void printActivities()
{
for(int i=0;i<numberOfActivities;i++)
System.out.println(activities[i].toString());
}
}
class DayScheduleFrontEnd{
public static void main(String[] args)
{
DaySchedule daysch=new DaySchedule();
int choice;
Scanner in=new Scanner(System.in);
while(true){
System.out.println(daysch.getNumActivities()+" activities scheduled for the day! ");
System.out.println("Please choose among one of the options: ");
System.out.println("1. Add an Activity");
System.out.println("2. Remove an Activity");
System.out.println("3. Print All Activities");
System.out.println("4. Quit");
choice=in.nextInt();
if(choice==4)
break;
if(choice==1)
{
System.out.println("Please enter the name of activity");
String name;
name=in.next();
System.out.println("Please enter the start and end hours of activity");
int startHour,endHour;
startHour=in.nextInt();
endHour=in.nextInt();
daysch.addActivity(new Activity(name,startHour,endHour));
}
if(choice==2)
{
String name;
System.out.println("Please enter the name of the activity to be removed: ");
name=in.next();
daysch.removeActivity(name);
}
if(choice==3)
{
daysch.printActivities();
}
}
}
}