Question

In: Computer Science

A state diagram shows the states of an object and the transitions between states •States are...

A state diagram shows the states of an object and the transitions between states

•States are particularly common with objects that interact with the program user. •Suppose that a user wants to retrieve recent voice mail messages.

•Some part of the voice mail system must keep track of the current state so that it can process the user input correctly

Java. I need a state diagram for the following code:

What the code Does: adds a new regular task, delete a task , show all tasks, and show regular tasks.

my code:

import java.util.ArrayList;
import java.util.Scanner;

class ToDoList {
    private ArrayList<Task> list;//make private array

    public ToDoList() {
//this keyword refers to the current object in a method or constructor
        this.list = new ArrayList<>();
    }

    public Task[] getSortedList() {
        Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array
        //fills array with given values by using a for loop
        for (int i = 0; i < this.list.size(); i++) {
            sortedList[i] = this.list.get(i);
        }
        //sorting values in array using bubble sort
        Task temp;//temp var to hold sorted values
        for (int i = 0; i < sortedList.length; i++) {
            for (int j = 1; j < (sortedList.length - i); j++) {
                if (sortedList[j].isEarlier(sortedList[j - 1])) {
                    temp = sortedList[j - 1];
                    sortedList[j - 1] = sortedList[j];
                    sortedList[j] = temp;
                }
            }
        }
        return sortedList;
    }//end of getSortedList
    public void addTask(Task task) {
        this.list.add(task);//.add to add elements in the list
    }
    public void deleteTask(int taskID) {
        //delete tasks using task ID
        for (int i = 0; i < this.list.size(); i++) {
            if (this.list.get(i).getTaskID() == taskID) {
                this.list.remove(i);
            }
        }
    }
    public void deleteAllTasks() {
        this.list.clear();
    }
    public int getTaskNumber() {
        return this.list.size();
    }
    public void printList(Task[] tasks) {
        System.out.println("-------------------------");
        for (Task task : tasks) {
            task.printTask();
        }
        System.out.println("-------------------------");
    }
    public static void displayMenu() {
        System.out.println("1)Add tasks");
        System.out.println("2)List tasks");
        System.out.println("3)Delete Tasks");
        System.out.println("4)Delete all tasks");
        System.out.println("5)Exit");
      }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ToDoList toDoList = new ToDoList();
       boolean run = true;

        while (run) {//will continue to loop until user chooses a valid option
             displayMenu();
            int choice = in.nextInt();
            switch (choice) {
                //nextLine() method advances this scanner past the current line and returns the input that was skipped
                case 1:
                    in.nextLine();
                    System.out.print("Task Name: ");
                    String taskTitle = in.nextLine();
                    System.out.print("Task Deadline (dd mm yyyy): ");
                    int taskDay = in.nextInt();
                    int taskMonth = in.nextInt();
                    int taskYear = in.nextInt();
                    in.nextLine();
                    System.out.print("Time Deadline (hh mm): ");
                    int taskHour = in.nextInt();
                    int taskMin = in.nextInt();
                    in.nextLine();
                    System.out.print("Is this task important? (yes/no): ");
                    String taskImportant = in.nextLine();
                    //Task ID made with all info set up neatly
                    Task newTask = new Task(new Time(taskHour, taskMin), new Date(taskDay, taskMonth, taskYear), taskTitle, taskImportant, toDoList.getTaskNumber() + 1);
                    //Adds the new task to the list
                    toDoList.addTask(newTask);
                    //Display the sorted tasks
                    System.out.printf("Task %d is added. The To-Do list is as follows:\n", newTask.getTaskID());
                    toDoList.printList(toDoList.getSortedList());
                    break;
                case 2:
                    //List all tasks
                    toDoList.printList(toDoList.getSortedList());
                    break;
                case 3:
                    //Delete a task
                    toDoList.printList(toDoList.getSortedList());
                    in.nextLine();
                    System.out.print("Enter ID number to delete task: ");
                    int taskID = in.nextInt();
                    toDoList.deleteTask(taskID);
                    System.out.printf("\nTask %d is deleted. The new list is:\n", taskID);
                    toDoList.printList(toDoList.getSortedList());
                    break;
                case 4:
                    //Delete all tasks
                    toDoList.deleteAllTasks();
                    System.out.println("Tasks Deleted");
                    break;
                case 5:
                    //Exit program
                    System.out.println("Exiting program, goodbye.");
                    run = false;
                    break;
            }
        }
    }

}


class Date {

    private int day;
    private int month;
    private int year;
    //Default constructor for date,
    public Date() {
        this(0, 0, 0);
    }

    public Date(int day, int month, int year) {
        this.day = day;
        this.month = month;
        this.year = year;
    }
    //accessors and mutators
    public int getDay() {
        return this.day;
    }

    public int getMonth() {
        return this.month;
    }

    public int getYear() {
        return this.year;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public void setYear(int year) {
        this.year = year;
    }

        public String toString() {
        return String.format("%d/%d/%d", this.day, this.month, this.year);

    }
    //Returns whether this date is earlier than another date provided as parameter. If they are the same, returns false
    public boolean isEarlier(Date date) {
        //If the years are the same, narrow the comparison down the months
        if (this.year == date.year) {
            //Repeat the same thing as years, but narrowed down for months
            if (this.month == date.month) {
                if (this.day == date.day) {
                    //Returns false because if they are the same date, then one is not earlier than the other
                    return false;
                } else if (this.day < date.day) {
                    return true;
                } else {
                    return false;
                }
            } else if (this.month < date.month) {
                return true;
            } else {
                return false;
            }
                    } else if (this.year < date.year) {
            return true;
                    } else {
            return false;
        }
    }
}
class Time {

    private int hour;
    private int min;
    //Default constructor for time
    public Time() {
        this(0, 0);
    }

    public Time(int hour, int min) {
        this.hour = hour;
        this.min = min;
    }
    //Accessors and mutator methods for hour and min attributes
    public int getHour() {
        return this.hour;
    }

    public int getMin() {
        return this.min;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public void setMin(int min) {
        this.min = min;
    }

    //Returns time in HH:MM format
    public String toString() {
        return String.format("%d:%d", this.hour, this.min);
    }
    //Returns whether or not this time is earlier than the time provided as a parameter. Returns false if both same
    public boolean isEarlier(Time time) {
        //If they are the same hour, narrow the comparison down to the minute
        if (this.hour == time.hour) {
            //Exact same process, but for min
            if (this.min == time.min) {
                //Returns false because if they're the same time, one is not earlier than the other
                return false;
            } else if (this.min < time.min) {
                return true;
            } else {
                return false;
            }
            //If this hour is less than the other hour, it is earlier, so return true
        } else if (this.hour < time.hour) {
            return true;
            //Otherwise, the time isn't earlier
        } else {
            return false;
        }
    }

}
class Task {
    private Time time;
    private Date date;
    private String title;
    private String Important;
    private int taskID;
    //Default constructor leaves attributes blank
    public Task() {
        this(null, null, null, null, 0);
    }
    public Task(Time time, Date date, String title, String Important, int taskID) {
        this.time = time;
        this.date = date;
        this.title = title;
        this.Important = Important;
        this.taskID = taskID;
    }
    //accessors and mutators for time, date, title, Important, and taskID
    public Time getTime() {
        return this.time;
    }
    public Date getDate() {
        return this.date;
    }
    public String getTitle() {
        return this.title;
    }
    public String getImportant() {
        return this.Important;
    }
    public int getTaskID() {
        return this.taskID;
    }
    public void setTime(Time time) {
        this.time = time;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public void setImportant(String Important) {
        this.Important = Important;
    }
    //Prints task date
    public void printTask() {
        System.out.printf("Task %d:  \n", this.taskID, this.title, this.date.toString(), this.time.toString(), this.Important);
    }
    //Returns true if this task is earlier than task provided as parameter.
    public boolean isEarlier(Task task) {
             if (this.date.isEarlier(task.getDate())) {
            return true;
              } else if (!this.date.isEarlier(task.getDate()) && !task.getDate().isEarlier(this.date)) {
            return this.time.isEarlier(task.time);
        }
        return false;
    }
}

Solutions

Expert Solution

ToDoList.java

import java.util.ArrayList;
import java.util.Scanner;

public class ToDoList {
   private ArrayList<Task> list;//make private array

   public ToDoList() {
       //this keyword refers to the current object in a method or constructor
       this.list = new ArrayList<>();
   }

   public Task[] getSortedList() {
       Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array
       //fills array with given values by using a for loop
       for (int i = 0; i < this.list.size(); i++) {
           sortedList[i] = this.list.get(i);
       }
       //sorting values in array using bubble sort
       Task temp;//temp var to hold sorted values
       for (int i = 0; i < sortedList.length; i++) {
           for (int j = 1; j < (sortedList.length - i); j++) {
               if (sortedList[j].isEarlier(sortedList[j - 1])) {
                   temp = sortedList[j - 1];
                   sortedList[j - 1] = sortedList[j];
                   sortedList[j] = temp;
               }
           }
       }
       return sortedList;
   }//end of getSortedList
   public void addTask(Task task) {
       this.list.add(task);//.add to add elements in the list
   }
   public void deleteTask(int taskID) {
       //delete tasks using task ID
       for (int i = 0; i < this.list.size(); i++) {
           if (this.list.get(i).getTaskID() == taskID) {
               this.list.remove(i);
           }
       }
   }
   public Task getTaskById(int taskId) {
       // delete tasks using task ID
       for (int i = 0; i < this.list.size(); i++) {
           if (this.list.get(i).getTaskID() == taskId) {
               return this.list.get(i);
           }
       }
       return null;
   }
   public void setTaskImportant(int taskId) {
       for (int i = 0; i < this.list.size(); i++) {
           if (this.list.get(i).getTaskID() == taskId) {
               this.list.get(i).setImportant("yes");
           }
       }

   }

   private void setTaskCompleted(int taskId) {
       for (int i = 0; i < this.list.size(); i++) {
           if (this.list.get(i).getTaskID() == taskId) {
               this.list.get(i).setTaskCompleted(true);
           }
       }

   }


   public void deleteAllTasks() {
       this.list.clear();
   }
   public int getTaskNumber() {
       return this.list.size();
   }
   public void printList(Task[] tasks) {
       System.out.println("-------------------------");
       for (Task task : tasks) {
           task.printTask();
       }
       System.out.println("-------------------------");
   }
   public static void displayMenu() {
       System.out.println("1)Add tasks");
       System.out.println("2)List tasks");
       System.out.println("3)Delete Tasks");
       System.out.println("4)Delete all tasks");
       System.out.println("5)Mark task as important");
       System.out.println("6)Mark task as completed");
       System.out.println("7)Show completed tasks");
       System.out.println("8)Show important tasks");
       System.out.println("9)Exit");
   }

   public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
       ToDoList toDoList = new ToDoList();
       boolean run = true;

