Question

In: Computer Science

Write a Console Java program that inserts 25 random integers in the range of 0 to...

Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List.

(Use SecureRandom class from java.security package.
SecureRandom rand = new SecureRandom(); - creates the random number object
rand.nextInt(100) - generates random integers in the 0 to 100 range)

Using a ListItreator output the contents of the LinkedList in the original order.

Using a ListItreator output the contents of the LinkedList in the reverse order.

Solutions

Expert Solution

Note: Variable names and object names are used arbitrarily. These can be changed according to the user's choice. Please refer to code snippets for a better understanding of comments and indentations.

IDE Used: Eclipse

Program Code

import java.util.*;
import java.security.SecureRandom;

public class randomlist {

   public static void main(String[] args) {
      
       // iterator variable for inserting 25 random integer
       int itr;
      
       // create an object of linkedlist class
       LinkedList<Integer> rand_list = new LinkedList<Integer>();
      
       // create an object of secure random class to generate random number
       SecureRandom sec_rand = new SecureRandom();
      
       // generate secure random number of range 0 to 100 and insert in the linked list
       for(itr= 0; itr < 25; itr++)
       {
           rand_list.add(sec_rand.nextInt(100));
          
       }
      
       // output the content of the linked list in original order using iterator
       System.out.println("Content of LinkedList in original order");
       Iterator<Integer> o_itr=rand_list.iterator();
while(o_itr.hasNext())
{
System.out.print(o_itr.next()+" ");
}
  
// output the content of the linked list in original order using iterator
System.out.println("\n\nContent of LinkedList in reverse order");
Iterator<Integer> r_itr=rand_list.descendingIterator();
while(r_itr.hasNext())
{
System.out.print(r_itr.next()+" ");
}

   }

}

Code Snippets

Sample Output


Related Solutions

Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the reverse order. Using a ListItreator output the contents of the LinkedList in the original order.
Write a java program that inserts 25 random integers from 0 to 100 in order into...
Write a java program that inserts 25 random integers from 0 to 100 in order into a LinkedList object. The program should sort the elements, then calculate the sum of the elements and the floating-point average of the elements.
Written in JAVA Code Write a program that inserts 25 random integers from 0 to 100...
Written in JAVA Code Write a program that inserts 25 random integers from 0 to 100 in order into a LinkedList object. The program should sort the elements, then calculate the sum of the elements and the floating-point average of the elements.
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
java please 1. Write a Java program to generate random numbers in the following range a....
java please 1. Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 2. Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤...
Write a Java program with comments that randomly generates an array of 500,000 integers between 0...
Write a Java program with comments that randomly generates an array of 500,000 integers between 0 and 499,999, and then prompts the user for a search key value. Estimate the execution time of invoking the linearSearch method in Listing A below. Sort the array and estimate the execution time of invoking the binarySearch method in Listing B below. You can use the following code template to obtain the execution time: long startTime = System.currentTimeMillis(); perform the task; long endTime =...
Write a Java program to generate random numbers in the following range a. 1 <=n <=...
Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 JAVA PROGRAMMING
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1 to 10 then subtract the second integer from the first integer. Note, if the second integer is greater than the first integer, swap the two integers before making the subtraction.Then prompt user for the answer after the subtraction. If the answer is correct, display “Correct”otherwise display “Wrong”.You will need to do this in a loop FIVE times and keep a count of how many...
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps:...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT