java problem

2. Within class Lab8’s main method, define four arrays named depts, oct, nov, and des, all of size 7. The first array is used to store the name of each department. The other arrays are used to store data for all 7 departments for one month. The index position in each array corresponds to the department number; for example, depts[0] contains the name of the first department, oct[0] contains sales data for the first department for the month of October, and dec[4] contains sales data for the fifth department for the month of December.

3. Within class Lab8’s main method, using the sales.txt file as input data, read the data from the file, line by line, and save each data item in its appropriate array.

Part 3.

4. Within class Lab8, define a method named double arrayAverage(double[] arr) that returns the average of all the values from an array arr.

5. Add the statements to Lab8’s main method that call the arrayAverage method in order to compute the average sales amount for all departments in the month of October. Display the average along with an explanatory text.

Part 4.

6. Define a method named avgByIndex within Lab8 that takes three double arrays and an integer number (0, 1,

2, …) as input parameters, and returns the average of the values located at the given index position in the

three arrays.

7. Add the statements in the main method that call the avgByIndex method to compute the average sales

amount in the months of October, November, and December for the department #4. Display the department

name and the computed average, along with an explanatory text.

Part 5.

8. Define a method named maxIndex that takes a double-valued array as an input and returns the index position

of the largest value in the array.

1 Answer

  • 7 years ago
    Best Answer


    import java.util.*;


    public class Y0501 {

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

    String[] depts = new String[7];

    double[] oct = new double[7], nov = new double[7], dec = new double[7];

    Scanner scan = new Scanner(new File("sales.txt"));

    for (int i = 0; i < depts.length; i++) {

    depts[i] = scan.nextLine();

    oct[i] = Double.parseDouble(scan.nextLine());

    nov[i] = Double.parseDouble(scan.nextLine());

    dec[i] = Double.parseDouble(scan.nextLine());


    System.out.println("October average sales: " + arrayAverage(oct));

    System.out.println("November average sales: " + arrayAverage(nov));

    System.out.println("December average sales: " + arrayAverage(dec));

    for (int i = 0; i < depts.length; i++) {

    System.out.println(depts[i] + " Q4 sales: " +

    avgByIndex(oct, nov, dec, i));


    int i = maxIndex(dec);

    System.out.println("Largest sales December: " +

    depts[i] + "(" + i + ") " + dec[i]);

    System.out.println("Largest sales November: " +


    System.out.println("Grand total sales for Q4: " + total(oct, nov, dec));

    System.out.println("Is October sales number sorted: " +

    (isSorted(oct) ? "Yes" : "No"));


    static double arrayAverage(double[] arr) {

    double x = 0.0D;

    for (double d : arr) x += d;

    return x / arr.length;


    static double avgByIndex(double[] a, double[] b, double[] c, int idx) {

    return (a[idx] + b[idx] + c[idx]) / 3;


    static int maxIndex(double[] a) {

    double d = Double.MIN_VALUE;

    int x = -1;

    for (int i = 0; i < a.length; i++) {

    if (a[i] > d) {

    x = i;

    d = a[i];



    return x;


    static double maxValue(double[] a) {

    double d = Double.MIN_VALUE;

    for (double x : a) {

    if (x > d) d = x;


    return d;


    static double total(double[] a, double[] b, double[] c) {

    double d = 0.0D;

    for (int i = 0; i < a.length; i++) {

    d += a[i] + b[i] + c[i];


    return d;


    static boolean isSorted(double[] a) {

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

    if (a[i] > a[i+1]) return false;


    return true;




    1. CS 1401, Fall 2012, Lab 8, Arrays


Still have questions? Get your answers by asking now.