       while (run) {// will continue to loop until user chooses a valid option
           displayMenu();
           int choice = in.nextInt();
           switch (choice) {
           // nextLine() method advances this scanner past the current line
           // and returns the input that was skipped
           case 1 :
               in.nextLine();
               System.out.print("Task Name: ");
               String taskTitle = in.nextLine();
               System.out.print("Task Deadline (dd mm yyyy): ");
               int taskDay = in.nextInt();
               int taskMonth = in.nextInt();
               int taskYear = in.nextInt();
               in.nextLine();
               System.out.print("Time Deadline (hh mm): ");
               int taskHour = in.nextInt();
               int taskMin = in.nextInt();
               in.nextLine();
               System.out.print("Is this task important? (yes/no): ");
               String taskImportant = in.nextLine();
               // Task ID made with all info set up neatly
               Task newTask = new Task(new Time(taskHour, taskMin),
                       new Date(taskDay, taskMonth, taskYear), taskTitle,
                       taskImportant, toDoList.getTaskNumber() + 1);
               // Adds the new task to the list
               toDoList.addTask(newTask);
               // Display the sorted tasks
               System.out.printf(
                       "Task %d is added. The To-Do list is as follows:\n",
                       newTask.getTaskID());
               toDoList.printList(toDoList.getSortedList());
               break;
           case 2 :
               // List all tasks
               toDoList.printList(toDoList.getSortedList());
               break;
           case 3 :
               // Delete a task
               toDoList.printList(toDoList.getSortedList());
               in.nextLine();
               System.out.print("Enter ID number to delete task: ");
               int taskID = in.nextInt();
               toDoList.deleteTask(taskID);
               System.out.printf(
                       "\nTask %d is deleted. The new list is:\n", taskID);
               toDoList.printList(toDoList.getSortedList());
               break;
           case 4 :
               // Delete all tasks
               toDoList.deleteAllTasks();
               System.out.println("Tasks Deleted");
               break;
           case 5 :
               toDoList.printList(toDoList.getSortedList());
               System.out.print(
                       "Enter ID number to mark task as important: ");
               int taskId = in.nextInt();
               Task task = toDoList.getTaskById(taskId);
               if (task == null) {
                   System.out.println("Entered task not found");
                   break;
               }
               toDoList.setTaskImportant(taskId);
               System.out.printf("Task %d marked important:\n", taskId);
               break;
           case 6 :
               toDoList.printList(toDoList.getSortedList());
               System.out.print(
                       "Enter ID number to mark task as completed: ");
               taskId = in.nextInt();
               task = toDoList.getTaskById(taskId);
               if (task == null) {
                   System.out.println("Entered task not found");
                   break;
               }
               toDoList.setTaskCompleted(taskId);
               System.out.printf("Task %d marked completed:\n", taskId);
               break;
           case 7 :
               boolean flagCompleted = false;
               System.out.println("Completed tasks:");
               for (Task completedTask : toDoList.getSortedList()) {
                   if (completedTask.isTaskCompleted()) {
                       completedTask.printTask();
                       flagCompleted = true;
                   }
               }
               if (!flagCompleted) {
                   System.out.println("No completed tasks found");
               }
               break;
           case 8 :
               boolean flagImportant = false;
               System.out.println("Important tasks:");
               for (Task importantTask : toDoList.getSortedList()) {
                   if (importantTask.getImportant()
                           .equalsIgnoreCase("yes")) {
                       importantTask.printTask();
                       flagImportant = true;
                   }
               }
               if (!flagImportant) {
                   System.out.println("No completed tasks found");
               }
               break;
           case 9 :
               // Exit program
               System.out.println("Exiting program, goodbye.");
               run = false;
               break;
           }
       }
       in.close();
   }


}

Date.java

public class Date {

   private int day;
   private int month;
   private int year;
   //Default constructor for date,
   public Date() {
       this(0, 0, 0);
   }
   //Default constructor for date with parameters
   public Date(int day, int month, int year) {
       this.day = day;
       this.month = month;
       this.year = year;
   }
   //setter and getter methods
   public int getDay() {
       return this.day;
   }

   public int getMonth() {
       return this.month;
   }

   public int getYear() {
       return this.year;
   }

   public void setDay(int day) {
       this.day = day;
   }

   public void setMonth(int month) {
       this.month = month;
   }

   public void setYear(int year) {
       this.year = year;
   }

   // when Object called toString will be executed.
   public String toString() {
       return String.format("%d/%d/%d", this.day, this.month, this.year);

   }
   //Returns whether this date is earlier than another date provided as parameter. If they are the same, returns false
   public boolean isEarlier(Date date) {
       //If the years are the same, narrow the comparison down the months
       if (this.year == date.year) {
           //Repeat the same thing as years, but narrowed down for months
           if (this.month == date.month) {
               if (this.day == date.day) {
                   //Returns false because if they are the same date, then one is not earlier than the other
                   return false;
               } else if (this.day < date.day) {
                   return true;
               } else {
                   return false;
               }
           } else if (this.month < date.month) {
               return true;
           } else {
               return false;
           }
       } else if (this.year < date.year) {
           return true;
       } else {
           return false;
       }
   }
}

Time.java

public class Time {

private int hour;
private int min;
//Default constructor for time
public Time() {
this(0, 0);
}
//Defult constructor for Time with parameters
public Time(int hour, int min) {
this.hour = hour;
this.min = min;
}
//Getter and Setter methods
public int getHour() {
return this.hour;
}

public int getMin() {
return this.min;
}

public void setHour(int hour) {
this.hour = hour;
}

public void setMin(int min) {
this.min = min;
}

//Returns time in HH:MM format
public String toString() {
return String.format("%d:%d", this.hour, this.min);
}
//Returns whether or not this time is earlier than the time provided as a parameter. Returns false if both same
public boolean isEarlier(Time time) {
//If they are the same hour, narrow the comparison down to the minute
if (this.hour == time.hour) {
//Exact same process, but for min
if (this.min == time.min) {
//Returns false because if they're the same time, one is not earlier than the other
return false;
} else if (this.min < time.min) {
return true;
} else {
return false;
}
//If this hour is less than the other hour, it is earlier, so return true
} else if (this.hour < time.hour) {
return true;
//Otherwise, the time isn't earlier
} else {
return false;
}
}

}

Task.java

public class Task {
private Time time;
private Date date;
private String title;
private String Important;
private int taskID;
private boolean taskCompleted;

//Default constructor leaves attributes blank
public Task() {
this(null, null, null, null, 0);
}
public Task(Time time, Date date, String title, String Important, int taskID) {
this.time = time;
this.date = date;
this.title = title;
this.Important = Important;
this.taskID = taskID;
this.taskCompleted =false;
}
//accessors and mutators for time, date, title, Important, and taskID
public Time getTime() {
return this.time;
}
public Date getDate() {
return this.date;
}
public String getTitle() {
return this.title;
}
public String getImportant() {
return this.Important;
}
public int getTaskID() {
return this.taskID;
}
public void setTime(Time time) {
this.time = time;
}
public void setDate(Date date) {
this.date = date;
}
public void setTitle(String title) {
this.title = title;
}
public void setImportant(String Important) {
this.Important = Important;
}
public boolean isTaskCompleted() {
return taskCompleted;
}
public void setTaskCompleted(boolean taskCompleted) {
this.taskCompleted = taskCompleted;
}

//Prints task date
public void printTask() {
System.out.printf("Task %d: \n", this.taskID, this.title, this.date.toString(), this.time.toString(), this.Important);
}
//Returns true if this task is earlier than task provided as parameter.
public boolean isEarlier(Task task) {
if (this.date.isEarlier(task.getDate())) {
return true;
} else if (!this.date.isEarlier(task.getDate()) && !task.getDate().isEarlier(this.date)) {
return this.time.isEarlier(task.time);
}
return false;
}
}

Output:

1)Add tasks
2)List tasks
3)Delete Tasks
4)Delete all tasks
5)Mark task as important
6)Mark task as completed
7)Show completed tasks
8)Show important tasks
9)Exit
1
Task Name: 11111
Task Deadline (dd mm yyyy): 10 10 2020
Time Deadline (hh mm): 11 15
Is this task important? (yes/no): yes
Task 1 is added. The To-Do list is as follows:
-------------------------
Task 1:
-------------------------
1)Add tasks
2)List tasks
3)Delete Tasks
4)Delete all tasks
5)Mark task as important
6)Mark task as completed
7)Show completed tasks
8)Show important tasks
9)Exit
1
Task Name: 22222
Task Deadline (dd mm yyyy): 12 12 2020
Time Deadline (hh mm): 11 11
Is this task important? (yes/no): no
Task 2 is added. The To-Do list is as follows:
-------------------------
Task 1:
Task 2:
-------------------------
1)Add tasks
2)List tasks
3)Delete Tasks
4)Delete all tasks
5)Mark task as important
6)Mark task as completed
7)Show completed tasks
8)Show important tasks
9)Exit
2
-------------------------
Task 1:
Task 2:
-------------------------
1)Add tasks
2)List tasks
3)Delete Tasks
4)Delete all tasks
5)Mark task as important
6)Mark task as completed
7)Show completed tasks
8)Show important tasks
9)Exit
8
Important tasks:
Task 1:
1)Add tasks
2)List tasks
3)Delete Tasks
4)Delete all tasks
5)Mark task as important
6)Mark task as completed
7)Show completed tasks
8)Show important tasks
9)Exit
7
Completed tasks:
No completed tasks found
1)Add tasks
2)List tasks
3)Delete Tasks
4)Delete all tasks
5)Mark task as important
6)Mark task as completed
7)Show completed tasks
8)Show important tasks
9)Exit
6
-------------------------
Task 1:
Task 2:
-------------------------
Enter ID number to mark task as completed: 2
Task 2 marked completed:
1)Add tasks
2)List tasks
3)Delete Tasks
4)Delete all tasks
5)Mark task as important
6)Mark task as completed
7)Show completed tasks
8)Show important tasks
9)Exit
7
Completed tasks:
Task 2:
1)Add tasks
2)List tasks
3)Delete Tasks
4)Delete all tasks
5)Mark task as important
6)Mark task as completed
7)Show completed tasks
8)Show important tasks
9)Exit
9
Exiting program, goodbye.

Output With Screenshots:


Related Solutions

The following is a diagram of energy states and transitions in the hydrogen atom.
The following is a diagram of energy states and transitions in the hydrogen atom.Match each of the responses below with the correct arrow from the figure. 1.) The emission line with the shortest wavelength. 2.) The absorption line with the longest wavelength. 3.) The emission line with the highest energy. 4.) The absorption line with the lowest energy. 5.) The emission line with the highest frequency. 6.) The line corresponding to the ionization energy of hydrogen.
Using the 7 state process model introduced in lecture, describe each state and the transitions between...
Using the 7 state process model introduced in lecture, describe each state and the transitions between them. Explain how this model developed from the simple FCFS model implemented on batch systems.
In a state machine diagram with composite states, what is the meaning of a transition that...
In a state machine diagram with composite states, what is the meaning of a transition that goes from the boundary of a state? What is the meaning of a transition that goes to the boundary of a state?
In a state machine diagram with composite states, what is the meaning of a transition that...
In a state machine diagram with composite states, what is the meaning of a transition that goes from the boundary of a state? What is the meaning of a transition that goes to the boundary of a state?
make an energy level diagram (electronic transitions) of compound Fluorescein
make an energy level diagram (electronic transitions) of compound Fluorescein
(A) Indicate the selection rules for atomic electronic transitions and the transitions allowed between the electronic...
(A) Indicate the selection rules for atomic electronic transitions and the transitions allowed between the electronic levels 2S1/2, 2P1/2 and 2P3/2 (B) Indicate if the processes of induced emission and spontaneous emission are resonant processes.
3. Sketch a T-v diagram and find the specified state for the following states a. Propane...
3. Sketch a T-v diagram and find the specified state for the following states a. Propane at critical temp & press find T(°C), p(bar) and v(m3 /kg) b. Water v=.05 m3 /kg, T=140°C find p(bar) c. Water at p=30 MPa, T=100°C find v(m3 /kg)
The Balmer series for the hydrogen atom corresponds to electronic transitions that terminate in the state...
The Balmer series for the hydrogen atom corresponds to electronic transitions that terminate in the state with quantum number n = 2 as shown in the figure below. Consider the photon of longest wavelength corresponding to a transition shown in the figure. (a) Determine its energy  (b) Determine its wavelength Consider the spectral line of shortest wavelength corresponding to a transition shown in the figure  (c) Find its photon energy  (d) Find its wavelength  (e) What is the shortest possible wavelength in the Balmer series?
In what respects does a UML state diagram differ from a state transition diagram?
In what respects does a UML state diagram differ from a state transition diagram?
In the energy level diagram shown below, the energy difference between States 3 and 4 is...
In the energy level diagram shown below, the energy difference between States 3 and 4 is half the energy difference between states 2 and 3. An electron undergoes a transition (a quantum \"leap\") from State 4 to State 3 resulting in the emission of a photon having a wavelength of 660 nm. (Note: 1 nm = 10-9 m.) What is the frequency the emitted photon resulting from an electron making a transition from State 4 to State 3. f4 to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT