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

import java.io.*; import java.util.ArrayList; import java.util.Scanner; // Sammy

ID: 3822482 • Letter: I

Question

import java.io.*;

import java.util.ArrayList;

import java.util.Scanner;

// Sammy Student: Data Analysis with Java File Processing

class DataAnalytics

{

public static void main(String args[])

{

// declare an object to receive the data

Scanner scan = new Scanner(System.in);

// declare an array list to hold the data

ArrayList<Integer> list;

list = new ArrayList<Integer>();

int count = 0;

int num = 0;

int val = 0;

String line = "";

try

{

// create or append to the file

FileWriter fileOut = new FileWriter("outData.txt");

BufferedWriter fout = new BufferedWriter(fileOut);

System.out.println("how many data items?");

count = scan.nextInt();

for (int i = 1; i <= count; i++)

{

System.out.println("enter a data value");

val = scan.nextInt();

fout.write(val + " ");

}

System.out.println("thank you ... the data has been recorded!");

// close the output stream objects

fout.close();

fileOut.close();

scan.close();

// read the data

FileReader fileIn = new FileReader("outData.txt");

BufferedReader fin = new BufferedReader(fileIn);

while ((line = fin.readLine()) != null)

{

num = Integer.parseInt(line);

list.add(num);

System.out.println(num);

}

System.out.println("thank you ... the data has been received!");

fin.close();

fileIn.close();

System.out.println("display unsorted data");

DisplayData(list);

System.out.println("display sorted data");

BubbleSort(list);

//change the method with bubbleSort

}

catch(Exception e)

{

// catch an exception if any arises

System.err.println("Error: " + e.getMessage());

}

}

public static void DisplayData(ArrayList<Integer> num)

{

for (int i = 0; i < num.size(); i++)

System.out.println(num.get(i).toString());

}

public static void BubbleSort(ArrayList<Integer> num)

{

int j = 0;

boolean flag = true; // set the flag to true to begin first pass

int temp = 0; // define the holding variable

while (flag)

{

flag = false; //set flag to false awaiting a possible swap

for (j = 0; j < num.size() - 1; j++)

{

if (num.get(j) > num.get(j + 1))

// for descending sort change to <

{

temp = num.get(j); //swap the elements

num.set(j, num.get(j + 1));

num.set(j + 1, temp);

flag = true; //shows a swap occurred

}

}

}

System.out.println("display sorted data");

DisplayData(num);

}

}

Modify the Program

     After testing your program that it displays the original data in an unsorted order and a sorted order, modify again the program to include each of these variations:

•           Add a new method named MaxMin() that will find the smallest and      largest value in the ArrayList after the data has been sorted.

•           Add a new method named Average() that will find the average value          in the ArrayList after the data has been sorted.

Save your program and perform a trial run of it. Test your program with data similar to that shown below.

            [ Program Output ]

                                    how many data items?

                                    5

                                    enter a data value

                                    20

                                    enter a data value

                                    50

                                    enter a data value

                                    10

                                    enter a data value

                                    60

                                    enter a data value

                                    40

                                    thank you . . . the data has been recorded!

Explanation / Answer

import java.io.*;

import java.util.ArrayList;

import java.util.Scanner;

// Sammy Student: Data Analysis with Java File Processing

class DataAnalytics

{

public static void main(String args[])

{

// declare an object to receive the data

Scanner scan = new Scanner(System.in);

// declare an array list to hold the data

ArrayList<Integer> list;

list = new ArrayList<Integer>();

int count = 0;

int num = 0;

int val = 0;

String line = "";

try

{

// create or append to the file

FileWriter fileOut = new FileWriter("outData.txt");

BufferedWriter fout = new BufferedWriter(fileOut);

System.out.println("how many data items?");

count = scan.nextInt();

for (int i = 1; i <= count; i++)

{

System.out.println("enter a data value");

val = scan.nextInt();

fout.write(val + " ");

}

System.out.println("thank you ... the data has been recorded!");

// close the output stream objects

fout.close();

fileOut.close();

scan.close();

// read the data

FileReader fileIn = new FileReader("outData.txt");

BufferedReader fin = new BufferedReader(fileIn);

while ((line = fin.readLine()) != null)

{

num = Integer.parseInt(line);

list.add(num);

System.out.println(num);

}

System.out.println("thank you ... the data has been received!");

fin.close();

fileIn.close();

System.out.println("display unsorted data");

DisplayData(list);

System.out.println("display sorted data");

BubbleSort(list);

//calling maxmin method

MaxMin(list);

//calling average method

Average(list);

//change the method with bubbleSort

}

catch(Exception e)

{

// catch an exception if any arises

System.err.println("Error: " + e.getMessage());

}

}
//method to find average of arraylist after sorting it..
public static void Average(ArrayList<Integer> num)
{
    double av=0;
    int i=0;
  
    while(i<num.size())
    {
        av=av+num.get(i);
        i++;
    }
    av=av/num.size();//calculating average
  
    System.out.println("The average is:"+av);
}

//method that finds the largest and smallest elements in array list, after sorting
public static void MaxMin(ArrayList<Integer> num)
{
    //when data is sorted...
    //first element is the smallest and last element is the maximum
  
    //printing maximum...
  
  
  
    System.out.println("The Maximum Element is:"+num.get(num.size()-1));
  
    //printing minimum
  
    System.out.println("The Minimum Element is:"+num.get(0));
  

}

public static void DisplayData(ArrayList<Integer> num)

{

for (int i = 0; i < num.size(); i++)

System.out.println(num.get(i).toString());

}

public static void BubbleSort(ArrayList<Integer> num)

{

int j = 0;

boolean flag = true; // set the flag to true to begin first pass

int temp = 0; // define the holding variable

while (flag)

{

flag = false; //set flag to false awaiting a possible swap

for (j = 0; j < num.size() - 1; j++)

{

if (num.get(j) > num.get(j + 1))

// for descending sort change to <

{

temp = num.get(j); //swap the elements

num.set(j, num.get(j + 1));

num.set(j + 1, temp);

flag = true; //shows a swap occurred

}

}

}

System.out.println("display sorted data");

DisplayData(num);

}

}

output:-

run:
how many data items?
5
enter a data value
2
enter a data value
3
enter a data value
1
enter a data value
5
enter a data value
8
thank you ... the data has been recorded!
2
3
1
5
8
thank you ... the data has been received!
display unsorted data
2
3
1
5
8
display sorted data
display sorted data
1
2
3
5
8
The Maximum Element is:8
The Minimum Element is:1
The average is:3.8
BUILD SUCCESSFUL (total time: 9 seconds)