Results 1 to 9 of 9
  1. #1
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default Displaying an array using a method contained in another class.

    In a joyous attempt to start getting used to encapsulation in one of my programs I've attempted to toss everything pertaining to the arrays into a separate class since it was taking up a LOT of room. I could realistically put the method back into my main class, but I'd really rather the practice since I suppose I'll need to know this stuff someday.

    Here's the basic idea I'm running with right now.

    Java Code:
    char[][] ArrayPlayer= new char[10][10];
    is declared inside BattleShip.java

    I try calling ArrayPlayer from Arrays.java with the following code:
    Java Code:
    for (i=0; i < BattleShip.ArrayPlayer.length; i++){ // Outer loop of the 2d array output
    And it tells me "Cannot find Variable: Symbol: ArrayPlayer Class: BattleShip"

    Is there something that I'm doing wrong here? Isn't ArrayPlayer contained in my BattleShip class?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You are accessing the ArrayPlayer array (a better name would be arrayPlayer or playerArray) as if it were static but the line you posted declares a nonstatic variable.

  3. #3
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default

    I don't really understand what you mean here. In the "main" statement it says "public static void main(String[] args){" So isn't that declaring it in a static location?

  4. #4
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    444
    Rep Power
    6

    Default

    sarevok9

    Could please post your BattleShip.java and Arrays.java ?

    I think you stuck with access modifier..
    Mak
    (Living @ Virtual World)

  5. #5
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default

    Certainly, but be warned, they are kinda long:
    BattleShip.java
    Java Code:
    /**
     *
     */
    import java.util.*;
    public class BattleShip {
    public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    
    System.out.println("Welcome to battleship\n\n");
    char[][] ArrayPlayer= new char[10][10]; // Create an Array for the player
    Arrays MyPlayers = new Arrays(ArrayPlayer); // Set MyPlayers object to contain array ArrayPlayer, called from Arrays.java
    
     //Patrol Boat
    Boat PatrolBoat = new Boat();
    PatrolBoat.health=2;
    PatrolBoat.name="Patrol Boat";
    PatrolBoat.letter='p';
    
    //Submarine
    Boat Submarine = new Boat();
    Submarine.health=3;
    Submarine.name="Submarine";
    Submarine.letter='s';
    
    //Destroyer
    Boat Destroyer = new Boat();
    Destroyer.health=4;
    Destroyer.name="Destroyer";
    Destroyer.letter='d';
    
    //Battleship
    Boat BattleShip = new Boat();
    BattleShip.health=5;
    BattleShip.name="BattleShip";
    BattleShip.letter='b';
    
    Boat SelectedBoat; // Create a boat called Selected Boat
    Boat[] boatarray = {PatrolBoat, Submarine, Destroyer, BattleShip}; // Boat array used in select, boat here gets applied to selectedboat
    int iSelect, x=0, y=0, i=0, j=0, iOrientation; // initialize heaps of variables
    System.out.println("Press 1 to place your patrol boat, 2 to place your submarine, 3 to place your destroyer, or 4 to place your battleship.");//Prompt user
    iSelect =input.nextInt(); // Get value for select from array
    SelectedBoat=boatarray[iSelect-1]; // Select boat from array
    System.out.println("Press 1 for horizontal, 2 for vertical: "); // Prompt for horiz or vert.
    iOrientation = input.nextInt(); // Get horiz or vert
    while (iOrientation !=1 || iOrientation !=2){
         System.out.println("You have selected an incorrect value, try again: ");
         iOrientation=input.nextInt();
    }
    System.out.println("Choose where you would like the piece to appear on a grid (A1 is top left J10 is bottom right): ");
    String c="";//Empty string / create string / dereference string
    c = input.next(); // Allow user to guess
    SelectedBoat.location = Boatloc.BoatLoc(c); // Transform String from letter / number to 2 numbers for grid placement
    y=SelectedBoat.location[0]; // Set y to y value
    x=SelectedBoat.location[1];// Set x to X value.
    
    if (iOrientation == 1){ // If orientation is vertical
    for (i=0; i <SelectedBoat.health; i++){ // While < health, place more
    MyPlayers.AddBoat(y,x,SelectedBoat.letter);// Add boat letter at selected location
    y++;//Increment the y variable
    }// end placement for
    }// End vert orientation if
    
    if (iOrientation == 2){//If boat orientation is horizontal, place as such
    for (i=0; i <SelectedBoat.health; i++){ // While < health, place more
    MyPlayers.AddBoat(y,x,SelectedBoat.letter);// Add boat letter at selected location
    x++;//Increment the y variable
    }// end placement for
    }// End vert orientation if
    
    
    
    //End player graph output.
    }// End main
    }// End Class
    Arrays.Java
    Java Code:
    /**
     *
     */
    
    public class Arrays {
        private char[][] Arrays;
        private boolean used;
        
        Arrays(char[][] playerArray) {
            Arrays = playerArray;
        }
        public boolean isUsed() {
            return used;
        }
        public void AddBoat(int x, int y, char boatLetter) {
            Arrays[x][y] = boatLetter;
        }
        public void playermap(){
            //Player Grid Output.
    int i=0, j=0;
    System.out.println("____________Player setup:____________\n"); // Top of the 'player positioning' box.
    System.out.println("         A B C D E F G H I J");  // Top of grid letters
    System.out.println("");// blank line
    for (i=0; i < BattleShip.ArrayPlayer.length; i++){ // Outer loop of the 2d array output
    if (i< 9){
    System.out.print("|  "+ (i+1) +"     "); // Left side of the box
    }
    else{
        System.out.print("|  "+ (i+1) +"    ");
    }
    for (j=0; j<10; j++){ // Inner loop of array
    System.out.print(BattleShip.ArrayPlayer[i][j]+ " "); // Array output
    }// End inner for.
    System.out.println("       |"); // Right side of playermap box
    }//End outer for
    System.out.println("______________________________________"); // Formatting 
        }
    }

    Boatloc.Java
    Java Code:
    /**
     *
     */
    public class Boatloc {
    public static int[] BoatLoc (String location){
     int y = Character.toLowerCase(location.charAt(0))-97;
     int z= Character.toLowerCase(location.charAt(1))-49;
     int[] x = {y, z};
    return x;
    }
    /*
    public int isCollide(int x, int y){
        if (Arrays.isUsed==1){
            collide = l
        }
        else{
            collide=0;
        }
        return collide;
    }*/
    }
    Boat.Java
    Java Code:
    /**
     *
     */
     public class Boat{
     private int numBoats;
     public int health;
     public int orientation;
     public char letter;
     public int[] location;
     public String name;
     
     public int getnumboats(){
         return numBoats;
     }
     
     
     }

  6. #6
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    444
    Rep Power
    6

    Default

    You have declared char[][] ArrayPlayer= new char[10][10]; as local variable in Main method. So you can t use that in Arrays.java .
    Mak
    (Living @ Virtual World)

  7. #7
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default

    Quote Originally Posted by makpandian View Post
    You have declared char[][] ArrayPlayer= new char[10][10]; as local variable in Main method. So you can t use that in Arrays.java .

    So how would I go about fixing this?

  8. #8
    jojo is offline Member
    Join Date
    Dec 2009
    Posts
    36
    Rep Power
    0

    Default

    place it outside the Main method

  9. #9
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default

    I got around this in a totally different way.
    battleship.java
    *snip*
    Arrays.DisplayMap(ArrayPlayer);

    Arrays.java
    *snip*
    public static void DisplayMap(char[][] Array)

    I feel like an idiot not thinking of that on my own. Thanks for pointing out my mistake though guys.

Similar Threads

  1. Help Displaying an Array
    By Spyderpig in forum New To Java
    Replies: 1
    Last Post: 03-15-2011, 01:08 PM
  2. Help displaying an array from a seperate class
    By aeon1613 in forum New To Java
    Replies: 2
    Last Post: 01-16-2011, 08:15 AM
  3. How to change color of area contained by rectangle
    By thayalan in forum AWT / Swing
    Replies: 2
    Last Post: 06-04-2009, 04:48 AM
  4. loading files contained within app jar file
    By thorne_ in forum New To Java
    Replies: 3
    Last Post: 05-18-2009, 02:26 PM
  5. init() method displaying html
    By reddzer in forum Java Servlet
    Replies: 0
    Last Post: 11-10-2007, 07:20 PM

Posting Permissions

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