Question

In: Computer Science

*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...

*****IN JAVA*****

Write a code snippet that initializes an array with ten random integers and then prints

the following output:

a. every element (on a single line)

b. every element at an even index (on a single line)

c. every even element (on a single line)

d. all elements in reverse order (on a single line)

e. only the first and last elements (on a single line)

Solutions

Expert Solution


import java.util.Random;

public class GenRandomInteger{
  
public static void main(String args[]){
  
//initialize an long array of length 10
long[] randInt = new long[10];
//create Random class object
Random random = new Random();
  
//iterate the loop 10 times, to generate 10 random integers
for(int i=0; i<10;i++){
//Generate Random integer
randInt[i] = random.nextInt();
}
  
//a) every element (on a single line)
for(int i = 0; i < 10; ++i)
System.out.print(randInt[i] + " ");
System.out.println();
  
//b) every element at an even index (on a single line)
for(int i = 0; i < 10; i+=2)
System.out.print(randInt[i] + " ");
System.out.println();
  
//c) every even element (on a single line)
for(int i = 0; i < 10; i++){
if (randInt[i] % 2 == 0)
System.out.print(randInt[i] + " ");
}
System.out.println();
  
//d) all elements in reverse order (on a single line)
for(int i = 9; i >= 0; --i)
System.out.print(randInt[i] + " ");
System.out.println();
  
//e) only the first and last elements (on a single line)
System.out.print(randInt[0] + " "+ randInt[9]);
System.out.println();
}
}


Related Solutions

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
Write a program that initializes an array of 6 random integers and then prints 4 lines...
Write a program that initializes an array of 6 random integers and then prints 4 lines of output, containing the following: 1. Only the first and last element 2. Every element at an odd index 3. Every odd element 4. All elements in reverse order
write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
*****IN JAVA***** A run is a sequence of adjacent repeated values. Write a code snippet that...
*****IN JAVA***** A run is a sequence of adjacent repeated values. Write a code snippet that generates a sequence of 20 random die tosses in an array and that prints the die values, marking the runs by including them in parentheses, like this: 1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 (3 3) Use the following pseudocode: inRun = false for each valid index i in the array If inRun...
Write a java code snippet to prompt the user for the number of names they’d like...
Write a java code snippet to prompt the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
Write Java program Lab43.java which declares and initializes numeric array of 5 elements (int num[5]) and...
Write Java program Lab43.java which declares and initializes numeric array of 5 elements (int num[5]) and produces as output the sum of integers in an array, the largest and the smallest element in an array. Your program should contain the following methods: public static int sum(int[]) public static int findLargest(int[]) public static int findSmallest(int[])
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 code that would allocate the space for an array of 20 integers. It will be...
Write code that would allocate the space for an array of 20 integers. It will be pointed to by a variable named "junk" Write code that puts “file did not open” into an error stream.
Write a java code snippet that allows a teacher to track her students’ grades. Use a...
Write a java code snippet that allows a teacher to track her students’ grades. Use a loop to prompt the user for each student’s name and the grade they received. Add these values to two separate parallel ArrayLists. Use a sentinel to stop. Output a table listing each of the student’s names in one column and their associated grades in the second column.
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT