2

So I am very new to learning java and I have a sub class which contains the main and a parent class which does all the calculations. I am having problems on how to pass the elements of an array from the main class to the parent class.

Here is my code. Please help me with how to instantiate the array.

import java.util.Scanner;

public class GreatestLeastAverageApp {

  public GreatestLeastAverageApp() {
    // TODO Auto-generated constructor stub
  }

  public static void main(String[] args) {

    GreatestLeastAverage number = new GreatestLeastAverage();

    int[] x = {0};
    int a = 0, b = 0;
    x = new int[a]; 
    {
      for (int i = 0; i <= a; i++) 
      {
        for (int j = 0; j <= a; j++)
          GreatestLeastAverage.a[i] = x[j]; // loop to pass  the values of the array elements to the parent class from the sub class. 
      }
    }


    Scanner keyboard = new Scanner(System.in); // for the input from the user 
    System.out.println("Enter the number of elements:");
    a = keyboard.nextInt(); // enter the number of integers to be entered so loop can run accordingly
    System.out.println("Enter a set of integers( Enter -99 to exit):");
    do // do loop so the user can input the variables until one of the variable is =-99. 
    {
      {
        for (b = 0; b <= a; b++)
          x[b] = keyboard.nextInt();
      }
    } while (x[b] != -99);

    //GreatestLeastAverage number = new GreatestLeastAverage(); // object made for parent class. 
    System.out.println("The Greatest Number is" + number.computegreatest()); // output line. 
    System.out.println("The Smallest Number is" + number.computeleast());
    System.out.println("The average is " + number.computeaverage());
    keyboard.close();
  }

}

public class GreatestLeastAverage {

  static int x; //variables declared for input in super class. 
  public static int[] a; // static variable to access in both classes.    
  int temp;
  int temp1;
  int temp2;
  int average;

  public int greatestleastaverage(int d) {
    d = x;
    return x;
  }

  public static int getarray(int[] b) {
    for (int i = 0; i <= x; i++) {
      for (int j = 0; j <= x; j++) {
        a[i] = b[j];
      }
    }
    return b[];
  }

I know you can't return elements of an array but I really need help.

barbsan
  • 3,418
  • 11
  • 21
  • 28
  • java: sub class is a class that `extends` the parent class - you are not doing it, there is no parent class in that code – user85421 Jul 04 '19 at 04:55
  • As Carlos said, you do not have a subclass, but you do have an inner class. Just to help with the java terminology. – theawesometrey Jul 04 '19 at 04:58
  • Side note: in some parts you have constructs like `{ for(i=0;i – barbsan Jul 04 '19 at 06:38
  • Just to clarify: There are no subclasses, and no inner classes in this code. There are two classes on the same level. – TeWu Jul 04 '19 at 08:25

2 Answers2

1

Use Lists. They're generally more flexible, and if you use ArrayList properly, it can be virtually just as performant as an array.

public class GreatestLeastAverage {
    // ... other private members
    private List<Integer> a = new ArrayList<Integer>();

    public List<Integer> getA() {
        return a;
    }
}

This lets you do code like:

GreatestLeastAverage gla = new GreatestLeastAverage();
for (int b = 0; b <= a; b++) {
    gla.getA().add(keyboard.nextInt());
}

If you should require that data for other uses, it's in gla already! Simply reuse it.

for(int a : gla.getA()) {
    // Do something with a
}

I'm sure you used static members because you didn't know how to make it work otherwise, but just know that using static members is generally discouraged unless they're final (constant).

Neil
  • 5,762
  • 24
  • 36
0

You shouldn't pass data (array) to methods in GreatestLeastAverage class by setting the value of the static field (public static int[] a;). Instead you should pass data to methods as theirs arguments. Your program should look like this:

import java.util.Scanner;

public class GreatestLeastAverageApp  {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter the number of elements:");
        int numOfElems = input.nextInt();
        int[] numbers = new int[numOfElems];
        System.out.println("Enter a set of integers:");
        for (int i = 0; i < numOfElems; i++) {
            numbers[i] = input.nextInt();
        }

        System.out.println("Statistics:");
        System.out.println("  min: " + GreatestLeastAverage.findMin(numbers));
        System.out.println("  max: " + GreatestLeastAverage.findMax(numbers));
        System.out.println("  average: " + GreatestLeastAverage.findAverage(numbers));
    }
}


class GreatestLeastAverage {

    public static int findMin(int[] numbers) {
        int candidate = numbers[0];
        for (int num : numbers) {
            if (num < candidate) candidate = num;
        }
        return candidate;
    }

    public static int findMax(int[] numbers) {
        int candidate = numbers[0];
        for (int num : numbers) {
            if (num > candidate) candidate = num;
        }
        return candidate;
    }

    public static double findAverage(int[] numbers) {
        int sum = 0;
        for (int num : numbers) {
            sum += num;
        }
        return sum / (double) numbers.length;
    }

}

To make this code even better:

  • The min/max/average calculations above are not great, e.g. If you pass empty array to findAverage, then numbers.length will be 0 and you'll divide by 0. You should make those calculations better, see: #1, #2, #3.
  • Consider using ArrayList instead of array, when you need to dynamically change the size of the list during runtime (See Neil's answer).
TeWu
  • 5,928
  • 2
  • 22
  • 36