Question

In: Computer Science

Please make your Task class serializable with a parameterized and copy constructor, then create a TaskListDemo...

Please make your Task class serializable with a parameterized and copy constructor, then create a TaskListDemo with a main( ) method that creates 3 tasks and writes them to a binary file named "TaskList.dat". We will then add Java code that reads a binary file (of Task objects) into our program and displays each object to the console. More details to be provided in class.

Here is a sample transaction with the user (first time the code is run):

Previously saved Tasks from binary file:
[None, please enter new Tasks]

Please enter task name (or "quit" to exit): Ace CS 112 Final Exam
Please enter due date (in form MM/DD/YYYY): 10/10/2019
Please enter deadline : 3:20 PM
Please enter priority : 1

After 1 Task has been saved (second time the code is run):

Previously saved Tasks from binary file:
Task [name=Ace CS 112 Midterm Exam, dueDate=10/10/2019, deadline=3:20 PM, priority=High]

Please enter task name (or "quit" to exit): quit

I don't know how to print the priorities High, medium, and Low in the toString method to where it is stored in the binary file after the user types 1, 2, or 3. 1 for high, 2 for medium, and 3 for low.

import java.io.*;
import java.util.Scanner;

public class IC10_TaskList {

    public static void main(String[] args) {

        String name, dueDate, deadline;
        int priority;
        Scanner keyboard = new Scanner(System.in);

        Task[] allTasks = new Task[10];
        int count = 0;

        File binaryFile = new File("Task.dat");

        try {

            System.out.println("\nPreviously saved tasks from the binary file:");

            if (binaryFile.exists()) {
                ObjectInputStream inFile = new ObjectInputStream(new FileInputStream(binaryFile));

                allTasks = (Task[]) inFile.readObject();

                inFile.close();

                for(int i = 0; i < allTasks.length; i++){

                    if(allTasks[i] != null){
                        System.out.println(allTasks[i]);
                        count++;
                    }

                    else
                        break;
                }
            }

            else
                System.out.println("[None, Please enter new task data]");

        }

        catch(IOException e){

            System.err.println("Cannot find Task.dat");
        }

        catch(ClassNotFoundException e){

            System.err.println("Serial Task version does not match.");
        }
        do{
            System.out.print("\nPlease enter task name(or \"quit\" to exit): ");
            name = keyboard.nextLine();
            if(name.equalsIgnoreCase("quit"))
                break;
            System.out.print("Please enter due date(in form MM/DD/YYYY): ");
            dueDate = keyboard.nextLine();
            System.out.print("Please enter deadline: ");
            deadline = keyboard.nextLine();
            System.out.print("Please enter priority: ");
            priority = keyboard.nextInt();

            allTasks[count++] = new Task(deadline, dueDate, name, priority);

            keyboard.nextLine();

        } while(!name.equalsIgnoreCase("quit"));

        try{
            ObjectOutputStream outFile = new ObjectOutputStream(new FileOutputStream(binaryFile));

            outFile.writeObject(allTasks);

            outFile.close();
        }

        catch(IOException e){

            System.err.println("File Task.dat cannot be found and/or written.");
        }
    }
}
import java.io.Serializable;
import java.util.Objects;

public class Task implements Serializable {

    public static final long serialVersionUID = 1;

    private String mDeadline;
    private String mDueDate;
    private String mName;
    private int mPriority, mChoice;

    public Task(String deadline, String dueDate, String name, int priority) {
        mDeadline = deadline;
        mDueDate = dueDate;
        mName = name;
        mPriority = priority;
    }

    public String getDeadline() {
        return mDeadline;
    }

    public void setDeadline(String deadline) {
        mDeadline = deadline;
    }

    public String getDueDate() {
        return mDueDate;
    }

    public void setDueDate(String dueDate) {
        mDueDate = dueDate;
    }

    public String getName() {
        return mName;
    }

    public void setName(String name) {
        mName = name;
    }

    public int getPriority() {
        return mPriority;
    }

    public void setPriority(int priority) {
        mPriority = priority;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Task task = (Task) o;
        return mPriority == task.mPriority &&
                mDeadline.equals(task.mDeadline) &&
                mDueDate.equals(task.mDueDate) &&
                mName.equals(task.mName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(mDeadline, mDueDate, mName, mPriority);
    }

    @Override
    public String toString() {
        return "Task[" +
                "Name = " + mName +
                ", Due Date = " + mDueDate +
                ", Deadline = " + mDeadline +
                ", Priority = " + mPriority +
                "]";
    }
}

Solutions

Expert Solution

//Task.java

import java.io.Serializable;

import java.util.Objects;

public class Task implements Serializable{

      

       public static final long serialVersionUID = 1;

    private String mDeadline;

    private String mDueDate;

    private String mName;

    private int mPriority, mChoice;

    public Task(String deadline, String dueDate, String name, int priority) {

        mDeadline = deadline;

        mDueDate = dueDate;

        mName = name;

        mPriority = priority;

    }

    public String getDeadline() {

        return mDeadline;

    }

    public void setDeadline(String deadline) {

        mDeadline = deadline;

    }

    public String getDueDate() {

        return mDueDate;

    }

    public void setDueDate(String dueDate) {

        mDueDate = dueDate;

    }

    public String getName() {

        return mName;

    }

    public void setName(String name) {

        mName = name;

    }

    public int getPriority() {

        return mPriority;

    }

    public void setPriority(int priority) {

        mPriority = priority;

    }

    @Override

    public boolean equals(Object o) {

        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        Task task = (Task) o;

        return mPriority == task.mPriority &&

                mDeadline.equals(task.mDeadline) &&

                mDueDate.equals(task.mDueDate) &&

                mName.equals(task.mName);

    }

    @Override

    public int hashCode() {

        return Objects.hash(mDeadline, mDueDate, mName, mPriority);

    }

    @Override

    public String toString() {

      

       String str = "Task[" +

                "Name = " + mName +

                ", Due Date = " + mDueDate +

                ", Deadline = " + mDeadline +

                ", Priority = " ;

       

       // assign priority High, Medium or Low based on mPriority

       if(mPriority == 1)

             str += "High";

       else if(mPriority == 2)

             str += "Medium";

       else

             str += "Low";

       str += "]";

      

       return str;

    }

}

//end of Task.java

//IC10_TaskList.java

import java.io.*;

import java.util.Scanner;

public class IC10_TaskList {

       public static void main(String[] args) {

        String name, dueDate, deadline;

        int priority;

        Scanner keyboard = new Scanner(System.in);

        Task[] allTasks = new Task[10];

        int count = 0;

        File binaryFile = new File("Task.dat");

        try {

            System.out.println("\nPreviously saved tasks from the binary file:");

            if (binaryFile.exists()) {

                ObjectInputStream inFile = new ObjectInputStream(new FileInputStream(binaryFile));

                allTasks = (Task[]) inFile.readObject();

                inFile.close();

                for(int i = 0; i < allTasks.length; i++){

                    if(allTasks[i] != null){

                        System.out.println(allTasks[i]);

                        count++;

                    }

                    else

                        break;

                }

            }

            else

                System.out.println("[None, Please enter new task data]");

        }

        catch(IOException e){

            System.err.println("Cannot find Task.dat");

        }

        catch(ClassNotFoundException e){

            System.err.println("Serial Task version does not match.");

        }

        do{

            System.out.print("\nPlease enter task name(or \"quit\" to exit): ");

            name = keyboard.nextLine();

            if(name.equalsIgnoreCase("quit"))

                break;

            System.out.print("Please enter due date(in form MM/DD/YYYY): ");

            dueDate = keyboard.nextLine();

            System.out.print("Please enter deadline: ");

            deadline = keyboard.nextLine();

            System.out.print("Please enter priority: ");

            priority = keyboard.nextInt();

            allTasks[count++] = new Task(deadline, dueDate, name, priority);

            keyboard.nextLine();

        } while(!name.equalsIgnoreCase("quit"));

        try{

            ObjectOutputStream outFile = new ObjectOutputStream(new FileOutputStream(binaryFile));

            outFile.writeObject(allTasks);

            outFile.close();

        }

        catch(IOException e){

            System.err.println("File Task.dat cannot be found and/or written.");

        }

       

        keyboard.close();

    }

}

//end of IC10_TaskList.java

Output:


Related Solutions

IN C++ PLEASE: (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign...
IN C++ PLEASE: (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). Public member functions SetDescription() mutator & GetDescription() accessor PrintItemDescription() - Outputs the item name and description in the format name: description Private data members string itemDescription - Initialized in default constructor to "none" (2) Create three new files: ShoppingCart.h - Class declaration ShoppingCart.cpp - Class definition main.cpp - main() function (Note:...
Step 1: Extend the dispenserType class per the following specifications. Add a parameterized constructor to initialize...
Step 1: Extend the dispenserType class per the following specifications. Add a parameterized constructor to initialize product name (name), product cost (cost) and product quantity (noOfItems). This constructor will be invoked from main.cpp. One such call to this constructor will look like: dispenserType sanitizer("hand sanitizer", 50, 100); Add the declaration and definition of this parameterized constructor to .h and .cpp files. Step2: Define a new class, cashRegister. Start by creating cashRegister.h and cashRegister.cpp files. Be sure to update your CMakeLists.txt...
This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor...
This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor function or ES6 class for a Book object. The Book object should store the following data in attributes: title and author. Library Class Create a constructor function or ES6 class for a Library object that will be used with the Book class. The Library object should be able to internally keep an array of Book objects. B. Methods to add Library Class The class...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
Create a class called Cipher. Make the constructor accept some text and a key. Encrypt the...
Create a class called Cipher. Make the constructor accept some text and a key. Encrypt the given text using the key. Use the following cipher: Take the key and mod it by 26. Example: a key of 30 becomes 4. If the character is a letter, shift it by the key, but 'wrap around' the alphabet if necessary. If the character is not a letter, then shift it by the key but do not wrap. Check the test cases for...
(In C++) Task 1: Implement a code example of Constructor Chaining / Constructor Overloading. Make sure...
(In C++) Task 1: Implement a code example of Constructor Chaining / Constructor Overloading. Make sure to label class and methods with clear descriptions describing what is taking place with the source code. Attach a snipping photo of source code and output
Define the following class: class XYPoint { public: // Contructors including copy and default constructor //...
Define the following class: class XYPoint { public: // Contructors including copy and default constructor // Destructors ? If none, explain why double getX(); double getY(); // Overload Compare operators <, >, ==, >=, <=, points are compare using their distance to the origin: i.e. SQR(X^2+Y^2) private: double x, y; };
in java Create a class City with x and y as the class variables. The constructor...
in java Create a class City with x and y as the class variables. The constructor with argument will get x and y and will initialize the city. Add a member function getDistanceFrom() to the class that gets a city as the input and finds the distance between the two cities.
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
White the class Trace with a copy constructor and an assignment operator, printing a short message...
White the class Trace with a copy constructor and an assignment operator, printing a short message in each. Use this class to demonstrate the difference between initialization Trace t("abc"); Trace u = t; and assignment. Trace t("abc"); Trace u("xyz"); u = t; the fact that all constructed objects are automatically destroyed. the fact that the copy constructor is invoked if an object is passed by value to a function. the fact that the copy constructor is not invoked when a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT