Programming Concept in Grid World

I don't understand what this specific section of code is doing, it

Page 1 of 1

6 Replies - 1325 Views - Last Post: 14 April 2009 - 08:51 AM Rate Topic: -----

#1 mykohlx43  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 13-January 09

Programming Concept in Grid World

Posted 18 March 2009 - 02:39 PM

I have been doing grid world recently in my comp science class and there is this one thing i don't understand. it is right where i put here and the arrow. it has Grid<Actor> grid; what is the <> doing in this situation


package info.gridworld.actor;

import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import java.awt.Color;


public class Actor
{
	private Grid<Actor> grid; ////// HHHEEERRREEE!!!!   <----------------
	private Location location;
	private int direction;
	private Color color;

	public Actor()
	{
		color = Color.BLUE;
		direction = Location.NORTH;
		grid = null;
		location = null;
	}

	public Color getColor()
	{
		return color;
	}

	public void setColor(Color newColor)
	{
		color = newColor;
	}

	public int getDirection()
	{
		return direction;
	}

	public void setDirection(int newDirection)
	{
		direction = newDirection % Location.FULL_CIRCLE;
		if (direction < 0)
			direction += Location.FULL_CIRCLE;
	}

	public Grid<Actor> getGrid()
	{
		return grid;
	}


	public Location getLocation()
	{
		return location;
	}

	public void putSelfInGrid(Grid<Actor> gr, Location loc)
	{
		if (grid != null)
			throw new IllegalStateException(
					"This actor is already contained in a grid.");

		Actor actor = gr.get(loc);
		if (actor != null)
			actor.removeSelfFromGrid();
		gr.put(loc, this);
		grid = gr;
		location = loc;
	}

	public void removeSelfFromGrid()
	{
		if (grid == null)
			throw new IllegalStateException(
					"This actor is not contained in a grid.");
		if (grid.get(location) != this)
			throw new IllegalStateException(
					"The grid contains a different actor at location "
							+ location + ".");

		grid.remove(location);
		grid = null;
		location = null;
	}

	public void moveTo(Location newLocation)
	{
		if (grid == null)
			throw new IllegalStateException("This actor is not in a grid.");
		if (grid.get(location) != this)
			throw new IllegalStateException(
					"The grid contains a different actor at location "
							+ location + ".");
		if (!grid.isValid(newLocation))
			throw new IllegalArgumentException("Location " + newLocation
					+ " is not valid.");

		if (newLocation.equals(location))
			return;
		grid.remove(location);
		Actor other = grid.get(newLocation);
		if (other != null)
			other.removeSelfFromGrid();
		location = newLocation;
		grid.put(location, this);
	}

	public void act()
	{
		setDirection(getDirection() + Location.HALF_CIRCLE);
	}

	public String toString()
	{
		return getClass().getName() + "[location=" + location + ",direction="
				+ direction + ",color=" + color + "]";
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Programming Concept in Grid World

#2 bizkut  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 18-March 09

Re: Programming Concept in Grid World

Posted 18 March 2009 - 04:34 PM

It's Javas way of letting the Grid know the type of Object it will hold. Trying to add any other type of an Object to the grid will result in an error.

It's essentially a way to prevent multiple types of non-associated Objects being put in Collections of sorts.
Was This Post Helpful? 0
  • +
  • -

#3 mykohlx43  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 13-January 09

Re: Programming Concept in Grid World

Posted 26 March 2009 - 02:23 PM

i'm kinda still a newb.. can you explain it differently!?
Was This Post Helpful? 0
  • +
  • -

#4 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Programming Concept in Grid World

Posted 26 March 2009 - 04:25 PM

what it is doing is defining the type that can be put into the Grid,

it is saying that Actors can be added

its sort of like having a string where you only add numbers. you will use it more when you start using ArrayLists
Was This Post Helpful? 0
  • +
  • -

#5 Ostralis  Icon User is offline

  • #include <greeter>
  • member icon

Reputation: 9
  • View blog
  • Posts: 559
  • Joined: 13-December 08

Re: Programming Concept in Grid World

Posted 11 April 2009 - 01:38 PM

View Postvirgul, on 26 Mar, 2009 - 03:25 PM, said:

what it is doing is defining the type that can be put into the Grid,

it is saying that Actors can be added

its sort of like having a string where you only add numbers. you will use it more when you start using ArrayLists

Similar to what virgul said, basically it signifies the use of an ArrayList, which means you can add more Actors.

Tried to restate what virgul said, in simpler terms.

Good luck, I'm doing the same thing!
Was This Post Helpful? 0
  • +
  • -

#6 Mikeyp926  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

Re: Programming Concept in Grid World

Posted 11 April 2009 - 05:17 PM

The technical name for this concept is generics. It's pretty confusing until you can wrap your head around it, but I think it definitely makes sense when you think about it in real world examples. If I'm a teacher and want a list of all my students, then I would obviously not want to put anything other than a student in this list. Java handles this situation by using generics. I could use something like...
ArrayList<Student> studentList;


This would make a list that can only store anything of the type "Student". If I wanted another list of all the parents, I would do
ArrayList<Parent> parentList;


This would make a list that can only store things of the type "Parent". If somewhere else in the program I accidentally attempted to add a Student to the parentList, or add a Parent to the studentList, then the compiler would alert me that this is not right and I would know to fix it. This is very beneficial for many reasons. One of the most obvious being that if I know that every object in a list is of a certain type, then I can confidently compare/sort objects in that list based on properties/variables of the specific object type.

Does this make sense? Kinda hard to explain in words, but definitely a very practical concept once you get it figured out.

-Michael

This post has been edited by Mikeyp926: 11 April 2009 - 05:19 PM

Was This Post Helpful? 0
  • +
  • -

#7 mykohlx43  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 13-January 09

Re: Programming Concept in Grid World

Posted 14 April 2009 - 08:51 AM

ya it does make sense now, it bugs me though that my teacher doesn't even know. he still teaches java 1.4 for some odd reason.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1