Question

In: Computer Science

IN JAVA Minimal Documentation Required (no javadoc) Purpose The purpose of this assignment is to introduce...

IN JAVA

Minimal Documentation Required (no javadoc)

Purpose

The purpose of this assignment is to introduce you to basic operations on a linked list.

Specifics

Design a program that generates a linked list of randomly generated Integer objects. Present a menu at program start that gives the user the following options (most of these options will have corresponding methods in a Linked List class):

1. Create a new list. The size will be specified by the user, make sure a non-negative value is entered. If there is a pre-existing list when this option is chosen, make sure you delete the contents of that list before creating your new list.

2. Sort the list. How you implement this is up to you. You can insert items in the list such that the list is always in sorted order or you can write a sort routine. You may not call a sort routine provided by the Java API.

3. Print the list (to the screen), one number per line.

4. Print the list in reverse order (to the screen), one number per line.

5. Generate a sub-list that contains all the even numbers in the current list. This list should be returned and the contents should be displayed (to the screen), one number per line.

6. Print the contents of every "nth" node in the list. Obtain the "n" from the user, ensure it is greater than 0.

7. Delete node(s) containing an integer value entered by the user. You should report how many were deleted to the user.

8. Delete the contents of the current list.

9. Quit

You may use any linked list implementation you wish (singly linked, doubly linked, dummy head node, circular).

In addition to your LinkedList class, include a driver file called ListTester that will contain the menu and anything else you deem necessary (perhaps a utility to generate random Integers...).

Keep things modular and encapsulate your data properly - do not let anything outside the Linked List class have direct access to any of the nodes.

Do not accept Invalid input of any type from the user and do not let user input crash your program.

PLEASE IMPLEMENT ALL

Solutions

Expert Solution

Dear Student,

I have developed the code for all the requirements. Created separate classes for the functionality and driver.

Please use any editor for testing the code

Let me know if you find any difficulties in understanding. I have provided adequate comments for your understanding.

I am pasting the code and output. I have used java inbuilt linked list for the assignment.

Added menu options for the selection of the functionality. You have to run linkedlist driver for testing the solution.

Code:
---------------------------------------------------------

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

//core class with functinality
public class LinkedListProblem {

   //java inbuilt linked list
   LinkedList<Integer> list;

   //function for creating list. It will refresh the elements whenever called
   public void createList(int size) {

       /*
       if (list.size() > 0&&list!=null)
           list.clear();
           */

       list = new LinkedList<Integer>();
       // random number object
       Random rand = new Random();

       // random numbers of list size elements. Inserting into list

       for (int iter = 0; iter < size; iter++) {
           int rand_int1 = rand.nextInt(100);
           list.add(rand_int1);
       }

   }
//displaying elements of list
   void printList() {
       int n = list.size();
       System.out.println("Size of linked list: " + n);
       for (int i = 0; i < n; ++i)
           System.out.println(list.get(i) );
   }

   //bubble sorting
   public void sort() {

       // get size of the array
       int size = list.size();

       // comparing with adjacent elements. If left element is greater than right
       // element, swap the elements
       for (int i = 0; i < size - 1; i++)
           for (int j = 0; j < size - i - 1; j++)
               if (list.get(j) > list.get(j + 1)) {
                   // swap arr[j+1] and arr[i]
                   int temp = list.get(j);
                   // arr[j] = arr[j+1];

                   int toAdd = list.get(j + 1);
                   list.remove(j);
                   list.add(j, toAdd);
                   list.remove(j + 1);

                   list.add(j + 1, temp);
                   // arr[j+1] = temp;
               }

   }
//reverse the list. Traverse the list from back side and printing
   public void reverseList() {
       int size = list.size();
       int counter = size - 1;
       while (counter >= 0) {
           System.out.println(list.get(counter));
           counter--;// reducing count after each loop
       }
   }
//for genreating even numbers
   public List<Integer> generateEvenList() {
       List<Integer> evenNumbers=new ArrayList<Integer>();
      
       for(int i=0;i<list.size();i++)
       {
           //if n is divisible by 2.then its even number
           if(list.get(i)%2==0)
               evenNumbers.add(list.get(i));
       }
       return evenNumbers;
   }

   public void printNthNumber(int n) {

       int count=n-1;
       while(count<list.size())
       {
           System.out.println(list.get(count));
           count=count+n;//increment by n as we need to print nth number
       }
      
   }

   public void deleteNumber(int number) {

       //delete all the instances of the number in the list
       if(!list.contains(number))
           System.out.println("Number is not present in the list to delete. Enter right number");
       //check if the number is present in the list
       while(list.contains(number))
       list.removeFirstOccurrence(number);
      
   }

   public void clearListContent() {
//java inbuilt method to clear the content
       list.clear();
   }

}
----------------------------------------------

import java.util.List;
import java.util.Scanner;
//driver program for testing the linkedlist
public class LinkedListDriver {

   public static void main(String[] args) {

       //creating object
       LinkedListProblem obj = new LinkedListProblem();
       //creating scan object for taking input

       Scanner scan = new Scanner(System.in);
       int input = 0;
       while (true) {
//menu for user. We have to enter the corresponding number for the functionality
           System.out.println("Welcome To Assignment: ");
           System.out.println("Please choose an option");
           System.out.println("1.Create LinkedList of size");
           System.out.println("2.Sort the linked list");
           System.out.println("3.Print the list");
           System.out.println("4.Print the reverse list");
           System.out.println("5.print the even list");
           System.out.println("6.Print the nth Number list");
           System.out.println("7.Delete the Number");
           System.out.println("8.Clear content from list");
           System.out.println("9.Exit");

           input = scan.nextInt();//input number
           if (input == 9)
               break;
           else if (input == 1) {
               //creating list
               System.out.println("Enter the size of linked list");
               int size = scan.nextInt();
               obj.createList(size);
               obj.printList();

           } else if (input == 2) {
               //sorting
               obj.sort();
               System.out.println("List after sorting.");
               obj.printList();

           } else if (input == 3) {
               obj.printList();
           } else if (input == 4) {
               //reversing
               obj.reverseList();
               System.out.println("List after reversing");

           } else if (input == 5) {
               List<Integer> list = obj.generateEvenList();
               int n = list.size();
               for (int i = 0; i < n; ++i)
                   System.out.println(list.get(i));
           } else if (input == 6) {

               System.out.println("Enter nth number to display");
               int n = scan.nextInt();
               obj.printNthNumber(n);
           } else if (input == 7) {
               System.out.println("Enter the number to delete");

               int number = scan.nextInt();
               System.out.println("List before delete");

               obj.printList();

               obj.deleteNumber(number);
               System.out.println("List after delete");

               obj.printList();
           }
           else if(input==8)
           {
               obj.clearListContent();
           }
           else
               System.out.println("Invalid input. enter numbers from 1 to 9");

       }
   }

}
---------------------

output:

Welcome To Assignment:
Please choose an option
1.Create LinkedList of size
2.Sort the linked list
3.Print the list
4.Print the reverse list
5.print the even list
6.Print the nth Number list
7.Delete the Number
8.Clear content from list
9.Exit
1
Enter the size of linked list
5
Size of linked list: 5
13
53
61
28
78
Welcome To Assignment:
Please choose an option
1.Create LinkedList of size
2.Sort the linked list
3.Print the list
4.Print the reverse list
5.print the even list
6.Print the nth Number list
7.Delete the Number
8.Clear content from list
9.Exit
2
List after sorting.
Size of linked list: 5
13
28
53
61
78
Welcome To Assignment:
Please choose an option
1.Create LinkedList of size
2.Sort the linked list
3.Print the list
4.Print the reverse list
5.print the even list
6.Print the nth Number list
7.Delete the Number
8.Clear content from list
9.Exit
4
78
61
53
28
13
List after reversing
Welcome To Assignment:
Please choose an option
1.Create LinkedList of size
2.Sort the linked list
3.Print the list
4.Print the reverse list
5.print the even list
6.Print the nth Number list
7.Delete the Number
8.Clear content from list
9.Exit
5
28
78
Welcome To Assignment:
Please choose an option
1.Create LinkedList of size
2.Sort the linked list
3.Print the list
4.Print the reverse list
5.print the even list
6.Print the nth Number list
7.Delete the Number
8.Clear content from list
9.Exit
6
Enter nth number to display
2
28
61
Welcome To Assignment:
Please choose an option
1.Create LinkedList of size
2.Sort the linked list
3.Print the list
4.Print the reverse list
5.print the even list
6.Print the nth Number list
7.Delete the Number
8.Clear content from list
9.Exit
7
Enter the number to delete
28
List before delete
Size of linked list: 5
13
28
53
61
78
List after delete
Size of linked list: 4
13
53
61
78
Welcome To Assignment:
Please choose an option
1.Create LinkedList of size
2.Sort the linked list
3.Print the list
4.Print the reverse list
5.print the even list
6.Print the nth Number list
7.Delete the Number
8.Clear content from list
9.Exit
8
Welcome To Assignment:
Please choose an option
1.Create LinkedList of size
2.Sort the linked list
3.Print the list
4.Print the reverse list
5.print the even list
6.Print the nth Number list
7.Delete the Number
8.Clear content from list
9.Exit
9


Related Solutions

Objective: The purpose of this assignment is to introduce declaration of list objects, the forstatement and...
Objective: The purpose of this assignment is to introduce declaration of list objects, the forstatement and the def keyword used to define functions. Problem: Write a Python module (a text file containing valid Python code) named p3.py. This file will contain the following.  Definition of a list containing strings which are in turn integers. These integers represent years, which will be used as inputs to the next item in the file  Definition of a function named isLeap. This...
Note: This is a single file C++ project - No documentation is required on this assignment....
Note: This is a single file C++ project - No documentation is required on this assignment. Write a Fraction class whose objects will represent Fractions. Note: You should not simplify (reduce) fractions, you should not use "const," and all of your code should be in a single file. In this single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. You must provide the following member functions: A...
The Brain Game The purpose of this assignment is to introduce you to the well-known scientific...
The Brain Game The purpose of this assignment is to introduce you to the well-known scientific fact that humans do not make decisions rationally. Most of the time, decisions are made without people even realizing the “why” behind their decisions. Our decisions are often driven by our own biases instead of logic or good judgement. These numerous “poor instincts” are called cognitive biases. It is important to identify cognitive biases in Personal Finance because they cause so many of the...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods.(Need Comment, Write by Java Code) Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of two dimensional arrays, input validation, and methods. (Write by Java Code, Need Comment) Instructions A theater seating chart is implemented as a two-dimensional array of ticket prices, like this: Seat Ticket Price 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates the use of loops, and generation of random integers. Instructions You are taking some time off from your paint business and currently are on vacation in Bahamas. You decide to write a Java program that generates 10 random numbers between 1 and 20 (all integers). You cannot use arrays (even if you know what they are) to store these numbers. It then picks up...
Law Library Reading and Assignment Purpose: To introduce you to the law library system, various resources...
Law Library Reading and Assignment Purpose: To introduce you to the law library system, various resources available and the importance to legal staff (attorneys, paralegals, and legal assistants, etc.) Outcome: You will be able to demonstrate general knowledge of legal references and resource material commonly found in law offices/law libraries. Assignment - Questions What are the four categories of law? What are the three classes of law? Describe the three types of library organization Describe how a court form can...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT