Results 1 to 4 of 4
  1. #1
    javanewbie82 is offline Member
    Join Date
    Oct 2016
    Posts
    13
    Rep Power
    0

    Default How do I use a print method to print an object?

    I have a print method to print a "game board" and a method to create a board. how can I use main to print it out?

    Java Code:
    public static void printBoard(MemoryMatch [][] memoryMatch)
    	{
    		System.out.println("\t\t1\t2\t3\t4");
    		System.out.println("\t---------------------------");
    		for(int i =0; i < memoryMatch.length; i++)
    		{
    			System.out.print((i + 1) + "\t|t");
    			for(int j = 0; j < memoryMatch.length; j++)
    			{
    				if(memoryMatch[i][j].isRevealed())
    				System.out.print(memoryMatch[i][j].getValue()
    				+ "\t");
    				else
    				System.out.print("\t");
    			}
    			System.out.println();
    		}
    public static MemoryMatch[][] createBoard()
    {
    	MemoryMatch[][] board = new MemoryMatch[4][4];
    	
    	for(int i = 1; i <= 8; i++)
    	{
    		for(int j = 1; j<=2; j++)
    		{
    			boolean selectedLocation = false;
    			while(!selectedLocation)
    			{
    				int row = (int)(Math.random() * 4);
    				int collum = (int)(Math.random() * 4);
    				if(board[row][collum] == null)
    				{
    					selectedLocation = true;
    					board[row][collum] = new MemoryMatch(i);
    				}
    			}
    		}

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How do I use a print method to print an object?

    how can I use main to print it out?
    Are you asking how you can call a class's method from the main() method?
    Normal way to call a method, get a reference to an instance of the class and use that to call the method: classRef.theMethodToCall(theArgs);
    If the method is static: ClassName.theMethodToCall(theArgs);
    If not please explain.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    javanewbie82 is offline Member
    Join Date
    Oct 2016
    Posts
    13
    Rep Power
    0

    Default Re: How do I use a print method to print an object?

    Got it. I was confused since I was trying to call a static method with a reference

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How do I use a print method to print an object?

    trying to call a static method with a reference
    The compiler should give you a warning and tell you how to correct it.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Javax Print Attribute for Selection Print Range
    By rsawatzky in forum AWT / Swing
    Replies: 0
    Last Post: 04-26-2012, 12:14 AM
  2. help please: print specific object in ArrayList
    By nignogs in forum New To Java
    Replies: 6
    Last Post: 04-09-2011, 09:12 AM
  3. How to print PreparedStatment object in console
    By sasi.tati in forum AWT / Swing
    Replies: 1
    Last Post: 08-21-2010, 06:16 AM
  4. How to print an object-filled array?
    By Verna_Venisa in forum New To Java
    Replies: 1
    Last Post: 10-20-2008, 08:09 PM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •