Results 1 to 9 of 9
- 04-21-2011, 09:16 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
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.
is declared inside BattleShip.javaJava Code:char[][] ArrayPlayer= new char[10][10];
I try calling ArrayPlayer from Arrays.java with the following code:
And it tells me "Cannot find Variable: Symbol: ArrayPlayer Class: BattleShip"Java Code:for (i=0; i < BattleShip.ArrayPlayer.length; i++){ // Outer loop of the 2d array output
Is there something that I'm doing wrong here? Isn't ArrayPlayer contained in my BattleShip class?
- 04-21-2011, 09:55 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 04-21-2011, 11:42 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
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?
- 04-21-2011, 12:42 PM #4
sarevok9
Could please post your BattleShip.java and Arrays.java ?
I think you stuck with access modifier..Mak
(Living @ Virtual World)
- 04-21-2011, 12:57 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Certainly, but be warned, they are kinda long:
BattleShip.java
Arrays.JavaJava 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
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
Boat.JavaJava 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; }*/ }
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; } }
- 04-21-2011, 01:05 PM #6
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)
- 04-21-2011, 01:32 PM #7
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
- 04-21-2011, 01:36 PM #8
Member
- Join Date
- Dec 2009
- Posts
- 36
- Rep Power
- 0
place it outside the Main method
- 04-21-2011, 01:54 PM #9
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
Similar Threads
-
Help Displaying an Array
By Spyderpig in forum New To JavaReplies: 1Last Post: 03-15-2011, 01:08 PM -
Help displaying an array from a seperate class
By aeon1613 in forum New To JavaReplies: 2Last Post: 01-16-2011, 08:15 AM -
How to change color of area contained by rectangle
By thayalan in forum AWT / SwingReplies: 2Last Post: 06-04-2009, 04:48 AM -
loading files contained within app jar file
By thorne_ in forum New To JavaReplies: 3Last Post: 05-18-2009, 02:26 PM -
init() method displaying html
By reddzer in forum Java ServletReplies: 0Last Post: 11-10-2007, 07:20 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks