Question

In: Computer Science

(8 marks) Write a program to ask user to enter an integer that represents the number...

  1. Write a program to ask user to enter an integer that represents the number of elements, then generate an ArrayList containing elements which are all random integers in range [75, 144] , and finally display index and value of each element.

REQUIREMENTS

  • The user input is always correct (input verification is not required).
  • Your code must use ArrayList.
  • Your program must use only printf(…) statements to adjust the alignment of your output.
  • Your code must display the index in descending order.
  • Your output must be displayed with the same alignment as the example (the text in bold indicates the user input).

Example of the program output:

Example 1:

Enter the number of elements: 5

Index   Element

4       129   

3       138   

2       134   

1       98    

0       84

Example 2:

Enter the number of elements: 12

Index   Element

11      109   

10      80    

9       122   

8       84    

7       142   

6       114   

5       114   

4       111   

3       82    

2       91    

1       132   

0       113

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class RandomList {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter the number of elements: ");
       int n = sc.nextInt();
       Random r = new Random();
       ArrayList<Integer>list = new ArrayList<>();
       for(int i=0;i<n;i++)
           list.add(r.nextInt(200));
       System.out.println("Index\tElement");
       for(int i=list.size()-1;i>=0;i--)
           System.out.println(i+"\t"+list.get(i));
   }
}


Related Solutions

Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
Write a mips assembly code program that ask the user to enter an integer value, and...
Write a mips assembly code program that ask the user to enter an integer value, and then print the result of doubling that number.
Write the following program in Java. Ask the user to enter the number of rows and...
Write the following program in Java. Ask the user to enter the number of rows and columns for a 2-D array. Create the array and fill it with random integers using (int)(Math.random() * 20) (or whatever number you choose). Then, provide the user with several menu choices. 1. See the entire array. 2. See a specific row. 3. See a specific column. 4. See a specific value. 5. Calculate the average of each row. 6. Calculate the total of each...
Write a python program that will ask the user to enter any number of days. The...
Write a python program that will ask the user to enter any number of days. The program reads the number of days from the user, and returns how many years, months, and days are there in the given number of days. Your program should prompt the user to: Enter the number of days : 1152 The user types number of days (1152 above, for example) and hit enter key. The program then will print: 3 years 1 months 27 days...
Please code C# 8. Write a program that prompts the user to enter an integer. The...
Please code C# 8. Write a program that prompts the user to enter an integer. The program then determines and displays the following: Whether the integer is divisible by 5 and 6 Whether the integer is divisible by 5 or 6
Write, save, and run a Python program Ask the user to enter a number of grams....
Write, save, and run a Python program Ask the user to enter a number of grams. Your program converts that to whole tones, then whole kilograms and whatever is left is in grams. It prints the entered grams , tones , kilograms and the remaining grams.      4 marks for i in range(0,10): print(i) 2    what is wrong in the above python code? display your explanation using print statement. mark 3    insert the following comments into your program of part...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they type DONE to quit. The program should DOUBLE each of the digits in the number and display the original entry AND the SUM of the doubled digits. Hint: Use the // and % operators to accomplish this. Print the COUNT of entries that were invalid Examples: if the user enters 93218, the sum of the doubled digits is 18+6+4+2+16 = 46. User Entry Output...
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
write a c++ program . Ask the user to enter a number less than 100. Test...
write a c++ program . Ask the user to enter a number less than 100. Test the input to make sure it is correct, and use a while loop to continuously ask the user for correct input value if they don't follow the input rule. After receiving the correct input, test the number to see if it is even or odd. If it is odd, use a while loop to do the following: Output to the monitor all odd numbers...
Write a C++ program that keeps asking user to enter a positive integer number until a...
Write a C++ program that keeps asking user to enter a positive integer number until a sentinel value (999) is entered. Then for each positive number entered by the user, find out how many digits it consists. (Hint: divide a number by 10 will remove one digit from the number. You can count how many divisions it needs to bring the number to 0.) An example of executing such a program is shown below. Note that the user input is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT