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.
Point(x,y)? 2) the fieldvalshould be changed to meanhasShip3) If you used the setter/getter functionality you would see that you should just set and get thehasShipvalue 4) considerpublic boolean checkHit () {return localtion == HIT);}- Scary Wombat