Part 1: Write a main method that reads 10 integer values from a file and stores
ID: 3804465 • Letter: P
Question
Part 1: Write a main method that reads 10 integer values from a file and stores them in an array called ages. Write a new method called printArray that receives the ages array and prints the content of the array separated by commas (No comma at the beginning or end of the line). File Sample: 5 7 18 30 5 78 8 22 6 90 Output Sample) Ages: 5,7,18,30,5,78,8,22,6,90 Part 2) Write a method called Sort1 that will use the array ages. This method will arrange all the ages in the array from youngest to oldest and return a new array called sortedAges. Print new arranged values by calling the method printArray with parameter sortedArray. Output Sample) Ages: 5,5,6,7,8,18,22,30, 78, 90 Part 3) In the main method, create a two dimensional array of length 5 rows by 5 columns with the values shown below. Write a method called print printArray2 to print the content of the array in matrix form. Output Sample) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Part 4). Write a method called print printArray3 to print the content of the elements of the diagonal elements in the matrix above. This print method should work for any array of size n by n. Output Sample) 1 7 13 19 20 5 9 13 17 21
(Java Language)
Explanation / Answer
package org.students;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFileMatrix {
public static void main(String[] args) {
// Creating an 1-D integer array of size 10
int array1[] = new int[10];
int i = 0;
// Opening the file
try {
Scanner ifsInput = new Scanner(new File("D:\matrix.txt"));
while (ifsInput.hasNextInt()) {
array1[i] = ifsInput.nextInt();
i++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Displaying the elements in the Array
System.out.print("Ages: ");
for (i = 0; i < 10; i++) {
System.out.print(array1[i]);
if (i < 9) {
System.out.print(",");
}
}
// Calling the methods
Sort1(array1);
printArray(array1);
// Declaring and initializing two dimensional array
int arr[][] = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 },
{ 11, 12, 13, 14, 15 }, { 1, 17, 18, 19, 20 },
{ 21, 22, 23, 24, 25 } };
// Calling the method
printArray2(arr);
}
// This method is used to display the two dimensional array
private static void printArray2(int[][] Array2) {
System.out.println(" Displayig Two Dimensioanl Array");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.printf("%5d", Array2[i][j]);
}
System.out.println();
}
// calling the method to display diagonal elements
printArray3(Array2);
}
// This method will print the diagonal elements
private static void printArray3(int[][] array2) {
System.out.println("Diagonal Elements :");
for (int i = 0; i < array2.length; i++) {
for (int j = 0; j < array2[0].length; j++) {
if (i == j)
System.out.print(array2[i][j] + " ");
}
}
int max = array2.length - 1;
for (int i = 0; i < array2.length; i++) {
System.out.print(array2[i][max--] + " ");
}
}
private static void printArray(int[] sortedArray) {
System.out.println(" --- After Sorting ---");
System.out.print("Ages: ");
for (int i = 0; i < 10; i++) {
System.out.print(sortedArray[i]);
if (i < 9) {
System.out.print(",");
}
}
}
private static void Sort1(int[] array1) {
// This Logic will Sort the Array of elements in Ascending order
int temp;
for (int i = 0; i < 10; i++) {
for (int j = i + 1; j < 10; j++) {
if (array1[i] > array1[j]) {
temp = array1[i];
array1[i] = array1[j];
array1[j] = temp;
}
}
}
}
}
__________________________
Output:
Ages: 5,7,18,30,5,78,8,22,6,90
--- After Sorting ---
Ages: 5,5,6,7,8,18,22,30,78,90
Displayig Two Dimensioanl Array
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
1 17 18 19 20
21 22 23 24 25
Diagonal Elements :
1 7 13 19 25 5 9 13 17 21
____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.