Writng this coding all in one file, using Java, i seem to be stuck somewhere...
ID: 3804850 • Letter: W
Question
Writng this coding all in one file, using Java, i seem to be stuck somewhere...
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). 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. 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. 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.
Explanation / Answer
PROGRAM CODE:
package array;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ArraySample {
public static void main(String[] args) {
int ages[] = new int[10];
int counter = 0;
try {
Scanner fileReader = new Scanner(new File("input.txt"));
while(fileReader.hasNextLine())
{
ages[counter++] = Integer.valueOf(fileReader.nextLine());
}
fileReader.close();
System.out.println("Ages before sorting: ");
printArray(ages);
System.out.println(" Ages after sorting: ");
Sort1(ages);
System.out.println();
//two dimensional array
int matrix[][] = {{7,3,4,5,7},
{1,7,3,7,5},
{1,2,7,4,5},
{1,7,3,7,5},
{7,2,3,4,7}};
System.out.println("Elements of 2D array:");
printArray2(matrix);
System.out.println("Diagonal elements of 2D array:");
printArray3(matrix);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void printArray(int array[])
{
for(int i=0; i<array.length; i++)
{
System.out.print(array[i]);
if(i<array.length-1)
System.out.print(", ");
}
}
public static int[] Sort1(int array[])
{
for(int i=0; i<array.length; i++)
{
for(int j=0; j<array.length-1; j++)
{
if(array[i] < array[j])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
printArray(array);
return array;
}
public static void printArray2(int array[][])
{
for(int i=0; i<array.length; i++)
{
for(int j=0; j<array[0].length; j++)
{
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
public static void printArray3(int array[][])
{
for(int i=0; i<array.length; i++)
{
for(int j=0; j<array[0].length; j++)
{
if(i==j || (i+j) == array.length-1)
System.out.print(array[i][j] + " ");
}
}
}
}
OUTPUT:
Ages before sorting:
10, 36, 54, 13, 45, 76, 24, 23, 25, 26
Ages after sorting:
10, 13, 23, 24, 25, 26, 36, 45, 54, 76
Elements of 2D array:
7 3 4 5 7
1 7 3 7 5
1 2 7 4 5
1 7 3 7 5
7 2 3 4 7
Diagonal elements of 2D array:
7 7 7 7 7 7 7 7 7
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.