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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 28

Thread: 3 Arrays

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 3 Arrays

    I need to implement 3 arrays as per description (look below)

    Linked list must be implemented as three arrays. An array of type Comparable must be used to store the data. Two arrays of integers must be used to represent the linked structure - one array is for the data in use, the other array is for the free list. The values in these arrays are indexes into the data array.

    If someone can please volunteer to give me a head start with this, I would truly appreciate it.

    I know this is "Java" forums but I can repay you back by assisting you with Web Designing (CSS, Flash, HTML, etc.).

    Thank you


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 3 Arrays

    Perhaps you should provide us with what code you currently have, and a detailed description of what issues you're running into? You'll learn a lot more that way (I'm assuming this is a homework assignment or something similar).

    If you really don't know where to start, I would recommend The Java Tutorials by Sun Oracle.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 3 Arrays

    An array of type Comparable must be used to store the data
    That's an interesting use of the Comparable interface. Have you read the API doc for the Comparable interface?
    It's mainly used with the sorting/ordering of lists.

  4. #4
    Junior Member
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 3 Arrays

    public static void main(String [ ] args){
    int dataArray[] = {2, 1, 4, 3};
    int linksArray[] = {4, 0, -1, 2};
    int freeListsArray[] = {5, -1};

    freeListsArray = dataArray;
    freeListsArray = linksArray;
    for(int i = 0; i < linksArray.length; i++)
    linksArray[i]++;
    for(int i = 0; i < dataArray.length; i++)
    dataArray[i]++;


    Even though I am making tiny progress, this is obviously something I am struggling with. So please help? Thank you.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 3 Arrays

    Can you describe what you need to do next and what problems you are having?
    What is the purpose/usage of the three arrays you have defined?
    Why do you destroy the original value of freeListsArray by assigning it the value of dataArray and then immediately destroy that by assigning it the value of linksArray?

    What design work have you done with a piece of paper and a pencil to figure out how to have a linked list and how to link the nodes in the list.
    You must design your program before you code it.
    Last edited by Norm; June 13th, 2011 at 02:02 PM.

  6. #6
    Junior Member
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 3 Arrays

    I am struggling with converting C language to Java language. Can someone please help me with the syntax?

    I need to implement 3 arrays. An array of type Comparable must be used to store the data. Two arrays of integers must be used to represent the linked structure - one array is for the data in use, the other array is for the free list. The values in these arrays are indexes into the data array.

    This is what I have in C for the program:

    HTML Code:
      data =     [two][one][four][][three][];
      links =    [4][0][-1][][2][];
      freelist = [][][][5][][-1];
     
      
      full(){ //checks whether data/links array are full
      	int full = 1;
      	for(int i = 0; i < freekist.length; i++){
      		if(freelist[i] != NULL)
      			full = 0;
      
      	return full; //1 = full and 0 = not full  
      }
      
      empty(){
        	int empty = 1;
        	for(int i = 0; i < data.length; i++){
        		if(data[i] != NULL)
        			empty = 0;
        
      	return full; //1 = empty and 0 = not empty 
        
      }
      
      
      equals() //not really sure what this function is supposed to do
      
      
      iterator(){ //iterates through arrays and prints out data
      int i = 1; //start of data array
      print data[i]; //print first array
      while(links[i] != -1){
      
         i = links[i];  //assign index to i
         print data[i]; //print data
      
      } //should ouput: one two three four
      
      
      }
      
      
      
      insertItem(Comparable item){ //because no position specified, i will assume to add to first free space
      	if(full())
      		print "error, array is full";
      	else {
    		for(int i = 0; i < freekist.length; i++){ //find first free location
    			if(freelist[i] != NULL)
    				break;
    		}
    		links[i] = freelist[i]; //put location in links
    		data[links[i]] = item;  //put item into data
    		temp = links[i];
    		links[data[temp]] = -1; //show where end of array is
    		
      	}  
      }
      
      
      
      
      
      insertItem(Comparable item, int position){
        	if(full())
        		print "error, array is full";
        	else {
        		if(data[position] == NULL){
      			links[position] = freelist[position]; //put location in links
      			data[links[position]] = item;  //put item into data
      			temp = links[position];
      			links[data[temp]] = -1; //show where end of array is
      		else 
      			print "error, location already in use";
      	} 
      
      
      
      }
      
      
    
      removeItem(int position){
      
      	if (data[position] == NULL)
      		print "error,position already empty";
     	 else {
     	 	data[position] = NULL; //remove data
     	 	for(int i = 0; i < links.length; i++){
     	 		if(links[i] == position){
     	 			links[i] == NULL; //remove link to data
     	 			break;
     	 		}	
     	 	
     	 	}
     	 	freelist[position] = i; //add to freelist
      
      	}
      
      
      }
    Thank you!

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 3 Arrays

    Please enclose your code in code tags to preserve formatting. Unformatted, left adjusted code is hard to read.
    Info here:BB Code List - Java Forums
    Last edited by Norm; June 13th, 2011 at 03:56 PM.

  8. #8
    Junior Member
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 3 Arrays

    @ Norm. Yes. I couldn't find my original thread. Anyways, I gave it a shot with the arrays. I am having issues with insert item and remove item.

    Here is my code:
     
     
    public final class SimpleComparableList implements SimpleListADT<java.lang.Comparable> {
        private int size;
        private int currentPosition;
        private int capacity;
        private Object data;
        private final int DEFAULT_CAPACITY = 100; 
        private String[] dataArray;
        private int[] linksArray;
        private int[] freeListArray;
     
        public static void main(String [ ] args){          
        }
     
     
        public SimpleComparableList(){
            this.capacity = DEFAULT_CAPACITY;
            this.currentPosition = 1;
            this.dataArray = new String[capacity];
            this.linksArray = new int[capacity];
            this.freeListArray = new int[capacity];
        }
     
        public SimpleComparableList(int capacity){       
        // Constructs and initializes a list that is empty. Current list size is 
        // zero and current position is set to one.        
            this.capacity = capacity;                 
            this.currentPosition = 1; 
            this.dataArray = new String[capacity];
            this.linksArray = new int[capacity];
            this.freeListArray = new int[capacity];        
        }
     
        @Override
        public boolean empty() {
            //throw new UnsupportedOperationException("Not supported yet.");
            return (this.size == 0);        
        }
     
        @Override
        public boolean full() {
            //throw new UnsupportedOperationException("Not supported yet.");
            return (this.size >= this.capacity);        
        }
     
     
        @Override
        public void insertItem(Comparable item) {
            this.insertItem(item, currentPosition);
        }
     
        @Override
        public void insertItem(Comparable item, int position) {
            //throw new UnsupportedOperationException("Not supported yet.");
            if (!(this.full())){
      		print "error, array is full";
            }else{
    		for(int i = 0; i < freeListArray.length; i++){ //find first free location
    			if(freeListArray[i] != NULL){
    				break;
                            }
    		linksArray[i] = freeListArray[i]; //put location in links
    		dataArray[links[i]] = item;  //put item into data
    		temp = linksArray[i];
    		linksArray[dataArray[temp]] = -1; //show where end of array is        
                    }
            }
        }
     
     
        @Override
        public void removeItem() {
            //throw new UnsupportedOperationException("Not supported yet.");
            this.removeItem(this.currentPosition);        
        }
     
        @Override
        public void removeItem(int position) {
            //throw new UnsupportedOperationException("Not supported yet.");
     
        }
     
        @Override
        public void removeAll() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
     
        @Override
        public int getCurrentListSize() {
            return this.size;
        }
     
        @Override
        public int getCurrentMaxSize() {
            return this.capacity;
        }
     
        @Override
        public int getCurrentPosition() {
            return this.currentPosition;
        }
     
        @Override
        public String getItem() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
     
        @Override
        public void setCurrentPosition(int position) {
            //throw new UnsupportedOperationException("Not supported yet.");
            if (position > this.size) 
                position = this.size;
            if (position < 1) position = 1;
            currentPosition = position;        
        }
     
        @Override
        public void next() {
         //throw new UnsupportedOperationException("Not supported yet.");
        // Increment only the current position of this list if it is not already the 
        // last position.         
            if (this.currentPosition < this.size){
                currentPosition++;
            }else{
                throw new SimpleListException
                        ("Can't increase current on the list");
            }        
        }
     
        @Override
        public void prev() {
            //throw new UnsupportedOperationException("Not supported yet.");
            if (this.currentPosition > 1){
                this.currentPosition--;
            }else{
                throw new SimpleListException
                        ("Can't decrease current on the list");
            }        
        }
     
        @Override
        public void sortList() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
     
        public void quickSort(int array[], int start, int end){
            int i = start;   // index of left-to-right scan
            int k = end;     // index of right-to-left scan
     
            if (end - start >= 1){                   // check that there are at least two elements to sort
                int pivot = array[start];       // set the pivot as the first element in the partition
                while (k > i){                   // while the scan indices from left and right have not met,
                  while (array[i] <= pivot && i <= end && k > i)  // from the left, look for the first
                    i++;                                    // element greater than the pivot
                      while (array[k] > pivot && k >= start && k >= i) // from the right, look for the first
                        k--;                                        // element not greater than the pivot
                          if(k > i)                                       // if the left seekindex is still smaller than
                            swap(array, i, k);                      // the right index, swap the corresponding elements
                }
     
                swap(array, start, k);          // after the indices have crossed, swap the last element in
                                                // the left partition with the pivot 
                quickSort(array, start, k - 1); // quicksort the left partition
                quickSort(array, k + 1, end);   // quicksort the right partition
            }
            else{return;}    // if there is only one element in the partition, do not do any sorting
                            // the array is sorted, so exit        
    }
     
    public void swap(int array[], int index1, int index2) {
    // pre: array is full and index1, index2 < array.length
    // post: the values at indices 1 and 2 have been swapped
     
    	int temp = array[index1];           // store the first value in a temp
    	array[index1] = array[index2];      // copy the value of the second into the first
    	array[index2] = temp;               // copy the value of the temp into the second
    }
     
     
     
    }

    Please help Norm. Thanks.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 3 Arrays

    You left off the import statements needed to compile the code. Either post clean code that compiles or post all of the errors you get when you try to compile the code.

    Also the main() method is empty. How do you plan to test the code?
    Last edited by Norm; June 13th, 2011 at 03:48 PM. Reason: Need clean code or errors

  10. #10
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: 3 Arrays

    Thanks Norm, I have merged both threads into this one.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  11. #11
    Junior Member
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 3 Arrays

    package util; is the only import statement I have.

    There is a separate class provided to me, so when I run it, it runs the test program and I can check what works/does not work. Example: default constructor, insertitem method, remove method, etc.

    OUTPUT: at util.SimpleComparableList.insertItem(SimpleCompara bleList.java:77)

    which is: throw new SimpleListException("Full list");

    Other than that, I do not see any errors.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 3 Arrays

    I get lots of errors. package util; is NOT an import statement!

    Where is SimpleListADT defined?

    Running: D:\Java\jdk1.6.0_25\bin\javac.exe -Xlint -g -deprecation -classpath D:\JavaDevelopment\;.;..\. SimpleComparableList.java

    SimpleComparableList.java:13: cannot find symbol
    symbol: class SimpleListADT
    public final class SimpleComparableList implements SimpleListADT<java.lang.Comparable> {
    ^
    SimpleComparableList.java:13: interface expected here
    public final class SimpleComparableList implements SimpleListADT<java.lang.Comparable> {
    ^
    SimpleComparableList.java:46: method does not override or implement a method from a supertype
    @Override
    ^
    SimpleComparableList.java:52: method does not override or implement a method from a supertype
    @Override
    ^
    SimpleComparableList.java:59: method does not override or implement a method from a supertype
    @Override
    ^
    SimpleComparableList.java:71: cannot find symbol
    symbol : variable NULL
    location: class SimpleComparableList
    if(freeListArray[i] != NULL){
    ^
    SimpleComparableList.java:75: cannot find symbol
    symbol : variable links
    location: class SimpleComparableList
    dataArray[links[i]] = item; //put item into data
    ^
    SimpleComparableList.java:75: incompatible types
    found : java.lang.Comparable
    required: java.lang.String
    dataArray[links[i]] = item; //put item into data
    ^
    SimpleComparableList.java:76: cannot find symbol
    symbol : variable temp
    location: class SimpleComparableList
    temp = linksArray[i];
    ^
    SimpleComparableList.java:77: cannot find symbol
    symbol : variable temp
    location: class SimpleComparableList
    linksArray[dataArray[temp]] = -1; //show where end of array is
    ^
    SimpleComparableList.java:77: incompatible types
    found : java.lang.String
    required: int
    linksArray[dataArray[temp]] = -1; //show where end of array is
    ^
    SimpleComparableList.java:64: method does not override or implement a method from a supertype
    @Override
    ^
    SimpleComparableList.java:83: method does not override or implement a method from a supertype
    @Override
    ^
    SimpleComparableList.java:89: method does not override or implement a method from a supertype
    @Override
    ^
    SimpleComparableList.java:95: method does not override or implement a method from a supertype
    @Override
    ^
    SimpleComparableList.java:100: method does not override or implement a method from a supertype
    @Override
    ^
    SimpleComparableList.java:105: method does not override or implement a method from a supertype
    @Override
    ^
    SimpleComparableList.java:110: method does not override or implement a method from a supertype
    @Override
    ^
    SimpleComparableList.java:115: method does not override or implement a method from a supertype
    @Override
    ^
    SimpleComparableList.java:120: method does not override or implement a method from a supertype
    @Override
    ^
    SimpleComparableList.java:137: cannot find symbol
    symbol : class SimpleListException
    location: class SimpleComparableList
    throw new SimpleListException
    ^
    SimpleComparableList.java:129: method does not override or implement a method from a supertype
    @Override
    ^
    SimpleComparableList.java:148: cannot find symbol
    symbol : class SimpleListException
    location: class SimpleComparableList
    throw new SimpleListException
    ^
    SimpleComparableList.java:142: method does not override or implement a method from a supertype
    @Override
    ^
    SimpleComparableList.java:153: method does not override or implement a method from a supertype
    @Override
    ^
    25 errors

    1 error(s)
    Last edited by Norm; June 13th, 2011 at 04:19 PM.

  13. #13
    Junior Member
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 3 Arrays

    Okay. With all respect, instead of completely nitpicking, can you provide assistance? You recommended I should draw a diagram - with a pencil and paper - i did that. You wanted me to provide some code - i provided that - now you won't provide assistance? Just tell me what's wrong with the code I PROVIDED. I will figure the "25 errors" and the "1 error(s)".

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 3 Arrays

    I am not going to do any of your design work. I will help you get your design to work. I will not write any code for you.
    The FIRST thing you must do is provide me with code that executes or if you're getting compiler errors, then a full list of the text of the errors.
    Until then we are wasting time.

    Just tell me what's wrong with the code I PROVIDED
    I gave you a list of the errors I got. Your last post stated: I do not see any errors.
    Fix the errors.
    Where is the missing class defined?

    Good luck on your project.

  15. #15
    Junior Member
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 3 Arrays

    When I run the program I get the compilation error (as follows).

    run:
    Exception in thread "AWT-EventQueue-0" util.SimpleListException: Full list
    at util.SimpleComparableList.insertItem(SimpleCompara bleList.java:77)
    at lab3.COMP139Lab3Driver.checkInsertItem2(COMP139Lab 3Driver.java:900)
    at lab3.COMP139Lab3Driver.checkIterator(COMP139Lab3Dr iver.java:1046)
    at lab3.COMP139Lab3Driver.jButtonDoTestsActionPerform ed(COMP139Lab3Driver.java:695)
    at lab3.COMP139Lab3Driver.access$1600(COMP139Lab3Driv er.java:19)
    at lab3.COMP139Lab3Driver$22.actionPerformed(COMP139L ab3Driver.java:497)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6289)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:605 4)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4652)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 82)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 82)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:644)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:603)
    at java.awt.EventQueue$1.run(EventQueue.java:601)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:617)
    at java.awt.EventQueue$2.run(EventQueue.java:615)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 614)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    Exception in thread "AWT-EventQueue-0" util.SimpleListException: Full list
    at util.SimpleComparableList.insertItem(SimpleCompara bleList.java:77)
    at lab3.COMP139Lab3Driver.checkInsertItem2(COMP139Lab 3Driver.java:900)
    at lab3.COMP139Lab3Driver.checkIterator(COMP139Lab3Dr iver.java:1046)
    at lab3.COMP139Lab3Driver.jButtonDoTestsActionPerform ed(COMP139Lab3Driver.java:695)
    at lab3.COMP139Lab3Driver.access$1600(COMP139Lab3Driv er.java:19)
    at lab3.COMP139Lab3Driver$22.actionPerformed(COMP139L ab3Driver.java:497)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6289)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:605 4)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4652)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 82)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 82)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:644)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:603)
    at java.awt.EventQueue$1.run(EventQueue.java:601)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:617)
    at java.awt.EventQueue$2.run(EventQueue.java:615)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 614)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)


    Now, Norm, if you are still gonna ask me to "correct" the above, I am through with you - BECAUSE - the program actually runs. It's a Test Program. It entails the following information:
    Default Constructor OK
    Constructor with given max size OK
    iterator()
    insertItem(Comparable item)
    insertItem(Comparable item, int position)
    removeItem()
    removeItem(int position)


    Any one else wanna help me out here?

    All I am requesting is what is wrong with my insertItem method. I will deal with other problems that you are thinking of.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 3 Arrays

    the compilation error (as follows).
    I'm sorry, to me it looks like an execution error.

    Without a definition for this class: SimpleListADT
    I can NOT compile the code. PERIOD. NO WAY to do any more.

  17. #17
    Junior Member
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 3 Arrays

    We are officially done talking. I do not need any assistance from you - ever.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 3 Arrays

    Good luck on the project.
    Hopefully you'll tell the next guy that tries to help where the parts are that are needed to compile the code.
    And I think there is a lot more about your environment that you are not telling.
    This reference to a class says a lot: lab3.COMP139Lab3Driver

  19. #19
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: 3 Arrays

    @D3158 - What do you think this does in your insertItem method?
    if (!(this.full())){
        print "error, array is full";
    }

    If you want more specific help, please post up some compilable code. Norm has been very patient - we don't get paid here, we just spend some precious time trying to help. The least we expect is that people asking for help post code that compiles. The help you will get is proportional to the effort you make to help us to help you. At the end of the day, it's your problem, not ours.

  20. #20
    Junior Member
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 3 Arrays

    I am sorry I was not being co-operative. I was just frustrated and I want to apologize to Norm specially.

    Here is my updated source code that compiles (if people still can help me). It runs.

    Here is my SimpleListADT.java:

    package util;
     
    /**
     * Defines a SimpleListADT.
     */
    public interface SimpleListADT<T> {
     
      /**
       * Determines if this list is empty.
       * @return <code>true</code> if the list is currently empty, false otherwise.
       */
      public boolean empty();
     
      /**
       * Determines if this list is full.
       * @return <code>true</code> if the list is currently full, or
       *         <code>false</code> otherwise.
       */
      public boolean full();
     
      /**
       * Inserts an <code>item</code> into the list at the current position. If
       * an element occupies the current position before this call, that element
       * will be the new item's successor after the call. The list's current
       * position does not change. The first element in the list is defined to be
       * at position 1 (one) <em>not</em> at position zero (0). To insert an
       * item after the last element in the list (i.e., insert an item which will
       * become the last one and will not have a successor) you must use the
       * <code>insertItem(T, int)</code> method.
       * @see SimpleListADT insertItem(T, int)
       * @param item the item to be inserted.
       * @throws <code>SimpleListException</code> if the list is full.
       */
      public void insertItem(Comparable item);
     
      /**
       * Inserts an <code>item</code> into the list at a specified
       * <code>position</code>. If the list is empty or the <code>position</code>
       * value is less than 1 (one), the item becomes the first one. If
       * <code>position</code> is greater than the current list size, the item
       * becomes the last one. If an element already occupies the specified
       * position, that element will be the new item's successor after the call.
       * Before returning, the current position is updated to <code>item</code>'s
       * position in the list. The current position will not be greater than the
       * current list size.
       * @see SimpleListADT insertItem(T)
       * @param item the item to be inserted.
       * @param position the position in the list that the new item will occupy
       *        after insertion.
       * @throws <code>SimpleListException</code> if the list is full.
       */
      public void insertItem(Comparable item, int positionOnTheList);
     
      /**
       * The element at the current position is removed from the list. If the
       * removed element had a successor then that successor will be at the
       * current position after the removal. The current position is not changed
       * unless the list becomes empty (in that case the current position is set
       * to one) or the last element in the list is removed (in that case the
       * current position is set to the current number of items in the list, after
       * the removal).
       * @see #removeAll()
       * @see #removeItem(int)
       * @throws <code>SimpleListException</code> if the list is empty or the
       *         current position is not set.
       */
      public void removeItem();
     
      /**
       * The element at the specified <code> position </code> is removed from the
       * list. If the removed element had a successor then that successor will be
       * at the vacated position after the removal.  The current position is not
       * changed unless the current position exceeds the length of the list after
       * the removal (in that case the current position is set to the length of the
       * list or 1 (one) if the list becomes empty.
       * @param position the position of the element to be removed.
       * @see #removeAll()
       * @see #removeItem()
       * @throws <code>SimpleListException</code> if the list is empty or the
       *         current position is not set.
       */
      public void removeItem(int positionOnTheList);
     
      /**
       * The list is made empty.
       * @see #removeItem()
       * @see #removeItem(int position)
       * @throws <code>SimpleListException</code> if the list is empty.
       */
      public void removeAll();
     
      /**
       * Obtain the size of the list.
       * @return the current number of elements in the list.
       */
      public int getCurrentListSize();
     
      /**
       * Obtain the maximum size of the list.
       * @return the maximum number of elements that can be stored in the list.
       */
      public int getCurrentMaxSize();
     
      /**
       * Obtain the current position for the list. The list's position indicator is
       * '1-based' <em>not</em> '0-based'. That is, the first position is numbered
       * 1, the second numbered 2, etc. The last position is 'n' where n is the
       * number of items in this list.
       * @return the current position.
       */
      public int getCurrentPosition();
     
      /**
       * Obtain the element at the current position.
       * @return the element stored at the current position in the list.
       * @throws <code>SimpleListException</code> if the list is empty.
       */
      public T getItem();
     
      /**
       * Sets the current position value for the list.  The position value must be
       * between 1 (one) and the number of elements in the list.  If the provided
       * value is less than one (1), the position value will be set to one (1).  If
       * the provided value is greater than the number of elements in the list, the
       * position value will be set to the number of elements in the list.
       * @param position the value for the list's current position.
       */
      public void setCurrentPosition(int position);
     
      /**
       * Increments the current position value for the list if the current
       * position is not already the last position; otherwise, the current position
       * value is not changed.
       * @throws <code>SimpleListException</code> if there is no next element
       * or if the list is empty.
       */
      public void next();
     
      /**
       * Decrements the current position value for the list if the current
       * position is not already the first position; otherwise, the current
       * position value is not changed.
       * @throws <code>SimpleListException</code> if there is no previous element
       * or if the list is empty.
       */
      public void prev();
     
      /**
       * Sorts the list into ascending order according to the natural ordering of
       * the elements. The natural ordering can be determined by the
       * <code>compareTo()</code> method. Before returning to the caller, the
       * current position will be set at the last (end of list) position.
       */
      public void sortList();
    }


    Here is the SimpleListException.java

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package util;
     
    /**
     *
     * @author D3158
     */
    public class SimpleListException extends RuntimeException  {
     
        public SimpleListException(String message){
            super(message);
        }
     
     
    }

    Here is SimpleComparableList.java

    package util;
    /* @author D3158 */
    public final class SimpleComparableList implements SimpleListADT<java.lang.Comparable> {
        private int size;
        private int currentPosition;
        private int capacity;
        private String[] array;      
        //private Object data;
        //private final int DEFAULT_CAPACITY = 100; 
        //private String[] dataArray;
        //private int[] linksArray;
        //private int[] freeListArray;
     
     
        public static void main(String [ ] args){          
        }
     
     
        public SimpleComparableList(){
            this.capacity = 100;
            this.array = new String[this.capacity];        
            this.currentPosition = 1;
            this.size = 0;        
            //this.dataArray = new String[capacity];
            //this.linksArray = new int[capacity];
            //this.freeListArray = new int[capacity];
        }
     
        public SimpleComparableList(int capacity){       
        // Constructs and initializes a list that is empty. Current list size is 
        // zero and current position is set to one.        
            this.capacity = capacity;                 
            this.currentPosition = 1; 
            //this.dataArray = new String[capacity];
            //this.linksArray = new int[capacity];
            //this.freeListArray = new int[capacti];        
            array = new String[capacity];
            this.size = 0;        
        }
     
        @Override
        public boolean empty() {
            //throw new UnsupportedOperationException("Not supported yet.");
            return (size == 0);
     
        }
     
        @Override
        public boolean full() {
            //throw new UnsupportedOperationException("Not supported yet.");
            return (size >= capacity);        
        }
     
     
        @Override
        public void insertItem(Comparable item) {
            //throw new UnsupportedOperationException("Not supported yet.");
            insertItem(item, currentPosition);        
        }
     
        @Override
        public void insertItem(Comparable item, int positionOnTheList) {
            //throw new UnsupportedOperationException("Not supported yet.");
        	if (full()) throw new SimpleListException("Full list");
        	int target = positionOnTheList-1;
        	if (target > size) target = size;
            for(int i = size ; i>target ; i--) array[i] = array[i-1];
            array[target] = (String) item;
            size++;
            currentPosition = positionOnTheList;                
        }
     
     
        @Override
        public void removeItem() {
            //throw new UnsupportedOperationException("Not supported yet.");
            removeItem(currentPosition);        
        }
     
        @Override
        public void removeItem(int positionOnTheList) {
            //throw new UnsupportedOperationException("Not supported yet.");
            if(empty()) throw new SimpleListException("Empty List");
        	size--;
            for(int i = --positionOnTheList ; i<size ; i++) array[i] = array[i+1];
            if (currentPosition > size) currentPosition = size+1;        
        }
     
        @Override
        public void removeAll() {
            //throw new UnsupportedOperationException("Not supported yet.");
            if(empty()) throw new SimpleListException("Empty List");
            size = 0;
            currentPosition = 1;        
        }
     
        @Override
        public int getCurrentListSize() {
            //throw new UnsupportedOperationException("Not supported yet.");
            return this.size;        
        }
     
        @Override
        public int getCurrentMaxSize() {
            //throw new UnsupportedOperationException("Not supported yet.");
            return this.capacity;
        }
     
        @Override
        public int getCurrentPosition() {
            //throw new UnsupportedOperationException("Not supported yet.");
            return this.currentPosition;        
        }
     
        @Override
        public String getItem() {
            //throw new UnsupportedOperationException("Not supported yet.");
            if (empty()) throw new SimpleListException("Empty List");
            return array[currentPosition-1];        
        }
     
        @Override
        public void setCurrentPosition(int position) {
            //throw new UnsupportedOperationException("Not supported yet.");
            if (position > size) position = size+1;
            else if (position < 1) position = 1;
            currentPosition = position;        
        }
     
        @Override
        public void next() {
            //throw new UnsupportedOperationException("Not supported yet.");
            if (currentPosition > size)
            	throw new SimpleListException ("Can't increase current on the list");	
            currentPosition++;        
        }
     
        @Override
        public void prev() {
            //throw new UnsupportedOperationException("Not supported yet.");
            if (currentPosition < 2)
            	throw new SimpleListException("Can't decrease current on the list");
            this.currentPosition--;        
        }
     
        @Override
        public void sortList() {
     
        }  
    }

    When I run the program, i don't get a compilation error (which is obviously good) and it runs the GUI interface:

    Default Constructor OK
    Constructor with given max size OK
    iterator()
    insertItem(Comparable item)
    insertItem(Comparable item, int position)
    removeItem()
    removeItem(int position)


    The unchecked ones are the problematic issues. My curiosity is do I need to "fix" the iterator() first so then others will follow or insertItem?

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 3 Arrays

    Apologies accepted.
    You still need to design/describe what you want your code to do.

    Have you looked at the java some of the classes in the src.zip file to get some ideas of how java did it?

  22. #22
    Junior Member
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 3 Arrays

    The bottom line are 2 things:
    1) The program should run with no compilation errors - Check. It runs.
    2) When the program runs, to run the each individual test in the GUI interface (see attached file)lab3tester.jpg - Work in progress

    So, so far I only have the 2 test that work:
    Default Constructor
    Constructor with given max size

    Clear now? I hope so!

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 3 Arrays

    Ok, got it. What is your next step? Which method are you going to do next?
    Remember, we're here to help you get your code to work. We don't write your code. You write it.

  24. #24
    Junior Member
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 3 Arrays

    I am trying to get the iterator to work.


    Here is the code for it.

        @Override
        public Iterator<String> iterator() {
            //throw new UnsupportedOperationException("Not supported yet.");
            return new SimpleComparableListIterator();        
        }
     
        private int size() {
            // Returns number of elements currently in this bag.
            return size;
        }
     
     
        private class SimpleComparableListIterator implements Iterator<String> {
                private int current;
                private SimpleComparableListIterator() {
                this.current = 0;        
                }
     
            @Override
            public boolean hasNext() {
                //throw new UnsupportedOperationException("Not supported yet.");
                return this.current < SimpleComparableList.this.size();            
            }
     
            @Override
            public String next() {
                //throw new UnsupportedOperationException("Not supported yet.");
                 if (this.current < SimpleComparableList.this.size()) {
                    return SimpleComparableList.this.array[this.current++];
                 }
                throw new NoSuchElementException("List has no more elements");
            }
     
            @Override
            public void remove() {
                throw new UnsupportedOperationException("Not supported yet.");
            }

    But it is not working as showed in the image I posted in my previous post.

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: 3 Arrays

    it is not working
    Can you explain in more detail what the problem is. What happens? Does it give an error?

    I don't see any image in your previous post.

Page 1 of 2 12 LastLast

Similar Threads

  1. 2d arrays help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 27th, 2010, 05:27 PM
  2. arrays
    By dwamalwa in forum Java Theory & Questions
    Replies: 4
    Last Post: November 27th, 2010, 01:44 AM
  3. Help with Character arrays
    By shanklove in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 6th, 2010, 12:56 PM
  4. Arrays
    By mlan in forum Java Theory & Questions
    Replies: 2
    Last Post: February 26th, 2010, 10:23 AM
  5. 2d Arrays
    By mgutierrez19 in forum Collections and Generics
    Replies: 5
    Last Post: October 27th, 2009, 04:08 PM