in the battleship class there are few errors that need help
in the battleship class there are few errors that need help to fix
//there is an error with length
for(int i=0;i<this.hit.length();i++)
//another error with length
if(count==hit.length())
//at the end of the class battleship there is an error //with
hit = arr;
_______________________________________
public abstract class Ship {
static boolean ShipPlacedAt(int r, int c, boolean h, Ocean aThis) {
throw new UnsupportedOperationException(\"Not supported yet.\"); //To change body of generated methods, choose Tools | Templates.
}
int bowRow;
int bowColumn;
int length;
boolean horizontal;
boolean []hit = new boolean[4];
public int getBowRow() {
return bowRow;
}
public void setBowRow(int a) {
this.bowRow = a;
}
public int getBowColumn() {
return bowColumn;
}
public void setBowColumn(int a) {
this.bowColumn = a;
}
public boolean isHorizontal() {
return horizontal;
}
public void setHorizontal(boolean a) {
this.horizontal = a;
}
int getLength(){
return length;
}
public Ship() {
}
abstract String getShipType();
boolean okToPlaceShipAt(int a, int b, boolean c, Ocean d){
if(c){
if(b>=0&&(b+this.getLength()-1)<=9&&a>=0&&a<=9){
for( int i=a-1;i<a+2;i++){
for( int j=b-1;j<b+this.getLength()+1;j++){
if(i>=0&&i<=9&&j>=0&&j<=9){
if(d.isOccupied(i, j))
{
return false;
}
}
}
}
return true;
}
}
else{
if(a>=0&&(a+this.getLength()-1)<=9&&b>=0&&b<=9){
for( int i=a-1;i<a+this.getLength()+1;i++){
for( int j=b-1;j<b+2;j++){
if(i>=0&&i<=9&&j>=0&&j<=9){
if(d.isOccupied(i, j))
{
return false;
}
}
}
}
return true;
}
}
return false;
}
void placeShipAt(int a, int b, boolean c, Ocean d){
this.setBowRow(a);
this.setBowColumn(b);
this.setHorizontal(c);
if(c){
for(int i=0;i<this.length;i++){
d.getShipArray()[a][b+i]=this;
}
}
else{
for(int i=0;i<this.length;i++){
d.getShipArray()[a+i][b]=this;
}
}
}
boolean shootAt(int a, int b){
int position;
if(!this.isSunk()){
if(this.isHorizontal()){
if(b>=this.bowColumn&&b<(this.bowColumn+this.length)&&this.bowRow==a){
position=b-this.bowColumn;
this.hit[position]=true;
return true;
}
}else{
if(a>=this.bowRow&&a<(this.bowRow+this.length)&&this.bowColumn==b){
position=a-this.bowRow;
this.hit[position]=true;
}
return true;}
}
return false;
}
boolean isSunk(){
int m=0;
for(int i=0;i<this.hit.length;i++){
if(this.hit[i]){
m++;
}
if(m==this.length){
return true;
}
}
return false;
}
}
public class Battleship extends Ship {
private String battleship;
public Battleship() {
}
public void setLength(int length) {
this.length = 4;
}
public void setHit(boolean[] hit) {
this.hit = hit;
}
String getShipType(){
return battleship;
}
public String toString()
{
int count=0;
for(int i=0;i<this.hit.length();i++)
{
count++;
}
if(count==hit.length())
return \"x\";
else return \"S\";
}
Battleship(int len,int[] arr)
{
length = len;
hit = arr;
}
}
Solution
length() is a string method , in the conditional statements and looping statements no need to write \'length()\' so change to length so insted of for(int i=0;i<this.hit.length();i++) write
for(int i=0;i<this.hit.length;i++)
in second error also change to
if(count==hit.length)
for the 3rd error you take hit type as boolean and arr type as int thats why error occur so,
you should change either type of hit type to int[] or type of arr type to boolean[]
Hope it helps you





