Question

In: Computer Science

In java. Using a linked list only, iterator and scanner. Develop a program to maintain a...

In java. Using a linked list only, iterator and scanner.

Develop a program to maintain a Linked List of homework assignments name and due date. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services each contained within its own method:

  • Add a new assignment. (3 pts)
  • Remove an assignment. (3pts)
  • Provide a list of the assignments in the order they were assigned. (3pts)
  • Find the assignment(s) with the earliest due date (3pts)

Solutions

Expert Solution

code in java with explanation in comments

import java.util.*;
import java.text.*; 
class Test{
    //static helper method to print menu
    public static void print_menu(){
        System.out.println("Select an option.");
        System.out.println("1. Add a new assignment");
        System.out.println("2. Remove an assignment");
        System.out.println("3. List assignment in order they were assigned");
        System.out.println("4. Assignment with earliest due date");
        System.out.println("0. Exit");
    }
    public static void main(String[] args) throws Exception{
        //linked list to store assignment name
        LinkedList<String> homeworkAssignmentsName = new LinkedList<String>();
        //linked list to store assignment due date
        LinkedList<Date> homeworkAssignmentsDueDate = new LinkedList<Date>();
        //Scanner class to take input
        Scanner sc = new Scanner(System.in);
        //declare variable to read from user
        String name, dateString;
        Date date;
        //Dateformatter to convert entered date to date class
        DateFormat formatter = new SimpleDateFormat("ddMMyyyy");
        int op;
        //Menu driven program
        do{
            //print menu
            print_menu();
            //take input from user
            op = sc.nextInt();
            //check for invalid input
            if(op<0||op>4){
                 System.out.println("Invalid Input\nPlease select an option from 0 to 4 only");
            }
            //perform different operations depending on input
            switch(op){
                case 1: {
                    //take input for new assignment to add
                    System.out.print("Enter name of assignment: ");
                    name=sc.next();
                    System.out.print("Enter due date (ddMMyyyy): ");
                    dateString=sc.next();
                    date = formatter.parse(dateString);
                    //add new assignment to our linked list
                    homeworkAssignmentsName.add(name);
                    homeworkAssignmentsDueDate.add(date);
                    break;
                }
                case 2: {
                     //take input for assignment to remove
                    System.out.print("Enter name of assignment to remove: ");
                    name=sc.next();
                    //search for the assignment in our list
                    int index = homeworkAssignmentsName.indexOf(name);
                    if(index==-1){
                        //display message if not found
                        System.out.println("Assignment not found!");
                    }else{
                        //delete both assignment and due date if found
                        homeworkAssignmentsName.remove(index);
                        homeworkAssignmentsDueDate.remove(index);
                        System.out.println("Assignment deleted.");
                    }
                    break;
                }
                case 3: {
                    //declare iterators to traverse in the linked list
                    Iterator it1 = homeworkAssignmentsName.iterator();
                    Iterator it2 = homeworkAssignmentsDueDate.iterator();
                    System.out.println("Assignments: Name  -  Date");
                    //traverse both list and print on console
                    while(it1.hasNext()){
                        System.out.println(it1.next() + "  -  " + it2.next());
                    }
                    break;
                }
                case 4:{
                    //declare iterators to traverse in the linked list
                    Iterator it = homeworkAssignmentsDueDate.iterator();
                    //check if there are no assignments and display error
                    if(!it.hasNext()){
                        System.out.println("No assignments added!");
                    }
                    else{
                        //initialize d with first value to compare
                        Date d = (Date)it.next();
                        //compare d with all other dates and store least date
                        while(it.hasNext()){
                            Date x = (Date)it.next();
                            if(d.after(x)){
                                d=x;
                            }
                        }
                        //search for assignment with this date
                        int index = homeworkAssignmentsDueDate.indexOf(d);
                        //display in console
                        System.out.println("Assignments with least due date is : " + homeworkAssignmentsName.get(index) + "  -  " + d);
                    } 
                    break;
                }
            }
        }while(op!=0);
    }   
}

Code Screenshot

Console Output Screenshot

Let me know in the comments if you have any doubts.
Do leave a thumbs up if this was helpful.


Related Solutions

Develop a program to maintain a Linked List of homework assignments name and due date. When...
Develop a program to maintain a Linked List of homework assignments name and due date. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services each contained within its own method: Add a new assignment. Remove an assignment. Provide a list of the assignments in the order they were assigned. Find the assignment(s) with the earliest due date...
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of...
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of names (two separate programs).The following operations are available: insert rear, insert front, remove a particular element, and print the whole list. Do not implement an ADT(Do not use a class with data and operations) Just set up a fixed size array or a linked list of nodes in main and provide code in main or functions/static methods to perform insert, remove, and print. You...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate that it can solve the Tower of Hanoi problem. Write implementation body of method “infixToPrefix(String[] e)” of class ArithmeticExpression to convert infix expressions into prefix expressions.
Using Linked List, create a Java program that does the following without using LinkedList from the...
Using Linked List, create a Java program that does the following without using LinkedList from the Java Library. and please include methods for each function. Create a menu that contains the following options : 1. Add new node at the end of LL. ( as a METHOD ) 2. Add new node at the beginning of LL. ( as a METHOD ) 3. Delete a node from the end of LL. ( as a METHOD ) 4. Delete a node...
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
C++ Create a program that use the linkedbag 3. Develop a program to maintain a list...
C++ Create a program that use the linkedbag 3. Develop a program to maintain a list of homework assignments. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services: • Add a new assignment. • Remove an assignment. • Provide a list of the assignments in the order they were assigned. • Find the assignment(s) with the earliest...
Data structure program Implement (your own) the Radix Sort using single linked list java language
Data structure program Implement (your own) the Radix Sort using single linked list java language
Objective: Learning linked list. Problem Specification:             An employer would like to maintain a linked list...
Objective: Learning linked list. Problem Specification:             An employer would like to maintain a linked list for employees, the data stored is ·An employee number (a positive integer) ·A yearly salary (a float). ·Number of dependents (a short positive integer) The employer would like you as the programmer to design and implement a linked list using classes. For each class two files are needed, one to define the class, the other to implement the methods. In addition, the client uses...
Write a program of doubly Circular linked list to maintain records of employees. Take employee ID,...
Write a program of doubly Circular linked list to maintain records of employees. Take employee ID, name and salary as data of each employee. Search a particular record on ID and display the previous and next records as well. Whichever ID it give, it should display all the records because of being circular. Code needed in Java.
how do you add two matrices linked list in java? (am using linked list because 2D...
how do you add two matrices linked list in java? (am using linked list because 2D arrays are not allowed.) ex [1st matrix] 1 3 2 4 2 1 3 2 4 + [2nd matrix] 3 2 3 2 1 4 5 2 3 = [3rd matrix] 4 5 5 6 3 5 8 4 7
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT