Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9111583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:32:26+00:00 2026-06-17T03:32:26+00:00

I currently have the following code below and I want to update the specific

  • 0

I currently have the following code below and I want to update the specific place in the 2D array with an X and then re-print the board. I am currently stuck where I need to update the object in the array to an X but I keep getting:

Type mismatch: cannot convert from String to Ship

What am I doing wrong? I have already placed all the objects on the board.

public void updateBoard(int row, int column, Ocean ocean){
    //get ship array
    Ship[][] sea = ocean.getShipArray();
    //mark the x/y co-ordinate with X 
    sea[row][column] = "X"; // PROBLEM  
    // print the board
}

I have included the Ship Class

public abstract class Ship {
// TODO add appropriate comments
@Getter
private int size;
private String type;
private String shortForm;

// TODO add appropriate comments
@Getter
@Setter(AccessLevel.PACKAGE)
private int bowRow;
@Getter
@Setter(AccessLevel.PACKAGE)
private int bowColumn;
@Getter
@Setter(AccessLevel.PACKAGE)
private boolean horizontal;

/**
 * An array of boolean which indicates whether that part of the ship has
 * been hit. This is initialised by the appropriate sub-class. Battleships
 * use all 4 locations; cruisers use the first 3; destroyers 2; submarines
 * 1; and "empty sea" 1.
 */
protected boolean[] hit;


/**
 * clears the hit array indicating whether that part of the "Ship" has been
 * hit
 */
protected Ship(int size, String type, String shortForm) {
    this.size = size;
    this.setType(type);
    this.shortForm = shortForm;
    hit = new boolean[size];
    for (int i = 0; i < hit.length; i++)
        hit[i] = false;
}

/**
 * Checks that ship of this size will not overlap another ship, or touch
 * another ship (vertically, horizontally, or diagonally) and that ship will
 * not "stick out" beyond the array.
 * 
 * @param row
 *            that will contain the bow
 * @param column
 *            that will contain the bow
 * @param horizontal = true if horizontal
 * @param ocean
 * @return true if it is okay to put a ship of this size with its bow in
 *         this location, with the given orientation.
 */
public boolean okToPlaceShipAt(int row, int column, boolean horizontal,
        Ocean ocean) {
    // Try to catch exception error if ship goes past board
    try{
        Ship ships[][] = ocean.getShipArray();

        for (int i = 0; i < getSize(); i++){
        if (!(ships[row][column] instanceof EmptySea)){ 
        //  System.out.println("Cant Print here + " + row + column + getSize());
                return false;
        }else{
            if (horizontal) {
                column++; 
            } else {
                row++;  
            }
        //  System.out.print(ships[row][column]);

            }
    //  System.out.println(" ");

        }
        return true;
    }
    catch(Exception err){
    //  System.out.println("OMG an error");
        return false;
    } 



}

/**
 * "places" the ship in the ocean, assigning values to the bowRow,
 * bowColumn, and horizontal. Places a reference to the ship in the ships
 * array in the Ocean object.
 * 
 * @param row
 *            to contain the bow
 * @param column
 *            to contain the bow
 * @param horizontal
 * @param ocean
 */
public void placeShipAt(int row, int column, boolean horizontal, Ocean ocean) {

    this.setBowRow(row);
    this.setBowColumn(column);
    this.setHorizontal(horizontal);

    Ship ships[][] = ocean.getShipArray();

    for (int i = 0; i < getSize(); i++) {
        // set position in array to contain the ship
        ships[row][column] = this;
        if (horizontal) {
            column++;
        } else {
            row++;
        }
    }
}

private int getSize() {
    // TODO Auto-generated method stub
    return size;
}

private void setHorizontal(boolean horizontal2) {
    // TODO Auto-generated method stub
    horizontal = horizontal2;
}

private void setBowColumn(int column) {
    // TODO Auto-generated method stub
    bowColumn = column;
}

private void setBowRow(int row) {
    // TODO Auto-generated method stub
    bowRow = row;
}

/**
 * If this ship has been hit, marks that part of the ship as "hit"
 * 
 * @param row
 *            User's supplied row shot
 * @param column
 *            User's supplied column shot
 * @return true if ship is hit, false otherwise
 */
public boolean shootAt(int row, int column) {
    if ((isHorizontal() && (row != getBowRow()))
            || (!isHorizontal() && (column != getBowColumn())))
        return false; // it's not a hit

    // it's a hit. Work out offset & set that position in hit array to true
    hit[(row - getBowRow() + column - getBowColumn())] = true;

            return true;
}

private int getBowRow() {
    // TODO Auto-generated method stub
    return bowRow;
}

private int getBowColumn() {
    // TODO Auto-generated method stub
    return bowColumn;
}

private boolean isHorizontal() {
    // TODO Auto-generated method stub
    return false;
}

/**
 * checks whether this ship is sunk - using the hit array
 * 
 * @return true if every part of the ship has been hit, false otherwise.
 */
public boolean isSunk() {

    for (boolean b : hit)
        if (!b)
            return false;

    return true;
}


/**
 * @return a single character String to use in Ocean's print method
 */
@Override
public String toString() {
    return shortForm;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}
public String setHit(String hit){
    return hit;
}

}

It is a bit long.
Below is a sample of one if the ships that extends Ship, the Submarine class. All the others are exactly the same except for their sizes.

/*
 * A Battleship class which extends ship
 * Four Submarines in the game Length 1
 */
package Battleships;
public class Submarine extends Ship {
    private final static int SIZE = 1;
    /**
     * sets the length & clears the hit array
     */
    public Submarine() {
        super(SIZE, "Submarine", "S");
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:32:27+00:00Added an answer on June 17, 2026 at 3:32 am

    sea is a Ship[][], "X" is a String. You can’t place a String in a variable that holds Ship.

    Edit:

    Since you’re trying to shoot at a ship, rather than place a new ship, I suspect what you actually need is to use the following function from ship…

    public boolean shootAt(int row, int column).

    From the code for Ship, I’m guessing what you actually need to do is…

    sea[row][column].shootAt(row,column);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have the following code which delays showing a fixed bar after a
Currently I have the following code to flash a DataGrid cell when it's value
I currently have the following js code function clearMulti(option) { var i; var select
The following code shows what I currently have. It is an adapter for circular
I have the following code currently: <DataTemplate DataType={x:Type vm:SectionViewModel}> <ScrollViewer> <ItemsControl ItemsSource={Binding ViewModels}> </ItemsControl>
I am currently working on Problem 62 I have tried the following code to
I currently have code that does the following: private final static ExecutorService pool =
hi i have the following code below, where i try to get all the
Anything I have tried didn't work. Currenly I have following code to change asp.net
I have following code return (EseshEntities.Current.Users.Select(u => new { Comunity = u.Apartment.Building.District.City })).ToList(); if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.