Question

In: Computer Science

Your Task: Starting with the following generic classes: public class LinkedListNode<E> { public E element; public...

Your Task:

Starting with the following generic classes:

public class LinkedListNode<E>
{
public E element;
public LinkedListNode<E> next;
}
public class LinkedListBox<E>
{
public LinkedListNode<E> head;
public int count = 0;
}

Create a LINKED LIST of random integers between 1 and 20. The program must prompt the user for the number of random integers to create before creating the list (i.e: if user asks for 40 integers then the program will link 40 integers between 1 and 20 generated at random).

Once this link is created:

1. Display the linked list. [4 marks]

2. Modify the linked list by removing all EVEN numbers from the list. [6 marks]

3. Redisplay the linked list.

Sample Output:

How many integers for the list? 5

LINKED-LIST with odd and even random numbers:

-->1-->6-->7-->4-->17

LINKED-LIST with even numbers removed:

-->1-->7-->17


(Please also give comments)

Solutions

Expert Solution

Below is the solution:


import java.util.*;

class LinkedListNode<E>
{
public E element;
public LinkedListNode<E> next;
}
class LinkedListBox<E>
{
public LinkedListNode<E> head;
public int count = 0;
}


public class Main
{
   public static void main(String[] args) {
   int number = 0;
   LinkedListNode<Integer> node = null;
   LinkedListBox<Integer> myLinkedList = new LinkedListBox<Integer>();;
   LinkedListNode lastNode = null;
     
   int max = 20; int min = 1;
   System.out.println("Enter the number of random integers to create");
  
   Scanner sc = new Scanner(System.in);
   number = sc.nextInt();

//Random number generation code


   for(int i=0; i < number; i++){
   node = new LinkedListNode<Integer>();
  
   int element = (int)(Math.random()*(max-min+1)+min);
   node.element = element;
   node.next = null;

//Checking if the head is present or not


       if (myLinkedList.head == null){
       myLinkedList.head = node;
       myLinkedList.count++;
       //lastNode = node;
       }
       else{
       LinkedListNode tmpNode = myLinkedList.head;
   while(tmpNode.next != null){
   tmpNode = tmpNode.next;
   }
   tmpNode.next = node;
   myLinkedList.count++;
       }
      
   }
  
   System.out.println("Displaying the Linked List : ");
   LinkedListNode tmpNode = myLinkedList.head;
//looping through the List and printing the elements

while(tmpNode.next != null){
   System.out.print(tmpNode.element + "->");
   tmpNode = tmpNode.next;
   }
   System.out.println(tmpNode.element);
     

  
   System.out.println("Removing the even numbers from Linked List ");
   tmpNode = myLinkedList.head;
LinkedListNode prevNode = null;


//Checking if the head value is even or not
while(tmpNode != null && (Integer)tmpNode.element % 2 == 0){
   myLinkedList.head = tmpNode.next;
   tmpNode = tmpNode.next;
   }
  
   tmpNode = tmpNode.next;
prevNode = myLinkedList.head;
  
while (tmpNode != null )
{

//checking the node value is even or not
if ((Integer)tmpNode.element % 2 != 0){
prevNode = tmpNode;
tmpNode = tmpNode.next;
}
else {

//Removing the node if the value is even.
prevNode.next = tmpNode.next;
tmpNode = tmpNode.next;
}
}

System.out.println("Displaying the Linked Listafter removing the even elements : ");
   tmpNode = myLinkedList.head;
   while(tmpNode.next != null){

//printing the values after removing the even numbers.
   System.out.print(tmpNode.element + "->");
   tmpNode = tmpNode.next;
   }
   System.out.println(tmpNode.element);
   }
}


Related Solutions

Task Generics: GenericStack Class. Java. package Modul02; public class GenericStack<E> { private java.util.ArrayList<E> list = new...
Task Generics: GenericStack Class. Java. package Modul02; public class GenericStack<E> { private java.util.ArrayList<E> list = new java.util.ArrayList<>(); public int size() { return list.size(); } public E peek() { return list.get(size() - 1); } public void push(E o) { list.add(o); } public E pop() { E o = list.get(size() - 1); list.remove(size() - 1); return o; } public boolean isEmpty() { return list.isEmpty(); } @Override public String toString() { return "stack: " + list.toString(); } } package Modul02; public class TestGenericStack...
To write a Generic Collection class for Stack<E>, using the generic Node<E> from Lab 5, and...
To write a Generic Collection class for Stack<E>, using the generic Node<E> from Lab 5, and test it using a stack of Car, Integer, and String Stack<E> For Programming Lab 6, you are to write your own Generic Collection for a Stack that will use your Node<E> from Lab 5 UML for Stack<E> Stack<E> - top : Node<E> - numElements : int + Stack( ) + push( element : E ) : void + pop( ) : E + size(...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main {   public static void main( String[] args ) {     Scanner myInput = new Scanner(System.in); // Create a Scanner object     System.out.println("Enter (3) digits: ");     int W = myInput.nextInt();     int X = myInput.nextInt();     int Y = myInput.nextInt();      } } Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and...
Consider the following classes: public class Clock extends Bear { public void method3() { System.out.println("Clock 3");...
Consider the following classes: public class Clock extends Bear { public void method3() { System.out.println("Clock 3"); } } public class Lamp extends Can { public void method1() { System.out.println("Lamp 1"); } public void method3() { System.out.println("Lamp 3"); } } public class Bear extends Can { public void method1() { System.out.println("Bear 1"); } public void method3() { System.out.println("Bear 3"); super.method3(); } } public class Can { public void method2() { System.out.println("Can 2"); method3(); } public void method3() { System.out.println("Can 3"); }...
The following classes along with the driver has been created and compiled. public class Car {...
The following classes along with the driver has been created and compiled. public class Car { public void method1() { System.out.println("I am a car object"); } } class Point { public void method2() { System.out.println("I am a Point object"); } } class Animal { public void method3() { System.out.println("I am an animal object"); } } The following driver class has been created and all the lines of code inside the for loop is not compiling. Modify the code and write...
Task 2/2: Java program Based upon the following code: public class Main {   public static void...
Task 2/2: Java program Based upon the following code: public class Main {   public static void main( String[] args ) {     String alphabet = "ABCDEFGHIJKLMNMLKJIHGFEDCBA";     for( <TODO> ; <TODO> ; <TODO> ) {       <TODO>;     } // Closing for loop   } // Closing main() } // Closing class main() Write an appropriate loop definition and in-loop behavior to determine if the alphabet string is a palindrome or not. A palindrome is defined as a string (or more generally, a token) which...
I have three classes: class VolleyPlayer, class VolleyTeam and class TestDriver 1.class: public class VolleyPlayer implements...
I have three classes: class VolleyPlayer, class VolleyTeam and class TestDriver 1.class: public class VolleyPlayer implements Comparable<VolleyPlayer> { // instance variables - private String name; private int height; private boolean female; public VolleyPlayer(String name, int height, boolean female) { this.name = name; this.height = height; this.female = female; } public String toString() {    return (female ?"Female":"Male")+": "+name+" ("+height+" cm)"; } public boolean isFemale() {    return female; } public boolean isMale() {    return !female; } public int getHeight()...
public class Assignment3 { public static Queue> makeQueue(double[] a){ // Each element of the given array...
public class Assignment3 { public static Queue> makeQueue(double[] a){ // Each element of the given array a must be inserted into a BTNode, // this method returns a queue of BTNodes, each node will contain a dataNode // the dataNode will have the value equal to the element of the array // count equal to the number of times that the element repeats // min and max must be equal to value. // the BTNode created must have its parent,...
JAVA Assignement In the same file, create two classes: a public class Module1 and a non-public...
JAVA Assignement In the same file, create two classes: a public class Module1 and a non-public (i.e. default visibility) class Module1Data. These classes should have the following data fields and methods: 1) Module1 data fields: a. an object of type Module1Data named m1d b. an object of type Scanner named scanner c. a double named input 2) Module1Data methods: a. a method named square that returns an int, accepts an int parameter named number, and returns the value of number...
Remove the Head element from the code below: public class LinkedList {    class Node{ int...
Remove the Head element from the code below: public class LinkedList {    class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element);    if(first == null) { first = newValueNode; } else { last.nextElement = newValueNode; } last = newValueNode; } public void displayValues() { Node recent = first; if(first ==...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT