Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write Pseudocode in java language based off this code public class ReadAgesFromF

ID: 3806148 • Letter: W

Question

Write Pseudocode in java language based off this code

public class ReadAgesFromFile {
public static void main(String[] args) throws FileNotFoundException {
int ages[] = new int[10];

File myFile = new File("ages.txt");
Scanner input = new Scanner(myFile);
int i = 0;
while (input.hasNext()) {
ages[i] = input.nextInt();
i++;
}

System.out.println("The ages in the file are:");
printArray(ages);

System.out.println(" The ages after sorting are:");

Sort1(ages);
printArray(ages);

// declare a two dimensional array
int[][] myArray = new int[5][5];
int count = 1;

// loop through the rows
for (int rows = 0; rows < myArray.length; rows++) {
for (int col = 0; col < myArray[0].length; col++) {
// store the integer in the array
myArray[rows][col] = count;
count++;
}
}
// Printing the two dimensional array using printArray2 method
System.out.println(" The array in matrix form is:");
printArray2(myArray);

// printing the diagonals using printArray3 method
System.out.println(" Diagonal elements are:");
printArray3(myArray);
}

// method that takes an array as parameter and prints it
public static void printArray(int[] ages) {
// loop through the array
for (int i = 0; i < ages.length; i++) {
if (i == 0) {
System.out.print(ages[i]);
} else {
System.out.print("," + ages[i]);
}
}
}

// method that takes an array and sorts it
public static void Sort1(int[] ages) {
int temp;
for (int i = 0; i < ages.length; i++) {
for (int j = 1; j < (ages.length - i); j++) {
// compares element at j with element at j-1
if (ages[j - 1] > ages[j]) {
// swaping elements
temp = ages[j - 1];
ages[j - 1] = ages[j];
ages[j] = temp;
}
}
}
}

// method that takes 2D array as parameter and print it
public static void printArray2(int[][] myArray) {
for (int rows = 0; rows < myArray.length; rows++) {
for (int col = 0; col < myArray[0].length; col++) {
// prints each row
System.out.print(myArray[rows][col] + " ");
}
System.out.println();
}
// Split line after each row
}

// method that take a 2D array as parameter and prints its diagonal elements
public static void printArray3(int[][] myArray) {
for (int i = 0; i < myArray.length; i++) {
for (int j = 0; j <= myArray[0].length; j++) {
if (i != j)
System.out.print("");
else
System.out.print(myArray[i][j] + " ");
}
System.out.println();
}

for (int i = 0; i <= myArray.length; i++) {
for (int j = 0; j <= myArray[0].length; j++) {
if (j != myArray[0].length - 1 - i)
System.out.print("");
else
System.out.print(myArray[i][myArray[0].length - 1 - i] + " ");
}
}

Explanation / Answer

Pseudocode to the above problem statement:

Class ReadAgesFromFile
Step 01: Main file throwing exception
Step 02:Construct input file ages.txt
Step 03: Take the user input through scanner and loop the file by adding the user inputs
Step 04: Print the ages in the file
Step 05: Sort and print the result
Step 06: Declare two-dimensional array and store the result in the array using for loop
Step 07: Print the results stored in the two-dimensional array by invoking
the method printArray and passing user input as the parameter

Step 08: Sort1 method is used to swap the data using the temp variable

Step 09: PrintArray2 method will print each row of record using for loop

Step 10: PrintArray3 method takes the input of the two-dimensional array list and doing
that it will loop the array list to print diagonal elements


P.S: Wherever we use two consecutive loops that are forLoop
OuterLoop is executed last and Inner Loop is executed first

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote