Question

In: Computer Science

Im working on modifying my current code to function with these changes below Create a Java...

Im working on modifying my current code to function with these changes below

Create a Java program that meets the following criteria

  • You program should contain a “static method” double[] readArray(String filename) which accepts the name of a file to read as a String parameter.
    • This function should read all the values into a new array of “doubles”
      • NOTE: The first value of the file is an integer used to indicate the size of the array that you will need (see above)
    • The function should return the array which was created and read in this method.
  • You program should contain a function double[] processArray() (double[] dataArray) which accepts the array read by the previous function.
    • This function should scan the array and calculate the following information:
      • The smallest value in the array.
      • The largest value in the array.
      • The average value in the array.
      • The sum of the values in the array.
      • A count of the number of items that are greater than the average value
      • The largest difference between any two adjacent values!
        1. For example, using the above sample input, the largest adjacent difference is between the values 7.876 and 3.1415 (array indexes 2 and 3), which is a difference of 4.7345.
        2. (This one is not terribly difficult, but it will require some thought!)
    • The function should return a new array of six values containing the information outlined above in the order shown.
      • For example, using the sample data file above, the array would contain the following values in the following order:
        { 1.23, 7.876, 3.9817, 19.9085, 2.0, 4.7345 }
      • The order is just: smallest, largest, average, sum, above average count, largest difference
  • The main method should:
    • Check to see the user passed a filename to main itself via the (String[] args)
      • If no file name exists, the program should prompt the user to enter a source-file name.
    • Call the functions above to read the array and calculate the results.
    • Display all the final results for example:

      Smallest = 1.23
      Largest = 7.876
      Average = 3.9817
      Sum = 19.9085
      Count of Larger than Average = 2.0
      Largest Difference = 4.7345

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];
    }

}

Solutions

Expert Solution

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


Related Solutions

Im working on a project proposal. The proposal is for a walmart in my state. The...
Im working on a project proposal. The proposal is for a walmart in my state. The schedules is made and soely created by the store manager which leads to many erros and a lot of ppl not getting scheduled or getting scheduled at the wrong times. The proposal is to get a group of assistant managers and have them come together for sixty minutes a week to make a week’s worth of schedules. Each manager will be assigned an department...
only JAVA code /** Create a method as instructed below and then call it appropriately. */...
only JAVA code /** Create a method as instructed below and then call it appropriately. */ import java.util.Scanner; public class MoreBankCharges { //Constant declarations for base fee and per check fees //Class scope so constants are accessible by all methods static final double BASE_FEE = 10.0; static final double LESS_THAN_20_FEE = 0.10; static final double TWENTY_TO_THIRTYNINE_FEE = 0.08; static final double FORTY_TO_FIFTYNINE_FEE = 0.06; static final double SIXTY_OR_MORE_FEE = 0.04; public static void main(String[] args) { //Variable declarations int numChecks;...
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
The programming language that is being used here is JAVA, below I have my code that...
The programming language that is being used here is JAVA, below I have my code that is supposed to fulfill the TO-DO's of each segment. This code in particular has not passed 3 specific tests. Below the code, the tests that failed will be written in bold with what was expected and what was outputted. Please correct the mistakes that I seem to be making if you can. Thank you kindly. OverView: For this project, you will develop a game...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
im trying to write a java code that take a matrix of vector and fine it's...
im trying to write a java code that take a matrix of vector and fine it's inverse (the the inverse in linear algebra) then multiple this matrix with a vector to fine other vector (matrix)-1 × ( vector ) = (vector)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT