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 11 of 11

Thread: HELP PLEASE!

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    13
    Thanks
    1
    Thanked 1 Time in 1 Post

    Exclamation HELP PLEASE!

    Hello,

    I am writing a class (pretty much an ADT) for an array implementation. I wrote the code and it both compiles and works, but in the process, I have encountered a problem. This problem is that the add method in the class does not work properly (neither do the remove methods). Although the logic appears to be correct and it compiles correctly, the classes do not serve the function I was hoping. I was hoping that I could just add an element and the class would dynamically add it (make more space or move items as needed). The remove methods don't seem to serve their function either. Would anybody be able to help me troubleshoot and sort this problem out? I really appreciate it and thank you in advance!

    P.S. If you see any other problems with the code, please feel free to let me know! Thanks again!

    Below is the code for the array implementation class:

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package arrayimplementation2;
     
    /**
     *
     * @author Ben
     */
    public class arrayimplementation 
    {
     
        protected int length;
        protected int size;
        public double list[];
     
        public arrayimplementation ()
        {
            size = 10;
            length = 0;
            list = new double [size];
        }
     
        public arrayimplementation (int arraysize)
        {
            if(size <= 0)
                System.err.println("The array size mist be positive and greater than zero!");
            else{
                arraysize = size;
                length = 0;
                list = new double [arraysize];
            }
        }
     
        public boolean isEmpty()
        {
             return (length == 0);
         }
     
        public boolean isFull ()
        {
            return (length == size);
        }
     
        public int noOfelements()
        {
            return length;
        }
     
        public int listcapacity ()
        {
            return size;
        }
     
        public void output()
        {
          for(int x=0; x< size; x++){
             System.out.print("{" + list[x]+"}");  
           }
        }
     
        public boolean isEqual(int location, int location2)
        {
            if (location <0 || location >= length){
                System.err.println("The location of the item to be compared is out of range!");
            return false;
            }
     
            return (list[location]== list[location2]);
     
        }
     
        public void Remove(int location)
        {
            if(location < 0 || location >= length)
                System.err.println("The location of the item to be removed is out of range!");
            else{
                /*for(int i=0; i< length -1; i++)
                    list[i-1] = i;
                 length --;*/
            //double removedelement = list[location];
            for(int i = location + 1; i < size; i++)
                list[i-1] = list[i];
     
            //return removedelement;
            }
        }
     
        public void Remove (double item)
        {
            int location;
     
            if(length == 0)
                System.err.println("Cannot delete an element from an empty list!");
     
            else
            {
                location = search(item);
                if (location != -1)
                    Remove(location);
                else
                    System.out.println("The item to be deleted is not in the list!");
            }
        }
     
        public int getindex( double item)
        {
            for(int i=0; i<size; i++){
                if(list[i] == item)
                    return i;
            }
                 return -1;      
        }
     
        public void clear(){
            list[size]=0;
            length = 0;
            System.gc();
        }
     
        public void add(int location, double item)
        {
            if(location<0 || location > size)
                System.err.println("The positon of the item to be added is out of range!");
            else if (length == size){
                double[] list2 = new double[length * 2];
                System.arraycopy(list2, 0, list2, 0, list2.length);
                list2 = list;
            }
            else if(location == length){
                list[location] = item;
                length++;
            }
            else {
            for( int i = size-1; i > location; i--)
                list[i] = list[i-1];
     
            list[location] = item;
            length ++;
            }
        }
     
        public int search (double item)
        {
            int location;
            boolean found = false;
     
            for(location = 0; location < length; location++)
                if(list[location] == item)
                {
                    found = true;
                    break;
                }
            if (found)
                return location;
            else
                return -1;
     
        }
     
    }

  2. #2
    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: HELP PLEASE!

    help me troubleshoot and sort this problem out
    Try debugging the code by adding println statements to show the values of variables as they are changed and used to show the what the compiler sees. If you understand what the code is supposed to do, seeing what it is doing should help.

    Do you have a way to test the code? There is no main() method here for testing.
    Last edited by Norm; September 13th, 2012 at 07:16 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    13
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: HELP PLEASE!

    I did not include a way to test the code. I should have, but it would be pretty simple:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package arrayimplementation2;
     
    /**
     *
     * @author Ben
     */
     
    import java.util.*;
     
    public class Arrayimplementation2 {
     
        /**
         * @param args the command line arguments
         */
     
        static Scanner console = new Scanner (System.in);
     
        public static void main(String[] args) {
     
            arrayimplementation array = new arrayimplementation();
     
            array.add(0,5);
            array.add(1,10);
            array.add(2,11);
            array.add(3,50);
            array.add(4,6);
            array.add(5,9);
            array.add(6,34);
            array.add(7,24);
            array.add(0,55);
            array.add(0,99);
            array.add(0,80);
            array.add(0,89);
     
            array.output();
    }
    }

    All you get when you run this code is an array of the size ten (the default constructor), not one greater, despite having 12 adds. Does this help? I did the println statements, but It didn't seem to help me...
    Thanks again for your help!

  4. #4
    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: HELP PLEASE!

    What prints out when you execute the code? How do you see there is a problem?
    Can you post the program's output and add some comments to the output that explains what is wrong with it and show what the output should be.

    When I execute the code I get this printed:
    {99.0}{55.0}{5.0}{10.0}{11.0}{50.0}{6.0}{9.0}{34.0 }{24.0}
    Last edited by Norm; September 13th, 2012 at 10:29 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  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: HELP PLEASE!

    Here's a start for testing and finding problems. Change the constructor call to this:
     
         Arrayimplementation array = new Arrayimplementation(3);       //  3 for testing<<<<<<<<
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    13
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: HELP PLEASE!

    Thats what I get when I print the code, which is the problem. I want the array to grow when you add more members to the array than are currently in the array. For example if you have an array of size 10, and add 12 members to the array, the array is supposed to double in size (20) and add the 12 members, if I'm correct, which it appears I'm not.

  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: HELP PLEASE!

    What happens when you try what I suggested in post #5?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Sep 2012
    Posts
    13
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: HELP PLEASE!

    The System.err.println(); is tripped and it won't add the elements...

  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: HELP PLEASE!

    Why does that happen? What variable does the if statement test? Is it the correct one?
    You need to look at the code and see what is being compared.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Sep 2012
    Posts
    13
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: HELP PLEASE!

    I don't know why it happens. I was hoping someone could tell me. Logically, it all seems to make sense. Length is the number of elements in the array, size is the capacity of the array if that helps...

  11. #11
    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: HELP PLEASE!

    Why does the code test the value of size? Instead of testing the value of the argument passed to the constructor: arraysize?
    What is the purpose of the if statement?
    If you don't understand my answer, don't ignore it, ask a question.