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.
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 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 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...
1. Write a program that will ask the user to enter a character and then classify...
1. Write a program that will ask the user to enter a character and then classify the character as one of the following using only IF-ELSE and logical operators. (50 points - 10pts for syntax, 10pts for commenting and 30pts for successful execution) • Integer • Lower Case Vowel • Upper Case Vowel • Lower Case Consonant • Upper Case Consonant • Special Character
8. write a program using switch-case statements that ask a user to enter a temperature value...
8. write a program using switch-case statements that ask a user to enter a temperature value in degrees Fahrenheit (In your program, use an integer to record the user’s entry. If the temperature falls between 0 and 96 make it print “Temperature below normal”. If the temperature is 97, 98, make it “Temperature normal”. If the temperature is between 100 and 150, print “You have a fever”. If the temperature is outside the ranges given above, display “Are you human”....
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT