זה התוכנית השלמה:
public class Country
{
private String _countryName;
private int _noOfCities;
private City[] _cities;
private final int MAX_NUM_CITIES = 1000;
public Country(String name)
{
// initialise instance variables
_countryName = name;
_cities = new City[MAX_NUM_CITIES];
_noOfCities = 0;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public boolean addCity(String cityName, double xCityCenter, double yCityCenter, double xCentralStation, double yCentralStation, int numOfResidents, int numOfNeighborhood)
{
// put your code here
if(_noOfCities < MAX_NUM_CITIES)
{
_cities[_noOfCities] = new City(cityName, xCityCenter, yCityCenter, xCentralStation, yCentralStation, numOfResidents, numOfNeighborhood);
_noOfCities++;
return true;
}
return false;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public int getNumOfResidents()
{
// put your code here
int count = 0;
for(int i = 0;i<_noOfCities;i++)
{
count += _cities.getNumOfResidents();
}
return count;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
private double distanceCenters(City a, City b)
{
// put your code here
Point centerA = a.getCityCenter();
Point centerB = b.getCityCenter();
double distance = centerA.distance(centerB);
return distance;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public double longestDistance()
{
// put your code here
if (_noOfCities<2)
{
return 0;
}
else
{
boolean found = true;
double longest = 0;
for(int i = 0;i<_noOfCities;i++)
{
for(int j =1;j<_noOfCities;j++)
{
double distance =distanceCenters(_cities , _cities[j]);
if(distance>longest)
{
longest = distance;
}
}
}
return longest;
}
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
private City findCity(String cityName)
{
// put your code here
for (int i = 0;i<_noOfCities;i++)
{
String name = _cities.getCityName();
if(name.equals(cityName))
{
return _cities;
}
}
return null;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public String citiesNorthOf(String nameCity)
{
// put your code here
City city = findCity(nameCity);
Point center = city.getCityCenter();
String x = "The cities north of" +nameCity+ "are: "+"\n";
String res ="The cities north of " +nameCity+ " are: "+"\n";
if(city.equals(null))
{
return "There is no city with the name "+ nameCity;
}
for(int i = 0;i<_noOfCities;i++)
{
if (_cities.getCityCenter().isAbove(center))
{
res = res + _cities.toString()+"\n"+"\n";
}
}
if(res.equals(x))
{
return "There are no cities north of " + nameCity;
}
else
{
return res;
}
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public City southernmostCity()
{
// put your code here
City min = _cities[0];
for(int i = 1;i<_noOfCities;i++)
{
Point center = _cities.getCityCenter();
if (center.isUnder(min.getCityCenter()))
{
min = _cities;
}
}
if(_noOfCities == 0)
{
return null;
}
else
{
return min;
}
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public String getCountryName()
{
// put your code here
return _countryName;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public int getNumOfCities()
{
// put your code here
return _noOfCities;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public City[] getCities()
{
// put your code here
City[] copy = new City[_noOfCities];
int i, j;
for(i = 0, j=0;i<_noOfCities;i++,j++)
{
copy = _cities[j];
}
return copy;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public City unifyCities(String city1, String city2)
{
// put your code here
City a = findCity(city1);
City b = findCity(city2);
long noOfResidents = a.getNumOfResidents() + b.getNumOfResidents();
int noOfNeighborhoods = a.getNoOfNeighborhoods() +b.getNoOfNeighborhoods();
Point centerA = a.getCityCenter();
double xCenterA = centerA.getX();
double yCenterA = centerA.getY();
Point centerB = b.getCityCenter();
double xCenterB = centerB.getX();
double yCenterB = centerB.getY();
double xCenterNew = (xCenterA + xCenterB)/2;
double yCenterNew = (yCenterA + yCenterB)/2;
Point stationA = a.getCentralStation();
double xStationA = stationA.getX();
double ystationA = stationA.getY();
Point stationB = b.getCentralStation();
double xStationB = stationB.getX();
double yStationB = stationB.getY();
double xStationNew;
double yStationNew;
if (stationA.isLeft(stationB))
{
xStationNew = xStationA;
yStationNew = ystationA;
}
else
{
xStationNew = xStationB;
yStationNew = yStationB;
}
City unifyCities = new City("city2-city1", xCenterNew, yCenterNew, xStationNew, yStationNew, noOfResidents, noOfNeighborhoods);
String AcityName = a.getCityName();
int indexA =0;
for(int i = 0;i<_noOfCities;i++)
{
String name = _cities.getCityName();
if(name.equals(AcityName))
{
indexA = i;
}
}
String BcityName = b.getCityName();
int indexB=0;
for(int i = 0;i<_noOfCities;i++)
{
String name = _cities.getCityName();
if(name.equals(BcityName))
{
indexB = i;
}
}
if(a.getNumOfResidents()<b.getNumOfResidents())
{
_cities[indexB] = new City(unifyCities);
for(int i=indexA;i<_noOfCities-1;i++)
{
_cities = _cities[i+1];
}
}
else
{
_cities[indexA] = new City(unifyCities);
for(int i=indexB;i<_noOfCities-1;i++)
{
_cities = _cities[i+1];
}
}
_cities[_noOfCities-1]=null;
return unifyCities;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public String toString()
{
// put your code here
String res = "Cities of "+getCountryName()+":" +"\n";
String x = "Cities of "+getCountryName()+":" +"\n";
for(int i = 0;i<_noOfCities;i++)
{
res = res + _cities.toString()+"\n"+"\n";
}
if(res.equals(x))
{
return "There are no cities in this country." ;
}
else
{
return res;
}
}
}