0

OK, I attempted to understand your answers and have changed my code, but I am still running into similar problems. I am attempting to call the IntArray constructor into my IntMath constructor, as there are variables I need to get my math to work. After which, I am attempting to call both constructors into my Main class. However, it is telling my that all my variables "might not have been initialized. I am so very new at this and just feel dumb and helpless. And I have used Google like crazy. Thank you in advance for your help.

package com.company;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        //int sum, even, odd, min, max, size;
        //int [] intArray;
        //double average;
        //int even = 0, odd = 0, min = 0, max = 0, size = 0, sum = 0;
        //double average;
        System.out.print("Please enter a number for the array size (zero or greater):  ");
        int n=in.nextInt();
        if (n<0){
            System.out.println("ERROR: Number must be at least zero!!!");
        }else {
            IntArray mainArray = new IntArray(n);
        }
        //com.company.IntMath intMath = new com.company.IntMath(average, even, odd, min, max, size, sum);
        IntMath intMath = new IntMath();
        //intMath.getAverage();
        intMath.getEven();
        intMath.getOdd();
        intMath.getMin();
       // intMath.getMax();
        intMath.getSize();
        intMath.getSum();

        System.out.println("Average: " + intMath.getAverage() );  //print the average of the elements
        System.out.println("Even Count: " + intMath.getEven()); //prints the count of all even numbers
        System.out.println("Odd Count: " + intMath.getOdd()); //prints the count of all odd numbers
        System.out.println("Min: " + intMath.getMin()); //prints the min number
        //System.out.println("Max: " + intMath.getMax()); //prints the max number
        System.out.println("Size: " + intMath.getSize()); //prints the size of the array
        System.out.println("Sum: " + intMath.getSum()); //prints the sum of all elements

    }
}
package com.company;

import java.util.Arrays;
import java.util.Random;

public class IntArray {
    private int n;
    private int [] intArray;
    private double average;
    private int sum;
    private int even;
    private int odd;
    private int max;
    private int min;
    private int size;

    public IntArray(int n) {
        this.n = n;

        Random randArray = new Random();
        int[] intArray = new int[n];
        intArray[0] = 0; //why does this not make the element 0 a 0?????????
        for (int i = 0; i < n; i++) {
            intArray[i] = randArray.nextInt(50); //I was getting very big random numbers, so I set the max to 50
        }
        System.out.println("Set: " + Arrays.toString(intArray));


    }
    public double getAverage() {
        average = 0;
        double total = 0;
        for (int i = 0; i < n; i++) {
            total = total + intArray[i];
        }
        return average;
    }

    public int getSum() {
        //find the sum of all elements
        sum = 0;
        for (int i = 0; i < n; i++) {
            sum += intArray[i];
        }
        return sum;
    }

    public int getEven() {
        even = 0;
        for (int i = 0; i < n; i++) {
            if (intArray[i] % 2 == 0) {
                even++;
            }
        }
        return even;
    }

    public int getOdd() {
        odd = 0;
        for (int i = 0; i < n; i++) {
            if (intArray[i] % 2 != 0) {
                odd++;
            }
        }
        return odd;
    }

    public int getMax() {
        max = intArray[0];
        for (int i = 1; i < n; i++) {
            if (intArray[i] > max) {
                max = intArray[i];
            }
        }
        return max;
    }

    public int getMin() {
        min = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < intArray.length; j++) {
                if (intArray[i] > intArray[j]) {
                    min = intArray[i];
                    intArray[i] = intArray[j];
                    intArray[j] = min;
                }
            }
        }
        return min;
    }

    public int getSize() {

        //find the size of the array
        size = n + 1;
        return size;
    }

}
package com.company;
public class IntMath {
    private double average;
    private int sum;
    private int even;
    private int odd;
    private int max;
    private int min;
    private int size;
    private int[] intArray;
    private int n;

    //com.company.IntArray mainArray = new com.company.IntArray(n); //call IntArray to get variables ''n'' and ''intArray''


    /*public IntMath(double average, int sum, int even, int odd, int max, int min, int size, int[] intArray) {
        this.average = average;
        this.sum = sum;
        this.even = even;
        this.odd = odd;
        this.max = max;
        this.min = min;
        this.size = size;
        this.intArray = intArray;

        com.company.IntArray mainArray = new com.company.IntArray(n); //call IntArray to get variables ''n'' and ''intArray''

        // average = 0;
        // double total = 0;
        // for (int i = 0; i < n; i++) {
        //     total = total + intArray[i];
        // }


        //find the sum of all elements
        //sum = 0;
        //for (int i = 0; i < n; i++) {
        //    sum += intArray[i];
        //}

        //find the count of the even numbers
        //even = 0;
        //for (int i = 0; i < n; i++) {
        //    if (intArray[i] % 2 == 0) {
        //        even++;
        //    }
        //}

        //find the count of the odd numbers
        //odd = 0;
        //for (int i = 0; i < n; i++) {
        //    if (intArray[i] % 2 != 0) {
        //        odd++;
        //    }
        //}

        //find the biggest number
        //max = intArray[0];
        //for (int i = 1; i < n; i++) {
        //    if (intArray[i] > max) {
        //        max = intArray[i];
        //    }
        //}

        //find the smallest number
        //min = 0;
        //for (int i = 0; i < n; i++) {
        //    for (int j = i + 1; j < intArray.length; j++) {
        //        if (intArray[i] > intArray[j]) {
        //            min = intArray[i];
        //            intArray[i] = intArray[j];
        //            intArray[j] = min;
        //        }
        //    }
    /

    //find the size of the array
    //size = n + 1;
*/

    public double getAverage() {
        average = 0;
        double total = 0;
        for (int i = 0; i < n; i++) {
            total = total + intArray[i];
        }
        return average;
    }

    public int getSum() {
        //find the sum of all elements
        sum = 0;
        for (int i = 0; i < n; i++) {
           sum += intArray[i];
        }
        return sum;
    }

    public int getEven() {
        even = 0;
        for (int i = 0; i < n; i++) {
            if (intArray[i] % 2 == 0) {
                even++;
            }
        }
        return even;
    }

    public int getOdd() {
        odd = 0;
        for (int i = 0; i < n; i++) {
            if (intArray[i] % 2 != 0) {
                odd++;
            }
        }
        return odd;
    }

    public int getMax() {
        max = intArray[0];
        for (int i = 1; i < n; i++) {
            if (intArray[i] > max) {
                max = intArray[i];
            }
        }
        return max;
    }

    public int getMin() {
        min = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < intArray.length; j++) {
                if (intArray[i] > intArray[j]) {
                    min = intArray[i];
                    intArray[i] = intArray[j];
                    intArray[j] = min;
                }
            }
        }
        return min;
    }

    public int getSize() {

        //find the size of the array
        size = n + 1;
        return size;
    }
}


dreedski
  • 19
  • 5
  • 1
    Your IntArray class won't compile. You have code that isn't in any method or constructor. Are you using an ide? That would have shown you this. – NomadMaker Mar 17 '21 at 20:19
  • You have to understand tha you **cannot** call a class, you can instantiate it by creating an object out of it, and then call the **methods** of this class. A class is just like a box with attributes and actions. Attributes are the variables and actions are the methods(functions). You can also check `static` methods where you don't need an object to call them. https://stackoverflow.com/questions/66652425/when-to-decide-use-static-functions-at-java/66652634#66652634 – Renis1235 Mar 18 '21 at 08:18
  • Ok fellas, I think I have changed what you both said, however I am now getting new errors telling my I have not initialized my variables, they are all in different constructors. – dreedski Mar 18 '21 at 18:12
  • OK, I have yet again redone my code. Srikanth Chakravarthy, you are right in what I am attempting to accomplish. Now I know I have a really weird code, with the same methods in two classes, not sure how I got here. But now all of the methods return "0". – dreedski Mar 19 '21 at 22:35

1 Answers1

1

In class Main, if you initialize all the variables, that class will compile without errors.

By going through your classes, I think what you might want to do is,

  • Accept a number to create an array of that size and populate the array with some random numbers
  • Next you want to perform various operations like finding average, sum, etc by using the array that you have populated.

If this is the case, then you would have to do something like this...

  • Effectively, you need not have the IntArray class.
  • Declare int[] intArray as a class level variable in IntMath class and populate that array as you are doing it now, in the constructor of IntArray class.
  • define multiple methods in your IntMath class for each of the operations..average, sum, etc and call them in the Main class while you are printing the answer.
  • You will have to correct the code in getAverage, getSize and getMin methods.