Question 1

 

import java.io.*;

import cs181.*;

 

public class InputAndSum {

 

static int [] details = {20,30,40,45,50,65,69,70,88,92};

public static void main (String [] args) throws IOException {

Transcript.println ("Average of numbers: " + average(details));

Transcript.println ("sum of numbers above average: "+

sumAboveMark (details, average(details)));

details = readFile("details.txt");

double avOfArray = average (details);

Transcript .println("Average and sum of numbers above average in file: "+

avOfArray +" " +sumAboveMark(details,avOfArray));

}

public static double average (int [] numbers) {

int sum = 0;

for (int j=0;j<numbers.length;j++) sum = sum+numbers.length;

return (double)sum / numbers.length;

}

 

public static int sumAboveMark(int [ ] numbers, double level) {

int sum = 0;

for (int j=0;j<numbers.length;j++)

if (numbers[j]>level)sum = sum+numbers.length;

return sum;

}

public static int [] readFile (String fileName) throws IOException {

BufferedReader br = new BufferedReader(new FileReader(fileName));

String line = br.readLine().trim();

int size = Integer.parseInt(line);

int [] tmp = new int [size];

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

String number = br.readLine();

tmp[i] = Integer.parseInt(number);

}

return tmp;

}

}