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 reverse order.

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

Solutions

Expert Solution


import java.security.SecureRandom;
import java.util.LinkedList;
import java.util.ListIterator;

class Main {
    public static void main(String[] args) {
        SecureRandom rand = new SecureRandom(); //creates the random number object
        LinkedList<Integer> list = new LinkedList<>(); // creates the linked list
        // loop to add integers to linked list
        for(int i=0; i<25; i++){
            int number = rand.nextInt(100); //generates random integers in the 0 to 100 range
            list.add(number);               // adds number to list
        }

        ListIterator<Integer> itr = list.listIterator(list.size()); // creates ListIterator to iterate list
        System.out.println("List in reverse order:");
        // hasPrevious() returns true if the list has previous element
        while (itr.hasPrevious()) {
            System.out.println(itr.previous());
        }
      
        System.out.println("\nList in original order:");
        // hasNext() returns true if the list has next element
        while (itr.hasNext()) {
            System.out.println(itr.next());
        }
    }
}


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 original order. Using a ListItreator output the contents of the LinkedList in the reverse 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 c++ program in which for loop iterates over all integers in the range 0...
write a c++ program in which for loop iterates over all integers in the range 0 to 99 inclusive and prints out all of those numbers dividible by both 2 and 5.
The question: Write a program in Python that writes four random integers in range 1-100 on...
The question: Write a program in Python that writes four random integers in range 1-100 on a file named 'num.txt'. Write try-except block to handle at least two standard python error (any two errors). Hints: import random def main(): # Local variables # Open output file. # Write random numbers to the file. # Write it on to the file. # Close the file. # Call the main function. main() Here is my answer: please let me know if anything...
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
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 well-commented java program that demonstrates the use of loops, and generation of random integers...
Write a well-commented java program that demonstrates the use of loops, and generation of random integers (Code will be written in Dr. Java) 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 the largest number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT