Welcome to Dream.In.Code
Become a Java Expert!

Join 150,375 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,555 people online right now. Registration is fast and FREE... Join Now!




compile error non-static method cannot be refrenced from a static cont

 
Reply to this topicStart new topic

compile error non-static method cannot be refrenced from a static cont

pbgamer602
2 Feb, 2008 - 07:33 AM
Post #1

New D.I.C Head
*

Joined: 3 Nov, 2007
Posts: 14


My Contributions
Hi, my goal in this program is to show polymorphism, I know the Queue and Stack class already exist in java, im just trying to implement them using a n abstract interface and only one method to add variables to the data types and another to remove them, I think i have that done already, my real trouble is to display the numbers stored in the queue and stack. I keep getting this error non-static method isEmpty() cannot be referenced from a static context, and non-static method remove() cannot be referenced from a static context. Please any help is greatly appreciated! oh and my method show data does not have to be plolymorphic. Thanks!


CODE

// Lab31st.java



import java.util.Random;

public class Lab31st
{
    public static void main(String args[])
    {
        System.out.println("\nLab31 \n");
        MyList list1 = new MyStack(100);
        MyList list2 = new MyQueue(100);
        Random rnd = new Random(12345);
        for (int k = 1; k <= 20; k++)
        {
            int rndInt = rnd.nextInt(20) + 10;
            addData(list1,rndInt);
            addData(list2,rndInt);
            
        }
        
        
        System.out.println();  

        showData(list1);
        showData(list2);
        
        System.out.println();
        
        for (int k = 1; k <= 5; k++)
        {
        removeData(list1);
        removeData(list2);
       }
        showData(list1);    
        showData(list2);
        System.out.println();
        }
    


    
    public static void addData(MyList obj, int x)
    {
        obj.add(x);
    }
    
    public static void removeData(MyList obj)
    {
        obj.remove();
    }
    
    public static void showData(MyList obj)
    {
        while (!MyList.isEmpty())
        {
            int number = MyList.remove();
             System.out.println(number);
        }
    }
    
    
    
    
    }





abstract interface MyList
{
    public abstract void add(int x);
    
    public abstract int remove();
    
    public abstract boolean isEmpty();
    
    public abstract int getSize();    

}


class MyStack implements MyList
{
    private int data[];
    private int top;
    private int size;
    private int y;
    
    public MyStack(int y)
    {
        data = new int[y];
        top = -1;
        size = 0;
    }
    
    public boolean isEmpty()
    {
        return size == -1;
    }

    
    public void add(int x)
    {
        top++;
        data[top] = x;
        size++;
    }
    
    public int remove()
    {
        int temp = data[top];
        top--;
        size--;
        return temp;
        
    }
    
    public int getSize()
    {
        return size;
    }
          
    
}

class MyQueue implements MyList
{
    private int data[];
    private int front;
    private int back;
    private int y;
    private int size;
    
    public MyQueue(int y)
    {
        data = new int[y];
        back = -0;
        front = 0;
        size = 0;
        
    }
    
    public boolean isEmpty()
    {
        return front == back;
    }

    
    public void add(int x)
    {
        data[back] = x;
        back ++;
        size++;
        
    }
    
    public int remove()
    {
        int temp = data[front];
        front ++;
        size--;
        return temp;
        
    }
        
    public int getSize()
    {
        return size;
    }
}



User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Compile Error Non-static Method Cannot Be Refrenced From A Static Cont
2 Feb, 2008 - 08:30 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well first of all, in Java all interfaces are abstract. Whether you use the keyword "abstract" or not. They are abstract because you define no implementation and any object that uses the interface is obligated to override all methods of the interface.

Secondly, this error about non-static methods is because main itself is static. Meaning that you don't need to create an instance of main in order to use it. Saying that, anything that is called by main directly must also be static since again, you don't need an instance of main, so it makes no sense to have instances inside something which itself is not an instance.

Thirdly, the main reason you are getting the error is because of how you are using your Mylist class. Remember that Mylist still needs to be an instance to use its methods. The polymorphism comes in because your MyStack or MyQueue can be instances of Mylist.

So you the object passed into your ShowData could be a MyStack or MyQueue object tucked away in a MyList. That is the polymorphism part. So in that function you need to change MyList to simply "obj" and use the object as an instance of Mylist... even if they are really Mystack or MyQueue objects.

CODE

public static void showData(MyList obj)
{
        // obj could be either a stack or queue, but we use it as its base
        // class MyList. The interface allows us to use isEmpty despite the
        // class' true type.

        while (!obj.isEmpty())
        {
            int number = obj.remove();
             System.out.println(number);
        }
}


Now making this change will allow your program to compile. However you have another error you have not discovered yet. Your remove method of the MyStack class runs out of bounds on the array. So check that part out and make sure that you are not trying to access the array when value of "top" is less than 0.

In your remove method you should check the top value before attempting a remove. Since you call removeData which decrements the index. oh and size should never equal -1... because it started at zero. Top should be -1 when empty and size would be 0.

So take a look at your incrementing/decrementing and I am sure you will see what is going on.

Good luck!

"At DIC we be polymorphism master ninjas!" decap.gif

This post has been edited by Martyr2: 2 Feb, 2008 - 08:32 AM
User is offlineProfile CardPM
+Quote Post

pbgamer602
RE: Compile Error Non-static Method Cannot Be Refrenced From A Static Cont
2 Feb, 2008 - 09:46 AM
Post #3

New D.I.C Head
*

Joined: 3 Nov, 2007
Posts: 14


My Contributions
Thanks I see it now!
User is offlineProfile CardPM
+Quote Post

pbgamer602
RE: Compile Error Non-static Method Cannot Be Refrenced From A Static Cont
14 Feb, 2008 - 03:46 PM
Post #4

New D.I.C Head
*

Joined: 3 Nov, 2007
Posts: 14


My Contributions
hey I came up with another problem, my program compiles and runs but the last two outputs after i remove 5 element, are the same and obviously shouldn't . The third one is correct but the fourth one is just the same as the third. Any help is greatly appreciated!!

heres my code

CODE



import java.util.Random;
import java.util.*;

public class Lab31st
{
    public static void main(String args[])
    {
        System.out.println("\nLab31  \n");
        MyList list1 = new MyStack(100);
        MyList list2 = new MyQueue(100);
        
        Random rnd = new Random(12345);
        for (int k = 1; k <= 20; k++)
        {
            int rndInt = rnd.nextInt(20) + 10;
            addData(list1,rndInt);
            addData(list2,rndInt);
            
        }
        
        
        System.out.println();  

        showData(list1);
        showData(list2);
        
        System.out.println();
        
        for (int k = 1; k <= 5; k++)
        {
        removeData(list1);
        removeData(list2);
        }
        showData(list1);    
        showData(list2);
        System.out.println();
        }
    


    
    public static void addData(MyList obj, int x)
    {
        obj.add(x);
    }
    
    public static void removeData(MyList obj)
    {
        
        if (!obj.isEmpty())
            {
                int number = obj.remove();
            }

    }
    
    public static void showData(MyList obj)
    {
        Stack<Integer> temp = new Stack<Integer>();
        
        while (!obj.isEmpty())
        {     
            Integer number = obj.remove();
            System.out.print( number +" ");
            temp.push(number);
        }
      
        System.out.println();
      
        while (!temp.isEmpty())
        {     
            Integer number = temp.pop();
            obj.add(number);
        }    
    
    }
    
    
    }

abstract interface MyList
{
    public abstract void add(int x);
    
    public abstract int remove();
    
    public abstract boolean isEmpty();
    
    public abstract int getSize();    

}


class MyStack implements MyList
{
    private int data[];
    private int top;
    private int size;
    private int y;
    
    public MyStack(int y)
    {
        data = new int[y];
        top = -1;
        size = 0;
    }
    
    public boolean isEmpty()
    {
        return size == 0;
    }

    
    public void add(int x)
    {
        top++;
        data[top] = x;
        size++;
    }
    
    public int remove()
    {
        int temp = data[top];
        top--;
        size--;
        return temp;
        
    }
    
    public int getSize()
    {
        return size;
    }
               
}

class MyQueue implements MyList
{
    private int data[];
    private int front;
    private int back;
    private int y;
    private int size;
    
    public MyQueue(int y)
    {
        data = new int[y];
        back = -0;
        front = 0;
        size = 0;
        
    }
    
    public boolean isEmpty()
    {
        return front == back;
    }

    
    public void add(int x)
    {
        data[back] = x;
        back ++;
        size++;
        
    }
    
    public int remove()
    {
        int temp = data[front];
        front ++;
        size--;
        return temp;    
        
    }
        
    public int getSize()
    {
        return size;
    }

}


User is offlineProfile CardPM
+Quote Post

bhandari
RE: Compile Error Non-static Method Cannot Be Refrenced From A Static Cont
15 Feb, 2008 - 04:49 AM
Post #5

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
QUOTE
abstract interface MyList


still the interface is abstract. Have some pain to change it to interface MyList.

User is offlineProfile CardPM
+Quote Post

pbgamer602
RE: Compile Error Non-static Method Cannot Be Refrenced From A Static Cont
15 Feb, 2008 - 03:34 PM
Post #6

New D.I.C Head
*

Joined: 3 Nov, 2007
Posts: 14


My Contributions
that doesn't do anything
User is offlineProfile CardPM
+Quote Post

bhandari
RE: Compile Error Non-static Method Cannot Be Refrenced From A Static Cont
17 Feb, 2008 - 06:12 PM
Post #7

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
QUOTE
that doesn't do anything


It won't have any effect but at least read the replies carefully. Read what Martyr said in his first line.

User is offlineProfile CardPM
+Quote Post

pbgamer602
RE: Compile Error Non-static Method Cannot Be Refrenced From A Static Cont
17 Feb, 2008 - 07:24 PM
Post #8

New D.I.C Head
*

Joined: 3 Nov, 2007
Posts: 14


My Contributions
I still don't understand why the 4th line is the same as the third
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Compile Error Non-static Method Cannot Be Refrenced From A Static Cont
17 Feb, 2008 - 07:28 PM
Post #9

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
I don't see where the 4th line is the same as the 3rd.

Would you mind clarifying?
User is offlineProfile CardPM
+Quote Post

pbgamer602
RE: Compile Error Non-static Method Cannot Be Refrenced From A Static Cont
18 Feb, 2008 - 06:41 AM
Post #10

New D.I.C Head
*

Joined: 3 Nov, 2007
Posts: 14


My Contributions
yea sorry about that i meant the 3rd and 4th output lines

when i compile and execute I get this


25 22 13 17 14 22 16 20 12 27 19 11 12 25 14 25 18 11 10 21
21 10 11 18 25 14 25 12 11 19 27 12 20 16 22 14 17 13 22 25

22 16 20 12 27 19 11 12 25 14 25 18 11 10 21 <<<<this one is correct
22 16 20 12 27 19 11 12 25 14 25 18 11 10 21 <<<this one should be different



User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:10PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month