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;
}
}