Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Please help with array code

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help with array code

    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] + " ");
    }
    }
    }
    Last edited by senecawolf; November 11th, 2011 at 06:14 PM. Reason: forgot the rest of the code


  2. #2
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Please help with array code

    What is the specific question you have?

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help with array code

    I want to know what I did wrong in my code, instead of printing out the desired output it prints something like: [DEBUG] The random number stored in location 98 is [I@239bf795

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Please help with array code

    What do you expect if;
    int arr[]=new int[10];
    System.out.println(arr);
    Look into your code and you will find it.

Similar Threads

  1. Duplicating array in C code
    By FearTheCron in forum Java Native Interface
    Replies: 1
    Last Post: April 6th, 2013, 09:33 PM
  2. Replies: 4
    Last Post: November 14th, 2011, 09:00 PM
  3. Help with array code.
    By beginnerjava in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 2nd, 2011, 05:50 AM
  4. Array Code Help
    By whattheeff in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 21st, 2011, 04:44 PM
  5. Need help with array code
    By n00bprogrammer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 6th, 2010, 10:54 PM