Java class problem:(

Started by
7 comments, last by Crazyfool 17 years ago
First and foremost, yes, this is an assignment for school.. but before you stop there, let me explain I'm asking for an issue with a bug, not how to do something. I have a class called CellInfo, that implements CellInfoInterface

class CellInfo 
implements CellInfoInterface
{
	
	private String timeTaken;
	
	private int intensity;
	
	private int row;
	
	private int col;
	
	public CellInfo()
	{
		timeTaken = DATE_TIME_NA;
		intensity = NO_DATA;
	}

	public void updatePosition(int row, int col)
	{
		this.row = row;
		this.col = col;
	}
	
	public int getRow()
	{
		return row;
	}
	
	public int getCol()
	{
		return col;
	}

	// set the cell's intensity to a new value
	public void updateIntensity(int newIntensity)
	{
		intensity = newIntensity;
	}

	// set the time the cell's GPR was done to a new value
	public void updateTimeTaken(String newTimeTaken)
	{
		timeTaken = newTimeTaken;
	}

	// return the cell's intensity
	public int getIntensity()
	{
		return intensity;
	}

	// return the time the cell's GPR was done
	public String getTimeTaken()
	{
		return timeTaken;
	}
	
	public String toString()
	{
		String str = "  " + row + "  ,  " + col + "\t" + intensity + "\t" + timeTaken;
		return str;
	}
}

And then I do some code like this:

CellInfo c = new CellInfo();
c.getIntensity();
Which should work, and does, for EVERY class/file/method EXCEPT a class called SurveyReport. Here is the method in SurveyReport that uses the code:

// removes the node with highest priority
	public CellInfo pop()
	{
		System.out.println("POP!");
		
		if(head != null)
		{
			if(head.next == null)
			{
				CellInfo e = head.data;
				head = null;
				size--;
				return e;
			}
			
			Iterator iter = iterator();
			Iterator highest = iterator();
			//iter and highest are head node!
			
			CellInfo c = new CellInfo(); //ERROR: Unexpected type on "new CellInfo();
			c.getIntensity(); //ERROR: cannot find symbol method

                        //rest of stuff

Any ideas at all on how to fix this? I dont understand it AT ALL!
Advertisement
Quote:Original post by scorchsaber
Never mind. Should have at least glanced at the code.



Eh?
Quote:Original post by Crazyfool
Quote:Original post by scorchsaber
Never mind. Should have at least glanced at the code.



Eh?


Ah... Not used to this forum system. I didn't notice a delete button. Sorry.

First guess: is your SurveyReport in a different package? Your CellInfo class has default visibility so SurveyReport most likely can't see it. Try declaring CellInfo as "public class".
Quote:Original post by lightbringer
First guess: is your SurveyReport in a different package? Your CellInfo class has default visibility so SurveyReport most likely can't see it. Try declaring CellInfo as "public class".


Ah, it had no public, but alas, no dice. Still same error.


Here:
java:78: unexpected typefound   : type parameter CellInfo required: class                        CellInfo c = new CellInfo();


Is the more descript error message, with a ^ pointing to the C in CellInfo();
Well, there's nothing wrong with the code as far as I can see. Are you sure you're importing the package containing CellInfo properly in the file containing SurveyReport? If you still can't find the problem, post the complete source to SurveyReport. I'll look at it again after I wake up, if nobody has found anything by then.
Perfectly happy to.

A few notes... its not finished, and dont finish it for me (not that you would, just reiterating that I do NOT want anyone to do my homework for me)

Also, it may be a bit sloppy, if you think so, tell me which functions are sloppy so I can take extra care with them.. thanks.

import java.util.NoSuchElementException;import java.lang.StringBuilder;public class SurveyReport<CellInfo>implements SurveyReportInterface{	private static class Node<CellInfo>	{		public CellInfo data;		public Node<CellInfo> next;				public Node(CellInfo data, Node<CellInfo> next)		{			this.data = data;			this.next = next;					}	}	// a reference to the first node in the list (null if the list is empty)	private Node<CellInfo> head;	// the number of elements in the list	private int size;	// way queue is sorted	private int reportOrder;		GPRGrid theGrid;		// the constructor initializes this linked list to be empty.	public SurveyReport(GPRGrid theGrid, int reportOrder)	{		System.out.println("CREATE!");		size = 0;		head = null;		this.reportOrder = reportOrder;		this.theGrid = theGrid;	}	// push will add an element to front	public void push(CellInfo e)	{		System.out.println("PUSH!");				head = new Node(e, head);		size++;	}			// removes the node with highest priority	public CellInfo pop()	{		System.out.println("POP!");				if(head != null)		{			if(head.next == null)			{				CellInfo e = head.data;				head = null;				size--;				return e;			}						Iterator iter = iterator();			Iterator highest = iterator();			//iter and highest are head node!									CellInfo c = new CellInfo();			c.getIntensity();						while(iter.hasNext())			{				switch(reportOrder)				{					case SORT_BY_INTENSITY:						//if(iter.curr().getIntensity() > highest.curr().getIntensity())							highest = iter;						break;					case SORT_BY_ROW:											break;					case SORT_BY_COLUMN:											break;					case SORT_BY_TIME:											break;					default:						break;				}								iter.next();			}									size--;			return highest.curr();		}				return null;	}		// size() returns the number of elements in this list.	public int size()	{		return size;	}			// clear() makes this list empty.	public void clear()	{		this.head = null;	}			public String getContent()	{		System.out.println("GETCONTENT!");				StringBuilder str = new StringBuilder();		str.append("Site:   " + theGrid.getSiteName() + "\n\n");				str.append("Number of rows: \t" + theGrid.getNumberOfRows() + "\n");		str.append("Number of columns: \t" + theGrid.getNumberOfCols() + "\n\n");				str.append("Row , Col\tIntensity\tTime\n");		str.append("==========================================\n\n");						//Check for the rows/columns because if it is this way		//I can do a very quick sort.. just move along the grid		if(reportOrder == SORT_BY_ROW || reportOrder == SORT_BY_COLUMN)		{			if(reportOrder == SORT_BY_ROW)			{				for(int y = 0; y < theGrid.getNumberOfRows(); y++)				{					for(int x = 0; x < theGrid.getNumberOfCols(); x++)					{						str.append(theGrid.getSurveyedArea()[y][x].toString() + "\n");					}				}			}						if(reportOrder == SORT_BY_COLUMN)			{				for(int x = 0; x < theGrid.getNumberOfCols(); x++)				{					for(int y = 0; y < theGrid.getNumberOfRows(); y++)					{						str.append(theGrid.getSurveyedArea()[y][x].toString() + "\n");					}				}			}		}		else		{			while(size() > 0)			{				str.append(pop().toString() + "\n");			}		}						return str.toString();	}		public Iterator iterator()	{		return new Iterator(head);	}	public class Iterator	{		Node<CellInfo> current;		Node<CellInfo> beforeCurrent;				public Iterator(Node<CellInfo> current)		{			this.current = current;			beforeCurrent = null;		}				public boolean hasNext()		{			if(current.next == null)			{				return false;			}			else			{				return true;			}		}						public CellInfo curr()		{			if(current != null)				return current.data;			else				return null;		}				public CellInfo next()		{			if(hasNext())			{				beforeCurrent = current;				current = current.next;				return current.data;			}			else			{				return null;			}		}				// inserts the node before currrent		public void insert(Node<CellInfo> node)		{			beforeCurrent.next = new Node(node.data, current);		}								// removes current		public void remove()		{			if(hasNext())			{				beforeCurrent.next = current.next;			}			else if(!hasNext())			{				current = beforeCurrent;				current.next = null;			}			else			{				throw new IllegalStateException();			}		}			}}


Ok, thanks!
The problem is the class declaration line. Your use of generics here is wrong, as you are overwriting the class "CellInfo" with the generic type "CellInfo" - the two have no relation. At least, that's what I think is happening... If you need to make it parameterized, you should use something else for the generic type, like E or T.

When you then actually use the class, you can declare it like SurveyReport sr = new SurveyReport<CellInfo>(); Otherwise, if you want the kind of behavior that you are probably aiming for with this declaration, you might want to stay away from generics and just code to the interface instead.
Aha! That was it! See, I was originally making a generic PriorityQueue, but then realized that it A) wasnt worth it or B) wasnt going to work that way, so I made it just havin to do with my assignment. So I just changed all the E to CellInfo thinking it'd only be declared with the CLASS cellinfo.. THANKS!

This topic is closed to new replies.

Advertisement