0
votes

In my class, we are assigned a CodeHS assignment called Battleship. I'm stuck on part 2 of 7, the Location class. The exercise is as follows:

The next class to write is the Location.java file. The Location class stores the information for one grid position.

A location has two defining attributes

1) Is there a ship at this location?

2) What is the status of this location?

The status of this would be whether this position is unguessed, we got a hit, or got a miss.

public static final int UNGUESSED = 0;
public static final int HIT = 1;
public static final int MISSED = 2;

These are the methods you will need to implement for the Location class.

// Location constructor. 
public Location()

// Was this Location a hit?
public boolean checkHit()

// Was this location a miss?
public boolean checkMiss()

// Was this location unguessed?
public boolean isUnguessed()

// Mark this location a hit.
public void markHit()

// Mark this location a miss.
public void markMiss()

// Return whether or not this location has a ship.
public boolean hasShip()

// Set the value of whether this location has a ship.
public void setShip(boolean val)

// Set the status of this Location.
public void setStatus(int status)

// Get the status of this Location.
public int getStatus()

And my work is as follows:

public class Location
{
    private int location;
    private int hit;
    private int miss;
    private boolean val;
    private int status;
    //Implement the Location class here
    public static final int UNGUESSED = 0;
    public static final int HIT = 1;
    public static final int MISSED = 2;

    // Location constructor. 
    public Location(int location){
        this.location = location;
    }

// Was this Location a hit?
public boolean checkHit(){
    if(location == HIT)
    return true;
    return false;
}

// Was this location a miss?
public boolean checkMiss(){
    if(location == MISSED)
    return true;
    return false;
}

// Was this location unguessed?
public boolean isUnguessed(){
    if(location == UNGUESSED)
    return true;
    return false;
}

// Mark this location a hit.
public void markHit(int hit){
    this.hit = hit;
}

// Mark this location a miss.
public void markMiss(int miss){
    this.miss = miss;
}

// Return whether or not this location has a ship.
public boolean hasShip(){
    return val;
}

    // Set the value of whether this location has a ship.
   public void setShip(boolean val){
    if(status == HIT)
    val = true;
    else 
    val = false;
}

// Set the status of this Location.
public void setStatus(int status){
    this.status = status;
}

// Get the status of this Location.
public int getStatus(){
    if(status == HIT)
    return HIT;
    else if (status == MISSED)
    return MISSED;
    else if(status == UNGUESSED)
    return UNGUESSED;
}

}

While I really wouldn't be surprised if I have errors elsewhere, my main problem is the setShip Boolean method. How am I supposed to set it to true (there is a ship in that position) or false (there is no ship). What I have was my best guess, but that only is true if tested after the "shot". And I think the exercise wants it to be tested before that.

3
1) Should the location be a Point (x,y)? 2) the field val should be changed to mean hasShip 3) If you used the setter/getter functionality you would see that you should just set and get the hasShip value 4) consider public boolean checkHit () {return localtion == HIT);} - Scary Wombat

3 Answers

1
votes

Thanks for the help guys. Using the feedback provided, I fixed the class to the following and it now works.

public class Location
{
    private int status;
    private boolean ship;
    //Implement the Location class here
    public static final int UNGUESSED = 0;
    public static final int HIT = 1;
    public static final int MISSED = 2;

    // Location constructor. 
    public Location(){
        status = UNGUESSED;
        ship = false;
    }

    // Was this Location a hit?
    public boolean checkHit(){
        if(status == HIT)
            return true;
        return false;
    }

    // Was this location a miss?
    public boolean checkMiss(){
        if(status == MISSED)
            return true;
        return false;
    }

    // Was this location unguessed?
    public boolean isUnguessed(){
        if(status == UNGUESSED)
            return true;
        return false;
    }

    // Mark this location a hit.
    public void markHit(){
        status = HIT;
    }

    // Mark this location a miss.
    public void markMiss(){
        status = MISSED;
    }

    // Return whether or not this location has a ship.
    public boolean hasShip(){
        return ship;
    }

    // Set the value of whether this location has a ship.
    public void setShip(boolean val){
        ship = val;
    }

    // Set the status of this Location.
    public void setStatus(int status){
        this.status = status;
    }

    // Get the status of this Location.
    public int getStatus(){
        return status;
    }
}
0
votes

First: Don't change the method-signatures you got from the exercise (e.g. the constructor shouldn't have a parameter, same goes for the markHit and markMiss methods.

You don't need to save your int miss and int hit in two different variables since a location can't be hit and missed at the same time, we therefore use a single variable status.

I'm pretty sure your question 1 and 2 are (more or less) the same thing since there's a ship if there's a hit and vice versa so you would only have an attribute int status = 0 so each location is UNCHECKED per default and you can read everything from there.

All you then have to do is to change the status variable on the markMissand markHit methods and call setShip accordingly.

Code example:

public void markMiss() {
    this.status = MISSED;
    setShip(false);
}

public void markHit() {
    this.status = HIT;
    setShip(true);
}

public boolean checkMiss() {
    return this.status == MISSED;
}

public boolean checkHit() {
    return this.status == HIT;
}

public boolean setShip(boolean val) {
    this.hasAShip = val;
}

public boolean hasShip() {
    return this.hasAShip;
}
0
votes

You have a private boolean val that you return with hasShip().

Your setShip(val) method should set the class variable with val, not calculate something based on stats.

It's just a simple setter method.

EDIT:

It's just like

// Mark this location a hit.
public void markHit(int hit){
    this.hit = hit;
}