These are the requirements for my assignment along with the outline of the main method and examples of the desired output. Below that is my actual code which I have written but the output doesn't match what is desired
For this assignment you will be reading in an int number that tells
you how many two-digit pseudorandom random int numbers to put into an array.
You will call a method "genRandom" to generate psuedorandom numbers and put
them into an array. The header of the genRandom method is
public static void genRandom(int num, int[] intArr, boolean DEBUG, int SIZE_ARR,
Random rand, int INT_LOWER_LIMIT, int INT_RANGE)
Once the array has been filled we will call a second method to print out the
data in the array. The header for this method is
public static void printData(int num, int[] arr)
After the entire array has been printed out, you will call a method that
computes the sum of the elements of the array. The header for this
method is
public static int sumData(int num, int[] iArr, boolean DEBUG)
After the returning from the sumData method you will need to print out the
int returned by the sumData method.
Then you will call a method that will print only the even numbers stored in
the array. The header for this method is
public static void printEven(int num, int[] iArr, boolean DEBUG)
Then you will call a method that will print out the odd numbers stored in the
array. The header for this method is
public static void printOdd(int num, int[] iArr, boolean DEBUG)
Then you will call a method that will print out the reverse of the used part of
the array. The header for this method is
public static void printReverseArray(int num, int[] iArr)
main method
------------
To create pseudorandom ints we will be using a standard class called Random.
You declare and instantiate a Random object with the following code. For the
Random class to work properly you will need to import java.util.Random .
final long SEED = 8675309L; // Arbitrary"seed" to be used
// in this assignment
final int INT_LOWER_LIMIT = 10; // Lower limit of pseudorandom ints
// for this assignment
final int INT_RANGE = 90; // Range of pseudorandom ints for this assignment
Random rand = new Random(SEED); // Declare and instantiate object rand
Declare SIZE_ARR, a final int equal to 30.
Declare an array of ints with size equal to SIZE_ARR:
int[] intArr = new int[SIZE_ARR];
Declare DEBUG and all other final named constants. See below.
Declare and instantiate the Random object rand.
Declare all necessary variables.
Read in the value of num, the number of pseudorandom numbers to be generated.
Call the genRandom method
Call the printData method
Call the sumData method
Print our the result of the sumData method
Call the printEven method
Call the printOdd method
Call the printReverse method
Sample Output with DEBUG = true
-------------------------------
DEBUG
The number of locations to fill is 5.
DEBUG
The random number stored in location 0 is 95
DEBUG
The random number stored in location 1 is 28
DEBUG
The random number stored in location 2 is 47
DEBUG
The random number stored in location 3 is 89
DEBUG
The random number stored in location 4 is 98
Full array:
intArr[0] = 95
intArr[1] = 28
intArr[2] = 47
intArr[3] = 89
intArr[4] = 98
DEBUG
The location of array being added to the sum is 0.
The number added to sum is 95.
The current sum is 95.
DEBUG
The location of array being added to the sum is 1.
The number added to sum is 28.
The current sum is 123.
DEBUG
The location of array being added to the sum is 2.
The number added to sum is 47.
The current sum is 170.
DEBUG
The location of array being added to the sum is 3.
The number added to sum is 89.
The current sum is 259.
DEBUG
The location of array being added to the sum is 4.
The number added to sum is 98.
The current sum is 357.
The sum of the array is 357.
Even numbers in the array are:
DEBUG
The nummber in array location 0 is 95.
DEBUG
The nummber in array location 1 is 28.
DEBUG
The number is even.
intArr[1] = 28
DEBUG
The nummber in array location 2 is 47.
DEBUG
The nummber in array location 3 is 89.
DEBUG
The nummber in array location 4 is 98.
DEBUG
The number is even.
intArr[4] = 98
Odd numbers in the array are:
DEBUG
The nummber in array location 0 is 95.
DEBUG
The number is odd.
intArr[0] = 95
DEBUG
The nummber in array location 1 is 28.
DEBUG
The nummber in array location 2 is 47.
DEBUG
The number is odd.
intArr[2] = 47
DEBUG
The nummber in array location 3 is 89.
DEBUG
The number is odd.
intArr[3] = 89
DEBUG
The nummber in array location 4 is 98.
The array reversed.
intArr[4] = 98
intArr[3] = 89
intArr[2] = 47
intArr[1] = 28
intArr[0] = 95
Sample output with DEBUG = false
--------------------------------
Full array:
intArr[0] = 95
intArr[1] = 28
intArr[2] = 47
intArr[3] = 89
intArr[4] = 98
The sum of the array is 357.
Even numbers in the array are:
intArr[1] = 28
intArr[4] = 98
Odd numbers in the array are:
intArr[0] = 95
intArr[2] = 47
intArr[3] = 89
The array reversed.
intArr[4] = 98
intArr[3] = 89
intArr[2] = 47
intArr[1] = 28
intArr[0] = 95
My code:
import java.util.Random;
public class Numbers
{
public static void main(String[] args)
{
final long SEED = 8675309L; // Arbitrary "seed" to be used
final int INT_LOWER_LIMIT = 10; // Lower limit of pseudorandom integers
final int INT_RANGE = 90; // Range of pseudorandom integers
Random rand = new Random(SEED); // Declare and instantiate object rand
final int SIZE_ARR = 30;
int[] iArr = new int[SIZE_ARR];
final boolean DEBUG = true;
int num = rand.nextInt();
genRandom(num, iArr, DEBUG, SIZE_ARR, rand, INT_LOWER_LIMIT, INT_RANGE);
printData(num, iArr, DEBUG);
sumData(num, iArr, DEBUG);
printEven(num, iArr, DEBUG);
printOdd(num, iArr, DEBUG);
printReverseArray(num, iArr);
}
public static void genRandom(int num, int arr[], boolean DEBUG, int SIZE_ARR, Random rand, int INT_LOWER_LIMIT, int INT_RANGE)
{
for(int i = 0; i < SIZE_ARR; i ++)
{
arr[i] = INT_LOWER_LIMIT + rand.nextInt(INT_RANGE);
}
// System.out.println("Error");
// System.exit(0);
}
public static void printData(int num, int arr[], boolean DEBUG)
{
for(int i = 0; i < arr.length; i++)
{
if(DEBUG)
{
System.out.println("[DEBUG] The number of locations to fill is " + i);
System.out.println("[DEBUG] The random number stored in location " + arr[i] + " is " + arr);
//System.out.println("[DEBUG] The ")
}
}
}
public static int sumData(int num, int arr[], boolean DEBUG)
{
int sum = 0;
for(int i = 0; i < arr.length; i++)
{
sum += arr[i];
System.out.println("The sum of the array is " + sum);
if(DEBUG)
{
System.out.println("[DEBUG] The location of array being added to the sum is " + arr[i] + ".");
System.out.println("[DEBUG] The number added to the sum is " + arr[i] + ".");
System.out.println("[DEBUG] The current sum is " + sum);
}
}
return sum;
}
public static void printEven(int num, int arr[], boolean DEBUG)
{
System.out.println("Even numbers in the array are: ");
for(int i = 0; i < arr.length; i++)
{
if(arr[i] % 2 == 0)
{
System.out.println("intArr [" + i + "] = " + arr);
}
if(DEBUG)
{
System.out.println("[DEBUG] The number in array location " + i + "is" + arr + ".");
System.out.println("[DEBUG] The number is even");
}
}
}
public static void printOdd(int num, int arr[], boolean DEBUG)
{
System.out.println("Odd numbers in the array are: ");
for(int i =0; i < arr.length; i++)
{
if(arr[i] %2 != 0 && DEBUG)
{
System.out.println("[DEBUG] The number in array location " + i + "is" + arr + ".");
System.out.println("[DEBUG] The number is odd.");
}
}
}
public static void printReverseArray(int num, int arr[])
{
for(int i = arr.length - 1; i <= 0; i--)
{
System.out.println(arr[i] + " ");
}
}
}