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 12 of 12

Thread: Please help with code for a method, much appreciated!!

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

    Default Please help with code for a method, much appreciated!!

    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;
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Please help with code for a method, much appreciated!!

    What questions do you have about the code?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Please help with code for a method, much appreciated!!

    thanks for the reply but i am unsure how to implement the code to define the method

    /*
    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;
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Please help with code for a method, much appreciated!!

    What does the code need to do?
    What variables does it have to get values from?
    What decisions can it make after looking at the values of those variables?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Please help with code for a method, much appreciated!!

    I have to fill in the areas where is marked "Complete me!!!" I cant do any of them!! any help would be much appreciated, thanks

    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!!!
          	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!!!
    	}
     
     
    	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!!!
     
          	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!!!
    	}
     
     
    	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!!!
     
          	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!!!
    	}
     
     
    	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);
    		}
    	}
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Please help with code for a method, much appreciated!!

    Did you see my questions about what the code is supposed to do? I'll repeat them in case you missed them:

    What variables does the method have to get values from?
    What decisions can it make after looking at the values of those variables?
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    mrjavajava (January 29th, 2013)

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

    Default Re: Please help with code for a method, much appreciated!!

    the variables values have to be gotten from are: int r, c and location l. I assume the boolean returns true if there are null in all values of the grid at locations l from the r (row) and c (column) arrays? Sorry but am very new to java language

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Please help with code for a method, much appreciated!!

    am very new to java language
    Then you should start at the beginning by studying the tutorial and come back to this assignment when you understand a little more.

    Some links:
    The Really Big Index
    Trail: Learning the Java Language: Table of Contents (The Java™ Tutorials)


    You need to put more effort into the assignment and write some code
    or ask some specific questions about your problems. No one here is going to do your assignment for you.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Please help with code for a method, much appreciated!!

    great. you legend

  11. #10
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Please help with code for a method, much appreciated!!

    Quote Originally Posted by mrjavajava View Post
    great. you legend
    That's the best thing you've said so far.

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

    Default Re: Please help with code for a method, much appreciated!!

    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

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Please help with code for a method, much appreciated!!

    I'm having a bit of trouble
    Can you explain your problems?
    Does the code compile without errors? If not post the full text of the errors.
    Does the code execute without errors?
    Does the code produce the wrong results? Post the results and explain what is wrong with it.

    how to implement square q.
    Can you explain? square q?
    If you don't understand my answer, don't ignore it, ask a question.