9 Replies - 1473 Views - Last Post: 18 February 2008 - 07:41 AM Rate Topic: -----

#1 pbgamer602  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-November 07

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

Posted 02 February 2008 - 08:33 AM

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!


// 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;
	}
}




Is This A Good Question/Topic? 0
  • +

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

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

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

Posted 02 February 2008 - 09:30 AM

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.

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!" :snap:

This post has been edited by Martyr2: 02 February 2008 - 09:32 AM

Was This Post Helpful? 0
  • +
  • -

#3 pbgamer602  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-November 07

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

Posted 02 February 2008 - 10:46 AM

Thanks I see it now!
Was This Post Helpful? 0
  • +
  • -

#4 pbgamer602  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-November 07

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

Posted 14 February 2008 - 04:46 PM

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



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;
	}

}



Was This Post Helpful? 0
  • +
  • -

#5 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

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

Posted 15 February 2008 - 05:49 AM

Quote

abstract interface MyList


still the interface is abstract. Have some pain to change it to interface MyList.
Was This Post Helpful? 0
  • +
  • -

#6 pbgamer602  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-November 07

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

Posted 15 February 2008 - 04:34 PM

that doesn't do anything
Was This Post Helpful? 0
  • +
  • -

#7 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

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

Posted 17 February 2008 - 07:12 PM

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.
Was This Post Helpful? 0
  • +
  • -

#8 pbgamer602  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-November 07

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

Posted 17 February 2008 - 08:24 PM

I still don't understand why the 4th line is the same as the third
Was This Post Helpful? 0
  • +
  • -

#9 Nayana  Icon User is offline

  • DIC Hawk - 나야나 नयन:
  • member icon

Reputation: 31
  • View blog
  • Posts: 824
  • Joined: 14-November 07

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

Posted 17 February 2008 - 08:28 PM

I don't see where the 4th line is the same as the 3rd.

Would you mind clarifying?
Was This Post Helpful? 0
  • +
  • -

#10 pbgamer602  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-November 07

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

Posted 18 February 2008 - 07:41 AM

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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1