2
\$\begingroup\$

I have thisPoint Class:

public class Point
{
    private final int CONSTANT_VALUE = 0;
    private int _x;
    private int _y;

    public Point(int x, int y)
    {
         _x = x;
             _y = y;
    }

    public Point(Point other)
    {
          _x = other._x;
          _y = other._y;
    }

public int getX()
{
    return _x;
}

public int getY()
{
    return _y;
}

public void setX(int x)
{
    if (x < 0)
        _x = CONSTANT_VALUE;
    else
        _x = x;
}

public void setY(int y)
{
    if (y < 0)
        _x = CONSTANT_VALUE;
    else
        _y = y;
}

public void move (int dX, int dY)
{
    int newXValue = _x + dX; 
    int newYValue = _y + dY;

    if (newXValue >= 0 || newYValue >= 0)
    {
        _x = _x + dX;
        _y = _y + dY;
    }   
}
}

And City Class:

public class City
{
    private String _cityName;
    private Point _cityCenter;
    private Point _centralStation;
    private long _numOfResidents;
    private int _noOfNeighborhoods;
    private final long RESIDENTS_CONST_VALUE = 0;
    private final int NEIGHBORHOODS_CONST_VALUE = 1;

    public City(String cityName, Point cityCenter,
        Point centralStation, long numOfResidents, int noOfNeighborhoods)

    {
        _cityName = cityName;
        _cityCenter = cityCenter;
        _centralStation = centralStation;
        if (numOfResidents < 0)
            _numOfResidents = RESIDENTS_CONST_VALUE;
        else
            _numOfResidents = numOfResidents;
        if (noOfNeighborhoods <= 0)
            _noOfNeighborhoods = NEIGHBORHOODS_CONST_VALUE;
        else                
            _noOfNeighborhoods = noOfNeighborhoods;;
    }

    public City(City other)
    {
        _cityName = other._cityName;
        _cityCenter = new Point(other._cityCenter);
        _centralStation= new Point(other._centralStation);
        _numOfResidents = other._numOfResidents;
        _noOfNeighborhoods = other._noOfNeighborhoods;      
    }

    public String getCityName()
    {
        return _cityName;
    }

    public Point getCityCenter()
    {
        return _cityCenter;
    }

    public Point getCentralStation()
    {
        return _centralStation;
    }

    public long getNumOfResidents()
    {
        return _numOfResidents;
    }

    public int getNoOfNeighborhoods()
    {
        return _noOfNeighborhoods;
    }

    public void setCityName(String cityName)
    {
        _cityName = cityName;
    }

    public void setCityCenter(Point cityCenter)
    {
        _cityCenter = cityCenter;
    }

    public void setCentralStation(Point centralStation)
    {
        _centralStation = centralStation;
    }

    public void setNumOfResidents(long numOfResidents)
    {
        _numOfResidents = numOfResidents;
    }

    public void setNoOfNeighborhoods(int noOfNeighborhoods)
    {
        _noOfNeighborhoods = noOfNeighborhoods;
    }

    public String toString()
    {
        return "City Name: " + _cityName + "\n" + 
                "City Center: " + _cityCenter + "\n" +
                "Central Station: " + _centralStation + "\n" +
                "Number of Residents: " + _numOfResidents + "\n" +
                "Number of Neighborhoods: " + _noOfNeighborhoods;               
    }

    public boolean addResidents(long residentsUpdate)
    {
        long newResidentAmount = _numOfResidents + residentsUpdate;

        if (newResidentAmount < 0)
        {
            _numOfResidents = 0;
            return false;
        }
        else
        {
            _numOfResidents = newResidentAmount;
            return true;
        }
    }

    public void moveCentralStation(int deltaX,int deltaY)
    {
        _centralStation.move(deltaX, deltaY);
    }

    public double distanceBetweenCenterAndStation()
    {
        return _cityCenter.distance(_centralStation);
    }

    public City newCity(String newCityName, int dX, int dY)
    {
        Point newCityCenterPoint = new Point(_cityCenter);
        newCityCenterPoint.move(dX, dY);

        Point newCityCentralStationPoint = new Point(_centralStation);
        newCityCentralStationPoint.move(dX, dY);

        return new City(
                newCityName, 
                newCityCenterPoint,
                newCityCentralStationPoint, 
                0, 
                1);
    }
}

My question:

  1. Point class: method public Point(Point other) - do i need to write this i other way to avoid aliasing ?

  2. what about the rest get and set method from both Point and City classes ? i need to write it in other way or this is OK ?

\$\endgroup\$
3
  • \$\begingroup\$ I personally prefer the short if, if the code is so short: _x = x < 0 ? CONSTANT_VALUE : x;, but this is a matter of taste. But By the way, CONSTANT_VALUE is not defined anywhere, so you should define it and give it a meaningful name. \$\endgroup\$ – 0xBADF00D Nov 15 '13 at 10:10
  • 3
    \$\begingroup\$ why are you prefixing "_" with the variables ? \$\endgroup\$ – Kinjal Nov 15 '13 at 10:41
  • \$\begingroup\$ Kinjal: This is my homework and this the way i have to define it @user hichaeretaqua: CONSTANT_VALUE added, i forgot it \$\endgroup\$ – user2976270 Nov 15 '13 at 11:03
1
\$\begingroup\$
  • There looks to be a bug in setY: it assigns to _x instead of _y.

  • This code can avoid aliasing problems on small classes like Point by making them immutable. That is, remove the setX and setY methods from Point. Any time a change needs to be made to a point, construct a new Point entirely.

moveCentralStation then becomes:

class Point {
    ...
    Point makeMovedPoint(int deltaX, int deltaY) {
        return new Point(_x + deltaX, _y + deltaY);
    }
    ...
}

class City {
    ...
    void moveCentralStation(int deltaX, int deltaY) {
        _centralStation = _centralStation.makeMovedPoint(deltaX, deltaY);
    }
    ...
}

City can be made immutable in a similar fashion. However, that might end up crossing a line and make things less maintainable.

\$\endgroup\$
1
  • \$\begingroup\$ Point move added, i cannot remove setX and setY methods from Point because that what i need to write (homework) and i cannot add new method except what define in my homework \$\endgroup\$ – user2976270 Nov 15 '13 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.