Using the IST Linux system create the following Java command line array applicat
ID: 3871805 • Letter: U
Question
Using the IST Linux system create the following Java command line array application in your home area Lab5.java.
------- Step 1: Array of Integers, Reverse Sort, Print elements values using for loop -------
Declare an array of integers named intArray and allocate memory for 5 integer types
Initialize the array elements with integer values (100, 1000, 23, 555, 356) using the Integer type not int.
Reverse sort the array intArray elements and print each array element out using iteration (loop)
You will need to import the Collections class and Arrays class to perform the reverse sort.
------- Step 2: Array of chars, Sort, Print elements values using a for loop -------
Declare an array of chars named charArray and allocate memory for 5 char types
Initialize the array elements with char values (characters G, E, Q, A, Z)
Sort the array charArray elements and print each element out using iteration (loop)
------ Step 3: Array of floats, Sort, Print elements values using a for loop -------
Declare an array of floats named floatArray and allocate memory for 5 floats
Initialize the array elements with float values (10.0, 32.5, 22.2, 99.1, 90.5)
Sort the array floatArray elements and print each element out using iteration (loop)
------ Step 4: Multidimensional array, Print elements values using a for loop -------
Declare a multidimensional array of integers named multiArray and allocate memory for 3 X 5 integers
Initialize the array elements with int values (row 1 = 1-5, row 2 = 11-15, row 3 = 21-25)
Print each row element out using a nested loop
3 1000 356 100 23 10.0 22.2 32.5 90.5 99.1 12 13 14 15 21 2.3 24 25Explanation / Answer
import java.util.Arrays;
import java.util.Collections;
class SortArrayExample {
public static void main(String[] args) {
// int Array
Integer[] intArray = new Integer[] {
new Integer(100),
new Integer(1000),
new Integer(23),
new Integer(555),
new Integer(356)
};
// Sorting int Array in descending order
Arrays.sort(intArray, Collections.reverseOrder());
// Displaying elements of int Array
for (int i = 0; i < intArray.length; i++)
System.out.println(intArray[i]);
//Character Array
char[] charArray ={ 'G', 'E', 'Q', 'A', 'Z' };
Arrays.sort(charArray);
for(int i=0;i<charArray.length;i++)
System.out.println(charArray[i]);
//Float Array
// float[] floatArray={10.0,32.5,22.2,99.1,90.5};
Float[] floatArray= new Float[]{
new Float(10.0),
new Float(32.5),
new Float(22.2),
new Float(99.1),
new Float(90.5),
};
Arrays.sort(floatArray);
for(int i=0;i<floatArray.length;i++)
System.out.println(floatArray[i]);
//Multidimensional Array
int[][] multiArray = { { 1, 2,3,4,5}, {11,12,13,14,15 } , {21,22,23,24,25}};
for(int i = 0; i < multiArray.length; i++) {
for (int j = 0; j < multiArray[i].length; j++) {
System.out.println(multiArray[i][j]);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.