Question

You are to write a Java Class using Generics. This class is to be a Double Linked List Container for holding other objects which are comparable. The Class should support the following features: 1. Print the elements of the collection in order (ascending). 2. nsert an item. 3. Remove an item. 4. Empty the collection. 5. Find the index of an element, using a binary recursive search The program should include a driver class (Main) that provides a com- mand line input/output. This driver class should: 1. Print a prompt indicating options to test each of the features above. 2. Provide an option to exit. 3. Accept command-line input for both integer and decimal values (such as 2.3). Additional Requirements 1. The class should be named MyList.java 2. The main class should be named Main.java 3. You are not to use any third party packages for List type collections. 4. The only packages you should be useing are for the Input/Output and for holding the input data. All code should have proper error checking and should have no unexpected termination. If invalid data is input, the user should be informed ofit and allowed to re-enter the data

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Output

Menu- 1. print list 2. insert 3. remove 4. empty list 5. find index 6. exit Enter your choice 2 Enter element to insert :1 --Enter your choice: -- し13t content ー- 10 20 -- Menu 1. print list 2. insert 3. remove 4. empty list 5. find index 6. exit Ent

Program MyList.java

=========================================

//Node class for doubly linked list structure
class Node<T> {

   public T data;
   public Node<T> next;
   public Node<T> previous;

   // Public constructor
   public Node(T id) {
       data = id;
   }
}

// MyList class for doubly linked list operations
public class MyList<T> {

   private Node<T> first;
   private Node<T> last;

   // Default constructor
   public MyList() {
       first = null;
       last = null;
   }

   private boolean isEmpty() {
       return first == null;
   }

   // 1. print function
   public void print() {

       System.out.println("-- List content --");
       Node<T> temp = first;
      
       if(temp== null) {
           System.out.println("No elements are present to display!");
       } else {
           while (temp != null) {
               System.out.print(temp.data+" ");
               temp = temp.next;
           }
          
           System.out.print("\n");
       }
   }

   // 2. insert function
   public void insert(T data) {

       Node<T> newNode = new Node<T>(data);

       // Check if empty
       if (isEmpty()) {
           first = newNode;
       } else {
           last.next = newNode;
           newNode.previous = last;
       }
       last = newNode;
   }

   // 3. delete function
   public Node<T> delete(T key) {

       Node<T> temp = first;

       // Get the node where the key is present
       while (temp.data != key) {

           temp = temp.next;
           if (temp == null)
               return null;
       }

       // Check for fist node
       if (temp == first) {
           first = temp.next;
       } else {
           temp.previous.next = temp.next;
       }

       // Check for last node
       if (temp == last) {
           last = temp.previous;
       } else {
           temp.next.previous = temp.previous;
       }

       return temp;
   }

   // 4. empty list
   public void emptyList() {

       // by making null for first and last nodes
       first = null;
       last = null;
   }

   // 5. find index
   public int findIndex(T key) {

       int index = -1;
       int i = 0;
       Node<T> temp = first;
       while (temp != null) {
           if (temp.data == key) {
               index = i;
               break;
           }
           i++;
           temp = temp.next;
       }

       return index;
   }

}

Program Main.java

=========================================

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {
      
       //Creating scanner object to read input from keyboard

       Scanner sc = new Scanner(System.in);
      
       //Create object of MyList
       MyList<Integer> myList = new MyList<Integer>();
      
       int input;
      
       while(true) {
          
           System.out.println("-- Menu -- ");
           System.out.println("1. print list");
           System.out.println("2. insert");
           System.out.println("3. remove");
           System.out.println("4. empty list");
           System.out.println("5. find index");
           System.out.println("6. exit");

           System.out.println("Enter your choice :");
           int choice = sc.nextInt();
          
           switch(choice) {
          
               case 1:

                   myList.print();
                   break;
                  
               case 2:

                   System.out.print("Enter element to insert :");
                   input = sc.nextInt();
                   myList.insert(input);
                   break;
  
               case 3:

                   System.out.print("Enter element to delete :");
                   input = sc.nextInt();
                   myList.delete(input);
                   break;

               case 4:
                   myList.emptyList();
                   break;

               case 5:
                   System.out.print("Enter element to search :");
                   input = sc.nextInt();
                   System.out.println("index found at position "+myList.findIndex(input));
                   break;
                  
               case 6:
                   break;
                  
               default:

                   System.out.println("Invalid Chioce!");

           }
          
           //If choice is 6 exit while loop
           if(choice==6) {

               break;
           }
          
       }

      
       //Close input stream
       sc.close();

   }
}

Add a comment
Know the answer?
Add Answer to:
You are to write a Java Class using Generics. This class is to be a Double...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • In this assignment, you sort a list of strings using mergesort and the compareToIgnoreCase method of...

    In this assignment, you sort a list of strings using mergesort and the compareToIgnoreCase method of the String class. The strings are provided as arguments to your main method. In case you haven't yet learned to configure command-line arguments to main in your IDE, now is a good time to learn. Your program should sort the array of strings using mergesort, then print the strings one per line, in order from smallest ("a") to largest ("z"). The name of your...

  • Write a complete Java program using the StringTokenizer class that computes and displays the average of...

    Write a complete Java program using the StringTokenizer class that computes and displays the average of a list of grades read from the command line. Each grade should be entered on the same line separated by commas. Enter signifies the end of the input.

  • what is the solution for this Java problem? Generics Suppose you need to process the following...

    what is the solution for this Java problem? Generics Suppose you need to process the following information regarding Students and Courses: A Student has a name, age, ID, and courseList. The class should have a constructor with inputs for setting name, ID and age. The class should have setters and getters for attributes name, ID and age, and a method addCourse, removeCourse, printSchedule. courseList: use the generic class ArrayList<E> as the type of this attribute addCourse: this method takes as...

  • Write a Gui programming by using JavaFx menus, stage and screen concepts to the RetailItem class,...

    Write a Gui programming by using JavaFx menus, stage and screen concepts to the RetailItem class, Write a class named Retailltem that holds data about an item in a retail store. The class should have the following fields description: The description field is a String object that holds a brief description of the item . unitsOnHand: The unitsOnHand field is an int variable that holds the number of units currently in inventory Price: The price field is a double that...

  • In this assignment, you will write a Java program(s) to print the binary representation of a...

    In this assignment, you will write a Java program(s) to print the binary representation of a positive integer inserted from command line.  You must finish your assignment in 2 different ways: Using a recursive method Using an iterative method     Your main method must be: public static void main(String[] args) {      int input;         input = Integer.parseInt(args[0]);     print_recursion(input);     print_binary(input); }     You must implement a class and test your program by different input values, including 0. Comment your program properly Example of test...

  • Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement...

    Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement some list functionalities on an ArrrayList of generic type using type parameters in method definition Our goal for this lab is to continue using the divide and conquer approach of splitting a list into its head and tail and keep recursing on the tail (which happens to be smaller). However, instead of trying the approach on a string (a list of characters) we would...

  • Write a Java program in Eclipse that reads from a file, does some clean up, and...

    Write a Java program in Eclipse that reads from a file, does some clean up, and then writes the “cleaned” data to an output file. Create a class called FoodItem. This class should have the following: A field for the item’s description. A field for the item’s price. A field for the item’s expiration date. A constructor to initialize the item’s fields to specified values. Getters (Accessors) for each field. This class should implement the Comparable interface so that food...

  • Please write a c++ header file, class implementation file and main file that does all of...

    Please write a c++ header file, class implementation file and main file that does all of the following and meets the requirements listed below. Also include a Output of your code as to show that your program works and functions properly. EXERCISING A DOUBLY-LINKED LIST CLASS This project consists of two parts, the second of which appears below. For the first part, write a class that implements an unordered list abstract data type using a doubly-linked list with pointers to...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT