Thursday, December 24, 2009

Need help with java coding?

I'm lost with this homework assignment,can someone please help me?The program is supposed to place the ships randomly in the grid and randomly fire on it to see if it hits or sinks anything





package battleship;





public class Ship


{


public Ship(Fleet f, String kind, int length)


{


Ship a = new Ship(f,';Aircraft Carrier';,5);


Ship b = new Ship(f,';Battleship';,4);


Ship c = new Ship(f,';Cruiser';,3);


Ship p = new Ship(f,';Patrol Boat';,2);


Ship s = new Ship(f,';Submarine';,3);


}


public void takeDamage()


{


int count;


if(target(x,y)==true)


{


count++;


}


if(count==ships.length)


{


shipSunk();


}


// that's one hit.


// If we are sunk, notify fleet


}


}Need help with java coding?
At the very least, class Ship is farked. Your ';count'; in takeDamage() is erased as soon as the function ends, so ';count'; will never exceed a value of 1.





2)


if(target(x,y)==true)





A target() function that is called within Ship.takeDamage() is not defined in class Ship, and Ship does not subclass anything, so it should throw an error. You do have a target() function in your Grid class, but Ship can't access that without more direction.





3)


Ship a = new Ship(f,';Aircraft Carrier';,5);


Ship b = new Ship(f,';Battleship';,4);


Ship c = new Ship(f,';Cruiser';,3);


.


.


.


These initialization of different Ships probably should not be in the Ship class itself. The initializations should be in the Class that USES the ships (looks like that would be class Fleet)








4)


Ship[][] array = new Ship[WIDTH][HEIGHT];


This line in the Grid class seems very strange. Why do you need a Ship for every spot on the grid? 10*10 ships??





5) Fleet.placeRandomly(Grid g)


is not going to work as intended. Notice that the code in the function is not even using the Grid that is passed to it.








Overall, there seems to be confusion about how the program should ';flow'; ... and how the objects should work with each other.





The basic gist is that a Grid should have Fleets and that Fleets should have Ships. Grids should not know about Ships .. everything from the Grid level (shots fired) should flow through the Fleets.

No comments:

Post a Comment