rogram71.java Write a program that creates a 32-element array of random integers all from 1 to 100, inclusive. Then execute a custom method with the array as its only argument. The method should begin by using a loop to print the array elements in 4 rows and 8 columns, with all columns being five characters wide. Duplicates are okay. This same loop should determine how many integers in the array are odd numbers. After this loop ends, use this figure to declare a new array of integers named odds. Process the array again in the method, but this time with a foreach loop, and add the odd integers to the odds array. Return the odds array back to main. In main, report the size of the array and sort it ascendi
Program71.java
Write a program that creates a 32-element array of random integers all from 1 to 100, inclusive. Then execute a custom method with the array as its only argument. The method should begin by using a loop to print the array elements in 4 rows and 8 columns, with all columns being five characters wide. Duplicates are okay. This same loop should determine how many integers in the array are odd numbers. After this loop ends, use this figure to declare a new array of integers named odds. Process the array again in the method, but this time with a foreach loop, and add the odd integers to the odds array. Return the odds array back to main. In main, report the size of the array and sort it ascending. Finally use a while loop to print the odd integers all on one line separated by spaces.
CODE - import java.util.Random; public class freezing { public static void main(String[] args) { // Create array of 30 integers int []temperature = new int[30]; // Create a Random object Random rand = new Random(); // Fill the array with random temperatures for (int i=0; i<30; i++) temperature[i] = rand.nextInt(66) + 10; // Call function to display the array of temperatures and get an array of freezing temperatures int []freezingTemperature = freezingTemp(temperature); // Get the number of freezing temperatures int count = freezingTemperature.length; // Sort the array for (int i=0; i<count; i++) { for(int j=1; j<count-i; j++) if (freezingTemperature[j] < freezingTemperature[j-1]) { int temp = freezingTemperature[j-1]; freezingTemperature[j-1] = freezingTemperature[j]; freezingTemperature[j] = temp; } } // Display the sorted array and the length System.out.println(count + " temperatures were below freezing point: "); for (int i=0; i<count; i++) System.out.print(freezingTemperature[i] + " "); } public static int[] freezingTemp(int[] temperature) { // Initialize number of freezing temperatures to 0 int countFreezingTemp = 0; // Loop to display the array and determine the number of freezing temperatures for(int i=0; i<30; i++) { if (i%5 == 0) System.out.println("\n"); System.out.printf("%7d ", temperature[i]); if (temperature[i] <= 32) countFreezingTemp++; } System.out.println(); // Create array for freezing temperatures int[] freezingTemperature = new int[countFreezingTemp]; // Fill the array with freezing temperatures int j = 0; for (int i=0; i<30; i++) { if (temperature[i] < 32) { freezingTemperature[j] = temperature[i]; j++; } } // return the array return freezingTemperature; } }
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images