Error with add method symbol, and boolean help in instance program?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1256 Views - Last Post: 29 September 2011 - 08:37 AM Rate Topic: -----

#1 n00bprogrammer89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-September 11

Error with add method symbol, and boolean help in instance program?

Posted 28 September 2011 - 12:19 PM

Hey guys. I have a program I have been working on and I am so close to finished I just can't figure out the last details.

I want to have a program that compares the timing of Arrays Vs. Array Lists in a loop of 150000 times. Now Java has a built in method ( .add() ) for array lists, so that was easy to time. However, there is no built in method to count the iterations of the array itself. So I tried making an instance program to do this. However I am getting a cannot find symbol error. No idea why. I declared it in my "parent" program, yet I am still getting issues? Aside from that, I was thinking of using an if/boolean statement to communicate back to the client program that there is no more room. Could someone PLEASE help me with this seeing as it has been driving me crazy for days now?

Thanks in advance!!

Here is the code

Client program -------------------

 


 import java.util.*;
 
   public class TimerIllustrationV2 {
   
      public static void main(String[] args) {
		
         int limit = 150000; // Constant 
			
         ArrayList<Integer> List = new ArrayList <Integer>();// Creates Array List
			
         intarray anArray = new intarray(limit);
			
         long arrayList_startTime = System.nanoTime();// Start of the measure of the elapsed time of the code
      
         for (int i=0; i<limit;i++)//For loop to carry out a large number of iterations for arrayList
         {
         
            List.add(i); //ArrayList has a built in method to add items to its list.
         
         }
      	
         long arrayList_endTime = System.nanoTime();//End of Elapsed time. Note: Elapsed time is not related to any other notion of system or wall clock time.
         long arrayList_totalTime = arrayList_endTime - arrayList_startTime;//Calculates the total time the code takes to execute. Gives us nanosecond precision, but not necessarily nanosecond accuracy.
      
      				
         System.out.println("Array List Execution Elapsed Time: " + arrayList_totalTime + " nanoseconds");
      	
      	long array_startTime = System.nanoTime(); //Begins the timing of the array//
      	
      	for (int i=0; i < limit; i++)//for loop to carry out [limit] (150000) iterations of array, so long as "i" is less than 150000
      	
      	{
            anArray.add(i);
         
         }
      	long array_endTime = System.nanoTime();
      	long array_totalTime = array_endTime - array_startTime;
      
      	System.out.println("Array Execution Elapsed Time: " + array_totalTime + " nanoseconds");
      
     		if (arrayList_totalTime > array_totalTime){  // the "if" clause to determine which executes faster
          System.out.println( "The Array Compiles Faster, By: " + (arrayList_totalTime - array_totalTime) + " Nanoseconds");
      }
      else System.out.println ( "The Array List Compiles Faster, By: " + (array_totalTime - arrayList_totalTime) + " Nanoseconds");
      }
		}





Instance program -----

public class intarray {

int next = 0;


public intarray (int limit) {  //constructor class with one argument



intarray anArray[] = new intarray[limit]; //construction of object anArray[] of size limit

}

public void add (int value) { //construction of add method with one argument

anArray[next] = value; // getting a cannot find symbol error??????????????????????
next = next + 1;

// I have three options I am considering --- this is where my if statement should be for how am I going to communicate back to the client that there is no more room. produce error message and abort. --- easiest idea?

//have boolean method return true or false ---- perhaps more efficient?

//throw an exception --- probably overkill for such a small program
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Error with add method symbol, and boolean help in instance program?

#2 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • Posts: 45,396
  • Joined: 27-December 08

Re: Error with add method symbol, and boolean help in instance program?

Posted 28 September 2011 - 12:21 PM

You declare intarray anArray[] = new intarray[limit]; in the constructor, making it local to the constructor and inaccessible outside of it. Use an instance field instead.
public class IntArray{ //please respect Java naming conventions

    private int[] array; //the instance field

    public IntArray(int length){
       array = new int[length]; //initialize the field in the constructor
    }

    public void add(int element){
         //now add() can access the array
    }

}


Was This Post Helpful? 1
  • +
  • -

#3 n00bprogrammer89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-September 11

Re: Error with add method symbol, and boolean help in instance program?

Posted 28 September 2011 - 12:40 PM

Oh wow. I'm embarrassed I overlooked that. Thank you osxnerd!

Now still for the boolean method... hmmm....
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • Posts: 45,396
  • Joined: 27-December 08

Re: Error with add method symbol, and boolean help in instance program?

Posted 28 September 2011 - 12:41 PM

So change the method from void to boolean, and return false if the index is the same as the length of the array.
Was This Post Helpful? 0
  • +
  • -

#5 n00bprogrammer89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-September 11

Re: Error with add method symbol, and boolean help in instance program?

Posted 28 September 2011 - 12:45 PM

View Postmacosxnerd101, on 28 September 2011 - 12:41 PM, said:

So change the method from void to boolean, and return false if the index is the same as the length of the array.



I would love to --- except the java API is not my friend today, and I am unsure how to write the return statement for such a case :( I'm going to keep at it though. Thanks for the advice!! You rule :)
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • Posts: 45,396
  • Joined: 27-December 08

Re: Error with add method symbol, and boolean help in instance program?

Posted 28 September 2011 - 12:47 PM

Use an if statement. And it has nothing to do with the API.
if(yourIndexField == arrayName.length){
   //you have reached the end of the array
   //and cannot add any more elements
}


Was This Post Helpful? 0
  • +
  • -

#7 n00bprogrammer89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-September 11

Re: Error with add method symbol, and boolean help in instance program?

Posted 28 September 2011 - 01:03 PM

View Postmacosxnerd101, on 28 September 2011 - 12:47 PM, said:

Use an if statement. And it has nothing to do with the API.
if(yourIndexField == arrayName.length){
   //you have reached the end of the array
   //and cannot add any more elements
}




Ok --- But I still need it to return true or false and convey that message back to the program to let it know its full. I was never really exposed to booleans, so I apologize for my extreme lack of knowledge and am very sorry to bother the community with such petty questions :(


This is what i've done so far with your help --

public boolean add (int value) { //construction of add method with one argument

anArray[next] = value;

next = next + 1;

if(next == anArray.length){

   //you have reached the end of the array

   //and cannot add any more elements

}

}


Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • Posts: 45,396
  • Joined: 27-December 08

Re: Error with add method symbol, and boolean help in instance program?

Posted 28 September 2011 - 01:04 PM

A boolean has two values- true or false. Which of those two would it make more sense to return if the array is full? What does true represent? What does false represent? Pick the logical one.
Was This Post Helpful? 0
  • +
  • -

#9 n00bprogrammer89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-September 11

Re: Error with add method symbol, and boolean help in instance program?

Posted 28 September 2011 - 01:10 PM

View Postmacosxnerd101, on 28 September 2011 - 01:04 PM, said:

A boolean has two values- true or false. Which of those two would it make more sense to return if the array is full? What does true represent? What does false represent? Pick the logical one.



Ok so I would say that ---

True - Represents the array is full.
False - Represents there is room left in the array.

But just exactly HOW do I get it to return true? Do I need to do something like Boolean full = true; ? Then have the statement return full?
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12297
  • View blog
  • Posts: 45,396
  • Joined: 27-December 08

Re: Error with add method symbol, and boolean help in instance program?

Posted 28 September 2011 - 01:18 PM

I would honestly do the opposite. Return true if the element was successfully added, false otherwise.

The basic syntax for a return statement.
public boolean getTrue(){ 
    return true; 
}



Also see this methods tutorial.
Was This Post Helpful? 0
  • +
  • -

#11 n00bprogrammer89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-September 11

Re: Error with add method symbol, and boolean help in instance program?

Posted 28 September 2011 - 01:21 PM

Thank you greatly! I will work this out and post my results in case others may be having similar issues. You have been more than helpful!
Was This Post Helpful? 0
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Error with add method symbol, and boolean help in instance program?

Posted 28 September 2011 - 01:21 PM

As mac said depends on your logic, becuase the name it self do not tell not like method isFull() or isEmpty() here we can guess.. What you need here is a return statement and I guess that check should be done before adding a value and the check should be if(next >= anArray.length){ so we can know before trying to add a value then if true return else add and then return another boolean which means not full
Was This Post Helpful? 1
  • +
  • -

#13 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Error with add method symbol, and boolean help in instance program?

Posted 28 September 2011 - 01:23 PM

Some pseudo-code which may help your logic -

public boolean add(int value)
{
    if (array is full)
        return false

    else
        array not full so add element
        return true
}


Therefore when we call add(), if it return false, the user knows that the operation failed and the element was not added to the array, otherwise the method return true, and the user knows that the element was successfully added to the array.

This post has been edited by Ryano121: 28 September 2011 - 01:25 PM

Was This Post Helpful? 1
  • +
  • -

#14 n00bprogrammer89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-September 11

Re: Error with add method symbol, and boolean help in instance program?

Posted 28 September 2011 - 02:43 PM

Thank you ryano! That was very helpful! My code now compiles and runs, but my question is, does it actually do anything? When I compare the timing to the code without the boolean vs. the code with the boolean, the timings seem identical. What do you guys think?

public class intarray
{ 
int next = 0;

private int[] anArray; //the instance field

public intarray(int limit){

anArray = new int[limit]; //initialize the field in the constructor

}


public boolean add (int value) { //construction of add method with one argument

if (next == 150000)
{
return false;
}

else
{
anArray[next] = value;

next = next + 1;

return true;
}
	}
		}


Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Error with add method symbol, and boolean help in instance program?

Posted 28 September 2011 - 08:17 PM

Rather than using an hardcoded value like that

if (next == 150000)



that you will have to change in your method if ever you change the size of your array do

if (next == array.length)


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2