In: Computer Science
In java
Step 1) Assume that you have tracked monthly BSIT inquires for 10 months. Save the following data into a text file in notepad in the order listed 40 22 17 35 28 17 22 28 25 19
Step 2) Write a java program that reads the monthly BSIT inquires for 10 months from the text file that you created in step 1. Read the monthly inquires from the input file into an array in one execution of the program. Write a sort method to sort the inquiry values lowest to highest. Write a method to calculate the average of the monthly inquiries. Write a method to determine the maximum value. Your program should call the sort, average and maximum methods. Create an output file to which you write the sorted array output, the average value and maximum value.
Please let me know if anything is required
Step1:
Copyable code:
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.util.*;
class Main
{
public static void main(String[] args) throws IOException
{
String filename;
System.out.print("Enter the file name to write the monthly BSIT
inquires: ");//filename input from the iser
Scanner sc = new Scanner(System.in);
filename=sc.next();
FileWriter fw=new FileWriter(filename); //to write on the
file
System.out.println("Enters the BSIT inquires: ");
for (int i = 0; i < 10; i++)
{
int t;
t=sc.nextInt();//taking monthly inquires from the user
fw.write(t+" "); //Writing 10 inquires into the file
}
System.out.println("BSIT inquires written to the file
successfully");
//close the file
fw.close();
}
}
Sample output:
Step2:
Copyable code:
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.util.*;
class Main
{
public static void main(String[] args) throws IOException
{
String filename;
System.out.print("Enter the file name to write the monthly BSIT
inquires: ");//filename input from the iser
Scanner sc = new Scanner(System.in);
filename=sc.next();
FileWriter fw=new FileWriter(filename); //to write on the
file
System.out.println("Enters the BSIT inquires: ");
for (int i = 0; i < 10; i++)
{
int t;
t=sc.nextInt();//taking monthly inquires from the user
fw.write(t+" "); //Writing 10 inquires into the file
}
System.out.println("BSIT inquires written to the file
successfully");
//close the file
fw.close();
Main obj = new Main();
int array[] = new int[10];
//method to load data to array
array = obj.readData(filename);
System.out.println("The data loaded from the file: ");
for(int i=0;i<10;i++)
{
System.out.print(array[i]+" ");
}
//method to sort array
array = obj.sort(array,10);
System.out.println("\nThe data after sorting: ");
for(int i=0;i<10;i++)
{
System.out.print(array[i]+" ");
}
//method to find the average
float average = obj.average(array,10);
System.out.println("\nThe Average of BSIT inquires is :
"+average);
//method to find the maximum
int maximum = obj.max(array,10);
System.out.println("The maximum of BSIT inquires is :
"+maximum);
}
//loading the data from the file
int [] readData(String filename)throws IOException
{
int array[] = new int[10];
int number;
Scanner scanner = new Scanner(new File(filename));
int i=0;
while(scanner.hasNextInt())
{
array[i++] = scanner.nextInt();
}
return array;
}
//sorting the array
int [] sort(int array[],int size)
{
for(int i=0;i<size;i++)
{
int minimum_index = i;
for (int j = i+1; j < size; j++)
{
if (array[minimum_index] > array[j]) //checking for the minimum
value index
{
minimum_index = j;
}
}
//swapping the minimum value to the left side of the array
int temp=array[i];
array[i]=array[minimum_index];
array[minimum_index]=temp;
}
return array;
}
//returning the average
float average(int array[],int size)
{
float avg=0;
for(int i=0;i<size;i++)
{
avg+=array[i];
}
return avg/(size);//returning the average
}
//returning the maximum
int max(int array[],int size)
{
return array[size-1];
}
}
Sample output: