Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Feb 2004
    Location
    UK, derby
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java Method help

    Hi All

    I am having some major problems with a piece of work involving Java.
    We have been giving the following:

    Code:
    static int add(int chain, int value)
    // here you need to provide the code that adds a new element to the front of the chain
    // my solution has four lines ...
      {
    	 
        return NULL; // this line is only here so you can compile and run the code as is.
      } // end of method "add"


    We are supposed to simulating the "Heap".

    Then later we have this:
    Code:
    static public void main(String args[])
              {
    			
                System.out.println("start of program");
                int myChain = NULL;	// initialise the chain ...
                int values [] = {76, 42, 33, 29, 50};	
    
                for (int i = 0; i < HEAP_SIZE; i++)
                 {
                   heapArray[i] = new HeapElement();
                 };
    
    			
    
                for (int i = 0; i < 5; i++) {
    				System.out.println(myChain = add(myChain,values[i]));
    				
    			}
    
    
                System.out.println("end of program");
              }

    I need to add the values in the "values" Array into specific random points allocated earlier in the program.

    But the bit I am confused about is the "add" method, and the way it is used, specifically, what does this mean?
    Code:
    myChain = add(myChain,values[i]

    Thanks in advance, jake

  • #2
    cfc
    cfc is offline
    Regular Coder
    Join Date
    Dec 2004
    Location
    Keswick, Ontario
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Your code on that line is stating:

    My Chain = The Function add() used to concatenate (or whatever add() is doing) My Current Chain and the Next Value to be added to it. Then, print that result to the console (assuming System.out is the console).

    The for loop basically iterates over the values and add()s them one-by-one to the chain.

  • #3
    Regular Coder
    Join Date
    Feb 2004
    Location
    UK, derby
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks cfc, I am not a little closer to understanding it.

    My next problem thats hurting my brain, is the int myChain, it seems that this int must have both a data value, and a next value, how can an int have two values?

    Or am I looking at this in the wrong way?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •