In: Computer Science
Im working on modifying my current code to function with these changes below
Create a Java program that meets the following criteria
My current code is show as below, i was wondering how i would go about doing that im a little lost.
import java.io.File;
import java.util.Scanner;
public class Lab5 {
public static void main(String[] args) {
String inFileName;
if (args.length > 1) {
inFileName = args[0];
} else {
Scanner console = new Scanner(System.in);
System.out.println("Enter a Filename: ");
inFileName = console.nextLine();
}
double[] myData = readArray(inFileName);
double[] results = processArray(myData);
// TODO: Display Results...
}
static double[] readArray(String filename) {
File inFile = new File(filename);
if (inFile.canRead()) {
try {
Scanner fileScanner = new Scanner(inFile);
int count = fileScanner.nextInt();
double[] data = new double[count];
for (int i = 0; i < count; i++) {
data[i] = fileScanner.nextDouble();
}
return data;
} catch (Exception e) {
return new double[0];
}
} else {
System.out.println("Can't read file.");
return new double[0];
}
}
static double[] processArray(double[] dataArray) {
// TODO: Implement Functionality from Requirements.
for (double data : dataArray) {
System.out.println(data);
}
return new double[0];
}
}
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// numbers.txt
10 34.45 56.67 32.12 34.45 56.67 89.87 76.65 55.33 22.34 12.23
__________________
// Lab5.java
import java.io.File;
import java.util.Scanner;
public class Lab5 {
public static void main(String[] args) {
String inFileName;
if (args.length > 1) {
inFileName = args[0];
} else {
Scanner console = new Scanner(System.in);
System.out.println("Enter a Filename: ");
inFileName = console.nextLine();
}
double[] myData = readArray(inFileName);
double[] results = processArray(myData);
// TODO: Display Results...
displauArray(results);
}
private static void displauArray(double[] results) {
System.out.println("Smallest =
"+results[0]);
System.out.println("Largest =
"+results[1]);
System.out.println("Average =
"+results[2]);
System.out.println("Sum =
"+results[3]);
System.out.println("Count of Larger
than Average = "+results[4]);
System.out.println("Largest
Difference = "+results[5]);
}
static double[] readArray(String filename) {
File inFile = new File(filename);
double[] data=null;
if (inFile.canRead()) {
try {
Scanner fileScanner = new Scanner(inFile);
int count = fileScanner.nextInt();
data = new double[count];
for (int i = 0; i < count; i++) {
data[i] = fileScanner.nextDouble();
}
fileScanner.close();
} catch (Exception e) {
return new double[0];
}
} else {
System.out.println("Can't read file.");
return new double[0];
}
return data;
}
static double[] processArray(double[] dataArray) {
double min=dataArray[0],max=dataArray[0];
double sum=0.0,avg=0.0,diff=0.0;
int aboveAvgCnt=0;
double largestDiff=Math.abs(dataArray[0]-dataArray[1]);
for(int i=0;i<dataArray.length;i++)
{
if(min>dataArray[i])
{
min=dataArray[i];
}
if(max<dataArray[i])
{
max=dataArray[i];
}
sum+=dataArray[i];
}
avg=sum/dataArray.length;
for(int i=0;i<dataArray.length;i++)
{
if(dataArray[i]>avg)
{
aboveAvgCnt++;
}
}
for(int i=0;i<dataArray.length-1;i++)
{
diff=Math.abs(dataArray[i]-dataArray[i+1]);
if(largestDiff<diff)
{
largestDiff=diff;
}
}
double result[]={min,max,avg,sum,aboveAvgCnt,largestDiff};
return result;
}
}
____________________________
Output:
Enter a Filename:
numbers.txt
Smallest = 12.23
Largest = 89.87
Average = 47.077999999999996
Sum = 470.78
Count of Larger than Average = 5.0
Largest Difference = 33.2
_______________Could you plz rate me well.Thank You