Lord Felix 0 Newbie Poster

I need some help in trying to find out how to cast a method that is within an object which is inside a LinkedList.

Let me try to explain:

I already know that I have to use a wrapper to convert an object back into another state. The think I am confused about is how to use a method from the object. What I am doing is storing Objects called Item into a LinkedList. I have a method from Item that gets a stored variable. Heres the code for my Item class.

public class Item implements Comparable
{
	private int myId;
	private int myInv;
	
	public Item(int id, int inv)
	{
		myId = id;
		myInv = inv;
	}
	
	public int getId() 
	{
		return myId;
	}
	
	public int getInv() 
	{
		return myInv;
	}
	
	public int compareTo(Object otherObject) 
	{
		Item right = (Item)otherObject;
		return myId - right.myId;
	}
	public boolean equals(Object otherObject) 
	{
		return compareTo(otherObject) == 0;
	}
	
	public String toString() 
	{
		String string = (getId() + "  " + getInv());
		
		return string;
	}
}

From my experience so far, Arrays with stored objects only need to method call after referencing it.

int id = myArray[index].getId()

What I tried was:

int id = myList.get(index).getId();

I knew that this aproach probably wouldn't work anyway. So right now I need someone to help me to understand how to call a method from an object within a LinkedList.


Thanks in advanced,

Lord Felix

Lord Felix 0 Newbie Poster

Aliright, I figured out that I closed the file too soon and thats what cuased the program to appear to be stuck at the first generation. The only problem I have right now is that cells that do not have neighbors are not dying in the next generation. I did some debugging and it seems that the program is reading in the cells that have no neighbors to have 2 neighbors. With this in mind, I think am currently looking over the program to see why this is so. Help would gladly be accepted as I am wanting to get it done.

Thanks,

Lord Felix

PS: I would edit my posts if there was a funtion to edit even after one day, sorry for the triple post.

Lord Felix 0 Newbie Poster

Alright, I revised that part of the code:

for (int i = 0; i < 21; i++)
      		{	
        		for (int j = 0; j < 21; j++)
        		{
          			numberOfNeighbors = countLiveNeighbors(i, j);
          			if (CellIsAlive(i, j) == true)
				 	{
			    		if (numberOfNeighbors <= 1 || numberOfNeighbors > 3)
			      			nextBoard[i][j] = false;
	  				}
	  				
	 				if (CellIsAlive(i, j) == false)
	 				{
	 					if (numberOfNeighbors == 3)
	 						nextBoard[i][j] = true;
	 				}

				}
			}

The program is stuck on the first generation however.

Lord Felix 0 Newbie Poster

Thanks jwenting, that helped alot. As for the messy coding, I initially had to copy and paste the code from the computer in my school so it messed up the format of the coding. Sorry for that inconveinance. Ill go revise that part of the code. :cheesy:

Lord Felix

Lord Felix 0 Newbie Poster

Sorry for making it unclear. I have to write a program that simulates the game of Life using Two-Dimensional arrays. The size of the grid will be a square 20 x 20 (Which is size of the Two-Dimensional Array). My program is supposed to read the locations of the cells that are in a text file and then store them into the Two-Dimension array. Once stored I have to use an algorithm to read the Two-Dimensional array and search for cells that have neighboring cells and apply Conway's Game of Life rules to them for a set amount of generations (Loops).

His rules of the Game are here if you arn't familiar with them:

A "neighbor" of a cell is defined as any cell touching that cell, for example the eight blue cells in the diagram are the neighbors of the cell in the middle.

Every empty cell with three living neighbors will come to life in the next generation (a "birth").

Any cell with one or zero neighbors will die of loneliness, while any cell with four or more neighbors will die from overcrowding (a "death").

Any cell with two or three neighbors will live into the next generation (no change).

All births and deaths occur simultaneously.

Well the problem I have my the code is that my method to count the number of cells that surround a cell is either wrong or not functional. I need someone to go over my code to see if Im …

Lord Felix 0 Newbie Poster

Well once again I have some trouble getting my lab to work. I am currently having trouble on an algorithm that is supposed to count the neighbors next to a cell in a Two-dimensional array which is supposed to resemble with the algorithm of Conway's Game of Life. For some reason becuase reason it doesnt seem to be working. I have everything else done and would gladly appriciate some help.

import apcslib.*; import chn.util.*;

public class Life
{
	boolean alive = false;
	boolean[][] board = new boolean[21][21];
	boolean[][] nextBoard = new boolean[21][21];
		
	FileInput inFile;
	FileOutput outFile;
	
	public Life(String input, String output)
	{
		inFile = new FileInput(input);
  		outFile = new FileOutput(output);	
	}
	
	public void cellConstruct()
	{
		int pairNumbers = inFile.readInt();
		int count = 0;
		int x, y;
		int width = 20;
		
		while (inFile.hasMoreTokens())
  		{
  			x = inFile.readInt();
  			y = inFile.readInt();
  			count++;

        	board[x][y] = true;
        }
        
	}
	
	public void setAlive(int x, int y, boolean alive)
	{
    	board[x][y] = true;
	}
	
	public boolean CellIsAlive(int i, int j)
	{
		return board[i][j];
	}
	
	public int countLiveNeighbors (int i, int j)
    {
    	int limit = 21 - 1;
    	int count = 0;
    	for (int ii = i - 1; ii <= i + 1; ii++)
    	{
      		for (int jj = j - 1; jj <= j + 1; jj++)
      			{
					if (ii == i && jj == j) continue;
						if (ii < 0 || ii > limit) continue;
							if (jj < 0 || jj > limit) continue;
								if (CellIsAlive (ii, jj) == true) count++;
      			} …
Lord Felix 0 Newbie Poster

Im not very sure on how to store the (x,y) coordinates of the Point2D.Double object into an ArrayList so that I could access them later. I have looked through the Java classes on Point2D.Double, but have only found its constructors and methods and not on how the coordinates can be stored in an ArrayList. I have not much of a lead on this but here's at what I have so far:

public class IrregularPolygon
{
	private ArrayList myPolygon;
	
	public IrregularPolygon() 
	{
	}

	public void add(Point2D.Double aPoint) 
	{
		myPolygon.add(aPoint);
	}

	public void draw() 
	{   
		DrawingTool pencil;
		SketchPad paper;
		paper = new SketchPad(450,450);
		pencil = new DrawingTool(paper);
		
		
	}

	public double perimeter() 
	{   
		double perim = 0;
		
		return perim;
	}

	public double area() 
	{   
		int area = 0;
		
		return area;
	}

}

class IPTester
{
	public static void main(String[] args)
	{
		IrregularPolygon poly = new IrregularPolygon();
	
		poly.add(Point2D.Double(50.0, 50.0));
	}
}

The lab can be found here: Here

Thanks in advance :)