Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-21-2007, 02:22 AM
Member
 
Join Date: Nov 2007
Posts: 4
naxalyte is on a distinguished road
Newbie can't spot his mistake :(
Hi all,

Basically I am designing a virtual 2Dimensional grid, made up of cells, and an object that will be able to simulate moving from cell to cell.

So far I've got four classes:
1 main
2 GridBot
3 Grid
4 Cell

My question is how do I allow an instance of GridBot to set a variable in an instance of Cell. I have tried objectname.method() but it doesnt work.

I've taken the time to slim down my coding so that you can see more easily what my problem is.

Code:
public class main(){ public gridBot aGridBot = new gridBot(); public Grid theGrid = new Grid(); public static void main(String args[]){ main theMainProgram = new main(); } public main(){ //link gridBot to cell 1,1 and set its occupied state to true aGridBot = new gridBot(); aGridBot.CellCoordinateX = 1; aGridBot.CellCoordinateY = 1; theGrid.setCell(1, 1, true); } } public class Grid{ public Cell aCell[][] = new Cell[4][4]; public Grid(){ this.InitializeCells(); } public void InitializeCells(){ for (int x=0; x<4; x++){ for (int y = 0; y<4; y++){ aCell[x][y] = new Cell(); } } } public void setCell(int x, int y, boolean YesNoOccupied){ aCell[x][y].Occupied = YesNoOccupied; } } public class Cell{ boolean Occupied; public Cell(){ Occupied = false; } } public class gridBot{ public int CellCoordinateX; public int CellCoordinateY; //this is the method I am trying to develop, currently not called by anything public void VirtualMove(){ CellCoordinateX = CellCoordinateX + 1; CellCoordinateY = CellCoordinateY + 1; //I want to invoke method setCell in 'theGrid' } }

Last edited by naxalyte : 11-21-2007 at 04:00 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-21-2007, 04:31 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
how do I allow an instance of GridBot to set a variable in an instance of Cell
Pass it a reference to the Grid instance.
Code:
public class main(){ public Grid theGrid = new Grid(); // Pass a copy of the reference theGrid to aGridBot. // This will instantiate the Grid instance/member // variable in that class and allow it to call // methods in the Grid class. public gridBot aGridBot = new gridBot(theGrid); public static void main(String args[]){ main theMainProgram = new main(); } public main(){ //link gridBot to cell 1,1 and set its occupied state to true // the gridBot member variable was instantiated above so // we don't need to do it again here. // aGridBot = new gridBot(); aGridBot.CellCoordinateX = 1; aGridBot.CellCoordinateY = 1; theGrid.setCell(1, 1, true); } } public class gridBot{ public int CellCoordinateX; public int CellCoordinateY; Grid theGrid; public gridBot(Grid theGrid) { this.theGrid = theGrid; } //this is the method I am trying to develop, currently not called by anything public void VirtualMove(){ CellCoordinateX = CellCoordinateX + 1; CellCoordinateY = CellCoordinateY + 1; //I want to invoke method setCell in 'theGrid' theGrid.setCell(args...); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-21-2007, 07:12 PM
Member
 
Join Date: Nov 2007
Posts: 4
naxalyte is on a distinguished road
Thanx for your reply hardwired. So if I have an array of gridBots, each one having a reference to theGrid, would they all be able to see the same grid or each their own individual copy of the grid? Is this the standard way to create 'relations' between classes?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-21-2007, 09:03 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
would they all be able to see the same grid or each their own individual copy of the grid?
If each bot had their own grid it would be like each one was playing in a different game. If you make one instance of Grid and pass the reference to each bot then they can all play in the same grid together. So, one instance for all bots.
Is this the standard way to create 'relations' between classes?
Generally, yes. Of course it depends on what you're doing.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-21-2007, 09:31 PM
Member
 
Join Date: Nov 2007
Posts: 4
naxalyte is on a distinguished road
Thanx so much
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie needs help... vicky08 New To Java 2 03-31-2008 06:26 PM
Java Sun SPOT development kit for sale theveg Reviews / Advertising 0 03-05-2008 03:53 AM
Newbie CSnoob87 Introductions 2 02-18-2008 10:49 AM
PLEASE!!!help me to find mistake sasha20 New To Java 1 01-11-2008 12:50 PM
Newbie reporting nelsaez Introductions 0 11-05-2007 08:39 PM


All times are GMT +3. The time now is 01:20 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org