Write a Java program to accomplish the following tasks. Task 1: Prompt the user to enter…
- User
Write a Java program to accomplish the following tasks.
Task 1: Prompt the user to enter an integer from 1 up to 100 (including 100) The following incorrect inputs are needed to be handled and different error messages should be printed: Out of range and Not an integer If the user failed in entering a correct integer, let the user retry until a correct integer is entered Let n to be the integer of the user input, print n
Task 2: Generated n random numbers (integers) where the range of the integers is [0~20] Stored the numbers into a List in descending order (From largest to smallest) Print the List
Task 3: Use the numbers in the List as the key of an entry and use the amount of the duplication of this number as the value and add(put) the entry into a Map The keys in the Map should be stored in descending order (The same order as the List) Print the Map
- Assistant
Here is a Java program that accomplishes the given tasks:
import java.util.*; public class NumberOperations { public static void main(String[] args) { // Task 1 int n = getUserInput(); System.out.println(n); // Task 2 List<Integer> randomNumbers = generateRandomNumbers(n); Collections.sort(randomNumbers, Collections.reverseOrder()); System.out.println(randomNumbers); // Task 3 Map<Integer, Integer> numberMap = createNumberMap(randomNumbers); System.out.println(numberMap); } public static int getUserInput() { Scanner scanner = new Scanner(System.in); int n = 0; boolean validInput = false; while (!validInput) { System.out.print("Enter an integer from 1 to 100: "); String input = scanner.nextLine(); try { n = Integer.parseInt(input); if (n >= 1 && n <= 100) { validInput = true; } else { System.out.println("Out of range. Please try again."); } } catch (NumberFormatException e) { System.out.println("Not an integer. Please try again."); } } return n; } public static List<Integer> generateRandomNumbers(int n) { Random random = new Random(); List<Integer> randomNumbers = new ArrayList<>(); for (int i = 0; i < n; i++) { int randomNumber = random.nextInt(21); randomNumbers.add(randomNumber); } return randomNumbers; } public static Map<Integer, Integer> createNumberMap(List<Integer> numbers) { Map<Integer, Integer> numberMap = new LinkedHashMap<>(); for (int number : numbers) { numberMap.put(number, numberMap.getOrDefault(number, 0) + 1); } return numberMap; } }In this example, we have a
NumberOperationsclass with four methods:main,getUserInput,generateRandomNumbers, andcreateNumberMap.The
mainmethod performs the three tasks described in the problem statement. First, it calls thegetUserInputmethod to get a valid integer from the user. Then, it generatesnrandom numbers using thegenerateRandomNumbersmethod, sorts them in descending order, and prints them. Finally, it uses the generated numbers to create a map with the count of each number using thecreateNumberMapmethod, and prints the map.The
getUserInputmethod prompts the user to enter an integer between 1 and 100 (inclusive) and handles any incorrect inputs. It uses aScannerobject to read the user's input, and awhileloop to keep asking for input until a valid integer is entered.The
generateRandomNumbersmethod takes an integernas input and generatesnrandom numbers between 0 and 20 (inclusive) using theRandomclass. The numbers are stored in a list and returned.The
createNumberMapmethod takes a list of numbers as input and creates a map where the numbers are the keys and the count of each number is the value. It loops through the numbers, using thegetOrDefaultmethod to increment the count for each number. The map is then returned. Note that we use aLinkedHashMapto preserve the order of the keys in descending order as the same order as the list.Hope this helps!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.