1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Anni [7]
3 years ago
6

Write a program that accepts a file name from the command line, then initializes an array with test data using that text file as

an input. The file should contain floating-point numbers (use double data type). The program should also have the following methods:
* getTotal. This method should accept a one-dimensional array as its argument and return the total of the values in the array.
* getAverage. This method should accept a one-dimensional array as its argument and return the average of the values in the array.
* getHighest. This method should accept a one-dimensional array as its argument and return the highest value in the array.
* getLowest. This method should accept a one-dimensional array as its argument and return the lowest value in the array.
Computers and Technology
1 answer:
postnew [5]3 years ago
4 0

Answer:

Complete code is given below:

Explanation:

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.Arrays;

import java.util.Scanner;

public class ArrayCalcs {

private int size;

String [] parts =null;

// based on number of elements in file, array will create.

double[] arr=null;

public void readFile(String file){

try{

FileReader fr = new FileReader(file+".txt");

BufferedReader br = new BufferedReader(fr);

String line = br.readLine();

int count = 0;

while (line != null) {

parts = line.split(" ");

for( String w : parts)

{

count++;

}

line = br.readLine();

}

size=count;

arr=new double[size];

for(int i=0;i<size;i++){

arr[i]=Double.parseDouble(parts[i]);

}

// System.out.println(count);

// System.out.println(Arrays.toString(parts));

System.out.println(Arrays.toString(arr));

} catch (FileNotFoundException ex){

System.out.println("This file does not exist");

} catch (IndexOutOfBoundsException e){

System.out.println("Thie file has more than 7 lines");}

catch (IOException e) {

System.out.println(e.getMessage());

}

}

public double getTotal(){

double sum=0.0;

for(double d:arr){

sum=sum+d;

}

return sum;

}

public double getAverage(){

double avg=getTotal()/arr.length;

return avg;

}

public double getHighest(){

double high=arr[0];

for(int i=0;i<arr.length-1;i++){

if(arr[i] > high)

high = arr[i];

}

return high;

}

public double getLowest(){

double low=arr[0];

for(int i=0;i<arr.length-1;i++){

if(arr[i] < low)

low= arr[i];

}

return low;

}

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.println("Enter file name:");

String file=s.next();

ArrayCalcs ac=new ArrayCalcs();

ac.readFile(file);

System.out.println(" Highest Element: "+ac.getHighest());

System.out.println("Lowest Element: "+ac.getLowest());

System.out.println("Total: "+ac.getTotal());

System.out.println("Average: "+ac.getAverage());

}

}

You might be interested in
If the wrong server edition is installed, what command can be used to change to a different edition?​
nekit [7.7K]
Dism because it is used to change and service programs
6 0
3 years ago
Copy the skeleton of code below into your answer. Then in the space indicated, add your own code to prompt the user for two numb
ahrayia [7]

Answer:

import java.util.*;

public class Main

{

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

   

 System.out.print("Enter the first: ");

 int first = input.nextInt();

 System.out.print("Enter the second: ");

 int second = input.nextInt();

 input.nextLine();

 System.out.print("Enter your name: ");

 String name = input.nextLine();

 

 int difference = Math.abs(second - first);

 

 System.out.println(name + ", the difference is " + difference);

}

}

Explanation:

*The code is in Java.

Create a Scanner object to get input from the user

Ask the user to enter the first, second, and name

Calculate the difference, subtract the second from the first and then get the absolute value of the result by using Math.abs() method

Print the name and the difference as in required format

7 0
2 years ago
If you write a toString method to display the contents of an object, object1, for a class, Class1, then the following two statem
Solnce55 [7]

Answer:

The answer is "Option A".

Explanation:

In the given question the method toString() is used that converts any value into a string. In this code two print method is used, that can be described as follows:

  • In the first method, object1 is created, which is used to print all object values.
  • In the second method, object1 uses the toString() method, which converts all values into a string, that's why the answer to this question is "True".

6 0
3 years ago
For what reason can security risks never be fully eliminated?​
raketka [301]

Answer:

A vulnerability level of ZERO can never be obtained since all countermeasures have vulnerabilities themselves. For this reason, vulnerability can never be zero, and thus risk can never be totally eliminated.

Explanation:

also we need to be protected from scams and stuff

7 0
3 years ago
What is computer hadware​
Alex Ar [27]

Answer: like the monitor or the keyboard

Explanation: it is true i even looked it up and it was true

8 0
2 years ago
Other questions:
  • What type of product does amd manufacture
    5·1 answer
  • Employers will check you out on social media sites like Facebook, MySpace, and Twitter.
    6·2 answers
  • A vehicle fails an HC emission test at idle and 2,500 rpm, and the engine has an acceleration stumble. The heated oxygen sensor
    15·1 answer
  • If metal shims are used for alignment adjustment in the front, they adjust ________.
    5·1 answer
  • In the URL, what is the subdomain and what is the domain name?
    5·1 answer
  • When you use the 3 bits 100 what color will it produce?<br><br> Computer Science
    11·1 answer
  • In cell K8, create a formula using the SUM function that calculates the total of range D17:D20 and subtracts it from the value i
    7·1 answer
  • Create an application that determines the final cost of food items and non-food items, assuming only non-food items are taxed. T
    8·1 answer
  • Which language should you use to add functionality to web pages.
    10·1 answer
  • If 200.123.4.6 is a classful internet protocol (ip) address, what class is it in?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!