help creating and using class methods

trying to obtain help with a specific class and looking for tips

Page 1 of 1

7 Replies - 775 Views - Last Post: 19 April 2010 - 10:13 PM Rate Topic: -----

#1 Guest_Grant*


Reputation:

help creating and using class methods

Posted 19 April 2010 - 04:43 PM

i am trying to create a very basic connect four game here are my two classes
	class Piece
	{
		private int owner;
		private String place;

		public Piece()
		{
			owner = Connect4.NOPLAYER;

		}
		public String Print()
		{
			if (owner == Connect4.PLAYER1)
				place = |X|;
			else
			if (owner == Connect4.PLAYER2)
				place = |O|;
			else
				place = | |;

			return place;
		}
	}

	class Board
	{
		private Piece[][] board = new Piece[8][8];
		private Piece owner;
		public Board()
		{
			for(int row=0;row < board.length;row++)
				for(int col=0;col < board[row].length; col++)
				{
					board[row][col] = new Piece();
				}

		}

		public int check_win()
		{
			int result = Connect4.PLAYER1;
			return result;
		}

		public Piece print()
		{
			for(int block = 0; block < board.length;block++)
				for(int lock = 0; lock <board[block].length; lock++)
					{
						Piece.print()= owner;
						return owner;
					}

		}
	}



the piece class will create piece objects that when empty should look like this | |
and when displayed in the game should form an 8x8 grid that looks like this
|0||1||2||3||4||5||6||7|
| || || || || || || || |
| || || || || || || || |
| || || || || || || || |
| || || || || || || || |
| || || || || || || || |
| || || || || || || || |
| || || || || || || || |
| || || || || || || || |

how ever i'm having trouble calling the print method from the Piece class when i wanna use it in the Board class to create that board.

any help with this problem would be fantastic as well as general tips for creating classes and methods for said classes. thank you

Is This A Good Question/Topic? 0

Replies To: help creating and using class methods

#2 Guest_Grant*


Reputation:

Re: help creating and using class methods

Posted 19 April 2010 - 05:30 PM

View PostGrant, on 19 April 2010 - 03:43 PM, said:

i am trying to create a very basic connect four game here are my two classes
	class Piece
	{
		private int owner;
		private String place;

		public Piece()
		{
			owner = Connect4.NOPLAYER;

		}
		public String Print()
		{
			if (owner == Connect4.PLAYER1)
				place = |X|;
			else
			if (owner == Connect4.PLAYER2)
				place = |O|;
			else
				place = | |;

			return place;
		}
	}

	class Board
	{
		private Piece[][] board = new Piece[8][8];
		private Piece owner;
		public Board()
		{
			for(int row=0;row < board.length;row++)
				for(int col=0;col < board[row].length; col++)
				{
					board[row][col] = new Piece();
				}

		}

		public int check_win()
		{
			int result = Connect4.PLAYER1;
			return result;
		}

		public Piece print()
		{
			for(int block = 0; block < board.length;block++)
				for(int lock = 0; lock <board[block].length; lock++)
					{
						Piece.print()= owner;
						return owner;
					}

		}
	}



the piece class will create piece objects that when empty should look like this | |
and when displayed in the game should form an 8x8 grid that looks like this
|0||1||2||3||4||5||6||7|
| || || || || || || || |
| || || || || || || || |
| || || || || || || || |
| || || || || || || || |
| || || || || || || || |
| || || || || || || || |
| || || || || || || || |
| || || || || || || || |

how ever i'm having trouble calling the print method from the Piece class when i wanna use it in the Board class to create that board.

any help with this problem would be fantastic as well as general tips for creating classes and methods for said classes. thank you


i should mention that this is still a work in progress and doesn't have all of it's methods i have also not included my main method with these classes
Was This Post Helpful? 0

#3 Guest_Grant*


Reputation:

Re: help creating and using class methods

Posted 19 April 2010 - 05:35 PM

This is the error i am running into with my code



C:\Users\Grant\Documents\Connect4.java:98: cannot find symbol
symbol : method print(Piece[][])
location: class Piece
Piece.print(board)= owner;
^
1 error

Tool completed with exit code 1
Was This Post Helpful? 0

#4 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 519
  • Joined: 14-January 09

Re: help creating and using class methods

Posted 19 April 2010 - 05:44 PM

'Piece.Print' is declared as a method starting with an uppercase letter.

You're calling it as a method starting with a lowercase letter.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Grant*


Reputation:

Re: help creating and using class methods

Posted 19 April 2010 - 05:54 PM

View PostGhlavac, on 19 April 2010 - 04:44 PM, said:

'Piece.Print' is declared as a method starting with an uppercase letter.

You're calling it as a method starting with a lowercase letter.


i should've caught that on my own however it's fixed now but i am still receiving errors

this is an updated version of my code (I've changed a little bit of it

i am also receiving a new error

	class Piece
	{
		private int owner;
		private String place;
		private Piece GamePiece;
		public Piece()
		{
			owner = Connect4.NOPLAYER;

		}
		public Piece Print(Piece name)
		{
			name = GamePiece;
			if (owner == Connect4.PLAYER1)
				place = "|X|";
			else
			if (owner == Connect4.PLAYER2)
				place = "|O|";
			else
				place = "| |";


			return GamePiece;
		}
	}

	class Board
	{
		private Piece[][] board = new Piece[8][8];
		private Piece owner;
		public Board()
		{
			for(int row=0;row < board.length;row++)
				for(int col=0;col < board[row].length; col++)
				{
					board[row][col] = new Piece();
				}

		}

		public int check_win()
		{
			int result = Connect4.PLAYER1;
			return result;
		}

		public Piece print()
		{
			for(int block = 0; block < board.length;block++)
				for(int lock = 0; lock <board[block].length; lock++)
					{
						Piece.Print(board[block][lock]);
						return board[block][lock];
					}

		}
	}



this is the new error
C:\Users\Grant\Documents\Connect4.java:98: Print() in Piece cannot be applied to (Piece)
Piece.Print(board[block][lock]);
____^
1 error

Tool completed with exit code 1
Was This Post Helpful? 0

#6 Guest_Grant*


Reputation:

Re: help creating and using class methods

Posted 19 April 2010 - 05:57 PM

View PostGrant, on 19 April 2010 - 04:54 PM, said:

View PostGhlavac, on 19 April 2010 - 04:44 PM, said:

'Piece.Print' is declared as a method starting with an uppercase letter.

You're calling it as a method starting with a lowercase letter.


i should've caught that on my own however it's fixed now but i am still receiving errors

this is an updated version of my code (I've changed a little bit of it

i am also receiving a new error

	class Piece
	{
		private int owner;
		private String place;
		private Piece GamePiece;
		public Piece()
		{
			owner = Connect4.NOPLAYER;

		}
		public Piece Print(Piece name)
		{
			name = GamePiece;
			if (owner == Connect4.PLAYER1)
				place = "|X|";
			else
			if (owner == Connect4.PLAYER2)
				place = "|O|";
			else
				place = "| |";


			return GamePiece;
		}
	}

	class Board
	{
		private Piece[][] board = new Piece[8][8];
		private Piece owner;
		public Board()
		{
			for(int row=0;row < board.length;row++)
				for(int col=0;col < board[row].length; col++)
				{
					board[row][col] = new Piece();
				}

		}

		public int check_win()
		{
			int result = Connect4.PLAYER1;
			return result;
		}

		public Piece print()
		{
			for(int block = 0; block < board.length;block++)
				for(int lock = 0; lock <board[block].length; lock++)
					{
						Piece.Print(board[block][lock]);
						return board[block][lock];
					}

		}
	}



C:\Users\Grant\Documents\Connect4.java:99: non-static method Print(Piece) cannot be referenced from a static context
Piece.Print(board[block][lock]);
>>>>^
1 error

Tool completed with exit code 1



EDIT: posted wrong error
Was This Post Helpful? 0

#7 Guest_Grant*


Reputation:

Re: help creating and using class methods

Posted 19 April 2010 - 09:19 PM

bump?
Was This Post Helpful? 0

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10669
  • View blog
  • Posts: 39,634
  • Joined: 27-December 08

Re: help creating and using class methods

Posted 19 April 2010 - 10:13 PM

The Print() method was not declared as static, so you have to invoke it from a Piece object, not the Piece class. Like so:
Piece p = new Piece();
p.Print(board[block][lock]); //legal


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1