Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: help with defining a method

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default help with defining a method

    I'm having a bit of trouble with this..

    public void emptySquare(Location l)
    	/*
    		Pre-condition: the given Location value is within
    					   the bounds of the current Grid
    					   object
    		Post-condition: the square at the position in the
    						grid indicated by the given Location
    						value is altered to be empty
    		Informally: update the square at the nominated location
    					of the grid with the empty symbol
    	*/
    	{
    		Square q;
     
          	trace("emptySquare: emptySquare starts");
     
    		//COMPLETE ME!!!
     
    		assert(!validMove(l));
     
    		i=findIndex(l);
    		board[i]=q;
    		q=null;
     
     
     
          	trace("emptySquare: emptySquare ends");
    	}

    I'm not sure how to implement square q. Does this seem right to you?

    Thanks

    Below is the full class with completed and some remaining methods still to be written..

    /**
    *	Grid ADT
    *	@author J.R.Dermoudy and <<INSERT YOUR NAME HERE>>
    *	@version December 2005
     
    	This file holds the Grid ADT which represents
    	the Nim board.  The Grid consists of an	array
    	of the (linear) squares in the board.
     
    	YOU NEED TO MAKE CHANGES TO THIS FILE!
    */
     
    import java.awt.*;
     
     
    public class Grid implements GridInterface
    {
    	//finals
    	private final int DIMENSION=21;						// number of squares in grid
    	private final int MAXIMUM_ROW=6;					// number of rows in grid
    	private final boolean TRACING=true;				// do we want to see trace output?
     
    	// properties
    	private int dimension;			// the number of rows in the grid
    	private Square board[];			// all the Squares within the grid
     
     
    	public Grid()
    	/*
    		Constructor method.
    		Pre-condition: none
    		Post-condition: a 21 element grid is created in
    						which all squares are occupied		
    		Informally: create a full 6 row triangular grid
    	*/
    	{
    		int i,r,c;
    		Location l;
     
    		trace("Grid: constructor begins");
     
    		dimension=MAXIMUM_ROW;		
    		board=new Square[DIMENSION];
     
    		trace("Grid: building squares");
    		i=0;
    		for (r=0; r<MAXIMUM_ROW; r++)
    		{
    			for (c=0; c<=r; c++)
    			{
    				l=new Location(r+1,c+1);
    				board[i]=new Square(l);
    				i++;
    			}
    		}
     
    		trace("Grid: constructor ends");
    	}
     
    	private int findIndex(Location l)
    	/*
    		Locate index in grid which corresponds to square at
    		given location
    		Pre-condition: location is within the current grid
    		Post-condition: the index of the element with the given
    						location is returned
    		Informally: identify which array element is at the given
    					location
    	*/
    	{
    		int i,r,c;
     
    		trace("findIndex: findIndex starts");	
     
    		i=0;
    		for (r=1;r<l.getRow();r++)
    		{
    			for (c=1;c<=r;c++)
    			{
    				i++;
    			}
    		}
     
    		i+=(l.getColumn()-1);
     
    		trace("findIndex: findIndex ends");
    		return i;
    	}
     
     
    	public void setSquare(Location l, Square s) throws IllegalGridException
    	/*
    		Set method for an element of the "board" instance variable.
    		Pre-condition: the given Square object and the board array
    					   are defined
    		Post-condition: the given square is assigned to an element
    						of the Grid object selected according to
    						the given location within the grid
    		Informally: insert the given square into the grid at the
    					appropriate location, an exception is thrown if
    					the location is not within the grid
    	*/
    	{
    		int i;
     
          	trace("setSquare: setSquare starts");
     
    		//COMPLETE ME!!!
    		assert(s!=null);
     
    		if(!validMove(l))
    		{
    			throw new IllegalGridException();
    		}
    		i=findIndex(l);
    		board[i]=s;
          	trace("setSquare: setSquare ends");
    	}
     
     
    	public Square getSquare(Location l) throws IllegalGridException
    	/*
    		Get method for an element of the "board" instance variable.
    		Pre-condition: the board array is defined
    		Post-condition: the Square object at the appropriate
    						element of the "board" selected according
    						to the given Location value is returned
    		Informally: return the square of the grid at the given
    					location, an exception is thrown if the
    					location is not within the grid
    	*/
    	{
    		int i;
     
          	trace("getSquare: getSquare starts");
     
    		//COMPLETE ME!!!
    		assert(board!=null);
     
    		if (!validMove(l))
    		{
    			throw new IllegalGridException();
    		}
    		i=findIndex(l);
    		return board[i];
    	}
     
     
    	public void setDimension(int d)
    	/*
    		Set method for the "dimension" instance variable.
    		Pre-condition: the given Dimension value is defined and
    					   valid
    		Post-condition: the instance variable "dimension" is
    						assigned the given Dimension value
    		Informally: assign the given dimension to the Grid object
    	*/
    	{
          	trace("setDimension: setDimension starts");
     
    		//COMPLETE ME!!!
    		dimension=d; //assigns d to the dimension int variable
     
          	trace("setDimension: setDimension ends");
    	}
     
     
    	public int getDimension()
    	/*
    		Get method for "dimension" instance variable.
    		Pre-condition: none
    		Post-condition: the value of the Grid object's dimension
    						field is returned
    		Informally: return the current grid's dimension
    	*/
    	{
          	trace("getDimension: getDimension starts and ends");
     
    		//COMPLETE ME!!!
    		return dimension; //returns the int dimension of the grid
    	}
     
     
    	public void emptySquare(Location l)
    	/*
    		Pre-condition: the given Location value is within
    					   the bounds of the current Grid
    					   object
    		Post-condition: the square at the position in the
    						grid indicated by the given Location
    						value is altered to be empty
    		Informally: update the square at the nominated location
    					of the grid with the empty symbol
    	*/
    	{
    		Square q;
     
          	trace("emptySquare: emptySquare starts");
     
    		//COMPLETE ME!!!
     
    		assert(!validMove(l));
     
    		i=findIndex(l);
    		board[i]=q;
    		q=null;
     
     
     
          	trace("emptySquare: emptySquare ends");
    	}
     
     
    	public boolean isEmpty(Location l)
    	/*
    		Pre-condition: the given Location value is within
    					   the bounds of the current grid
    		Post-condition: a Boolean value is returned which
    						represents whether the symbol of
    						the square of the current Grid
    						object with the given Location
    						value is empty
    		Informally: return whether or not the square at
    					the given location is empty
    	*/
    	{
          	trace("isEmpty: isEmpty starts and ends");
    		//COMPLETE ME!!!
     
    		assert(!validMove(l));
     
    		i=findIndex(l);
     
    	}
     
     
    	public boolean validMove(Location l)
    	/*
    		Pre-condition: none
    		Post-condition: true is returned if the given
    						Location is within the bounds of
    						the current Grid object, false is
    						returned if it is not
    		Informally: return whether or not the given
    					location lies within the current grid
    	*/
    	{
    		int r;
    		int c;
     
          	trace("validMove: validMove starts and ends");
     
    COMPLETE ME!!!
    	}
     
     
    	public boolean gameOver()
    	/*
    		Pre-condition: none
    		Post-condition: true is returned if the game is
    						over because the grid is empty
    		Informally: return whether or not the game is over
    	*/
    	{
    		int r,c;
    		Location l;
     
          	trace("gameOver: gameOver starts");
     
    COMPLETE ME!!!
     
          	trace("gameOver: gameOver ends");
      		return true;
        }
     
     
    	public String toString()
    	/*
    		Pre-condition: none
    		Post-condition: a String representation of the grid
    						is returned
    		Informally: find a String representation of the grid
    	*/
    	{
    		String s="[";
    		int r,c;
    		Location l;
     
     
          	trace("toString: toString starts");
     
    		for (r=1;r<=dimension;r++)
    		{
    			for (c=1;c<=r;c++)
    			{
    				l=new Location(r,c);
    				if (isEmpty(l))
    				{
    					s+=" ";
    				}
    				else
    				{
    					s+="X";
    				}
    			}
    			if (r != dimension)
    			{
    				s+=",";
    			}
    		}
     
    		s+="]";
     
          	trace("toString: toString ends");
    		return s;
    	}
     
     
    	public void showGrid(Display s)
    	/*
    		Pre-condition: the Screen parameter is correctly defined
    		Post-condition: the screen representation of the Grid
    						object is displayed on the given Screen
    		Informally: display the current grid
    	*/
    	{
    		int i;
     
          	trace("showGrid: showGrid starts");
     
    		for (i=0; i<DIMENSION; i++)
    		{
    			board[i].showSquare(s,getDimension());
    		}
     
    		trace("showGrid: grid is " + toString());
     
          	trace("showGrid: showGrid ends");
     	}
     
    	public void trace(String s)
    	/*
    		Provide trace output.
    		Pre-condition: none
    		Post-condition: if trace output is desired then the given String
    						parameter is shown on the console
    		Informally: show the given message for tracing purposes
    	*/
    	{
    		if (TRACING)
    		{
    			System.out.println("Grid: " + s);
    		}
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: help with defining a method

    Is this the same question?
    http://www.javaprogrammingforums.com...preciated.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: January 23rd, 2013, 07:29 AM
  2. [SOLVED] How to create a Java generic method, similar to a C++ template method?
    By Sharmeen in forum Object Oriented Programming
    Replies: 3
    Last Post: October 18th, 2012, 02:33 AM
  3. cannot find symbol error with defining a string
    By r3dApple in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 25th, 2012, 11:37 PM
  4. Defining a rational class. Simple but i cant figure it out to save my life
    By spetillo3 in forum Object Oriented Programming
    Replies: 3
    Last Post: October 26th, 2011, 11:02 PM
  5. [SOLVED] What do I need to do to use a bean in Netbeans 7 apart from defining it?
    By Lord Voldemort in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: July 31st, 2011, 12:29 AM