Advertisement
Guest User

mjytrnthdbfgdvc

a guest
Jan 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.43 KB | None | 0 0
  1.  
  2. /**
  3. * Class: Country
  4. *
  5. * @author Nir Geva
  6. * @version 19/1/19
  7. */
  8. public class Country
  9. {
  10.  
  11. private CityNode _head;
  12. private String _name;
  13.  
  14. /**
  15. * Constructor for Country object with input of the country name.
  16. * <br/>Initializes head node to null.
  17. * <br/>
  18. * <br/>Runtime Complexity: O(1)
  19. * <br/>Space Complexity: O(1)
  20. */
  21. public Country(String name)
  22. {
  23. _head = null;
  24. _name = name;
  25. }
  26.  
  27. /**
  28. * Recieves input of a string, returns true if a city with that name exists in the country, false if not.
  29. * <br/>
  30. * <br/>Runtime Complexity: O(n)
  31. * <br/>Space Complexity: O(1)
  32. */
  33. private boolean cityNameExists(String cityName)
  34. {
  35. //Checks whether a city with the same name already exists in this country. If so, returns false.
  36. CityNode i = _head;
  37. while (i != null)
  38. {
  39. if(i.getCity().getCityName().equals(cityName))
  40. {
  41. return true;
  42. }
  43. i = i.getNext();
  44. }
  45. return false;
  46. }
  47.  
  48. /**
  49. * Creates a new CityNode with a new City with input arguments and adds the node to the start of the node list. Returns true after adding the city.
  50. * <br/>If the country already has a city with that name, returns false instead.
  51. * <br/>
  52. * <br/>Runtime Complexity: O(n)
  53. * <br/>Space Complexity: O(1)
  54. */
  55. public boolean addCity(String cityName, double centerX, double centerY, double stationX, double stationY, long population, int hoods)
  56. {
  57. //If country is empty, simply add new city as the head.
  58. if(_head == null)
  59. {
  60. _head = new CityNode(new City (cityName, centerX, centerY, stationX, stationY, population, hoods));
  61. return true;
  62. }
  63. //If country isn't empty, need to check whether this city already exists. If not, add it as a new head with previous head as next node.
  64. else
  65. {
  66. if(cityNameExists(cityName))
  67. return false;
  68. CityNode c = new CityNode(new City (cityName, centerX, centerY, stationX, stationY, population, hoods), _head);
  69. _head = c;
  70. return true;
  71. }
  72. }
  73.  
  74. /**
  75. * Returns sum of all residents in country.
  76. * <br/>Goes through the country nodes and sums the total residents of all of them.
  77. * <br/>
  78. * <br/>Runtime Complexity: O(n)
  79. * <br/>Space Complexity: O(1)
  80. */
  81. public long getNumOfResidents()
  82. {
  83. long sum = 0;
  84. CityNode i = _head;
  85. while(i != null)
  86. {
  87. sum += i.getCity().getNumOfResidents();
  88. i = i.getNext();
  89. }
  90. return sum;
  91. }
  92.  
  93. /**
  94. * Returns longest distance between 2 cities in the country.
  95. * <br/>Calculates distance between every 2 city centers and find largest one to return.
  96. * <br/>
  97. * <br/>Runtime Complexity: O(n^2)
  98. * <br/>Space Complexity: O(1)
  99. */
  100. public double longestDistance()
  101. {
  102. double longest = 0;
  103. double current = 0;
  104. CityNode i = _head;
  105. while(i!=null)
  106. {
  107. CityNode j = i.getNext();
  108. while(j!=null)
  109. {
  110. current = i.getCity().getCityCenter().distance(j.getCity().getCityCenter());
  111. if(current > longest)
  112. longest = current;
  113. j = j.getNext();
  114. }
  115. i = i.getNext();
  116. }
  117. return longest;
  118. }
  119.  
  120. /**
  121. * Recieves a string of a city name as input, returns a string describing all cities in the country with a city center y value larger than the city with recieved name.
  122. * <br/>Will return an error message string instead if there are no cities by that name in the country.
  123. * <br/>
  124. * <br/>Runtime Complexity: O(n)
  125. * <br/>Space Complexity: O(1)
  126. */
  127. public String citiesNorthOf(String cityName)
  128. {
  129. String start = "The cities north of " + cityName + " are:";
  130. String res = start;
  131. Point c = null;
  132. CityNode i = _head;
  133. //Checks whether city exists in the country, and gets its city center point if it does.
  134. while (i != null)
  135. {
  136. if(i.getCity().getCityName().equals(cityName))
  137. {
  138. c = new Point(i.getCity().getCityCenter());
  139. break;
  140. }
  141. i = i.getNext();
  142. }
  143. //If the previous loop reached the end of the country, it means it didn't find a city with that name.
  144. if (i == null)
  145. return "There is no city with the name " + cityName;
  146. i = _head;
  147. while(i!=null)
  148. {
  149. if(i.getCity().getCityCenter().isAbove(c))
  150. {
  151. res = res + "\n\n" + i.getCity().toString();
  152. }
  153. i = i.getNext();
  154. }
  155. //If the final string equals to the initial string, it means nothing was added - no cities was found above cityName.
  156. if(res.equals(start))
  157. return "There are no cities north of " + cityName;
  158. return res;
  159. }
  160.  
  161. /**
  162. * Finds and returns the city with lowest city center Y value in the country.
  163. * <br/>If there are no cities in the country, returns null.
  164. * <br/>
  165. * <br/>Runtime Complexity: O(1)
  166. * <br/>Space Complexity: O(1)
  167. */
  168. public City southernmostCity ()
  169. {
  170. if(_head == null)
  171. return null;
  172. City south = _head.getCity();
  173. CityNode i = _head;
  174. while (i!=null)
  175. {
  176. if(i.getCity().getCityCenter().isUnder(south.getCityCenter()))
  177. south = i.getCity();
  178. i = i.getNext();
  179. }
  180. return south;
  181. }
  182.  
  183. /**
  184. * Returns country name.
  185. * <br/>
  186. * <br/>Runtime Complexity: O(1)
  187. * <br/>Space Complexity: O(1)
  188. */
  189. public String getCountryName ()
  190. {
  191. return _name;
  192. }
  193.  
  194. /**
  195. * Sums and returns number of city nodes in the country.
  196. * <br/>
  197. * <br/>Runtime Complexity: O(1)
  198. * <br/>Space Complexity: O(1)
  199. */
  200. public int getNumOfCities ()
  201. {
  202. int sum = 0;
  203. // if(_head == null)
  204. // return sum;
  205. CityNode i = _head;
  206. while (i != null)
  207. {
  208. sum++;
  209. i = i.getNext();
  210. }
  211. return sum;
  212. }
  213.  
  214. /**
  215. * Makes and returns copy of the country, with copies of all its city nodes with copies of their cities.
  216. * <br/>
  217. * <br/>Runtime Complexity: O(n)
  218. * <br/>Space Complexity: O(n)
  219. */
  220. public Country getCities()
  221. {
  222. if (_head == null)
  223. return null;
  224. CityNode i = new CityNode(_head);
  225. CityNode c = new CityNode(new City(i.getCity()));
  226. CityNode headCopy = c;
  227. Country countryCopy = new Country(_name);
  228. countryCopy.addHead(headCopy);
  229. //If there's only 1 city in the country, returns the copied country with copied head (with no next).
  230. if(i.getNext() == null)
  231. {
  232. return countryCopy;
  233. }
  234. while(i!=null)
  235. {
  236.  
  237. i = i.getNext();
  238. if(i!=null)
  239. {
  240. c.setNext(new CityNode(new City(i.getCity())));
  241. c = c.getNext();
  242. }
  243. }
  244. return countryCopy;
  245. }
  246.  
  247. /**
  248. * Sets the country head to a node from input.
  249. * <br/>
  250. * <br/>Runtime Complexity: O(1)
  251. * <br/>Space Complexity: O(1)
  252. */
  253. private void addHead(CityNode n)
  254. {
  255. _head = n;
  256. }
  257.  
  258. /**
  259. * Unifies 2 cities into 1. Returns unified city.
  260. * <br/>Combines their names, residents, neighborhoods.
  261. * <br/>New city center is in the middle of the 2 cities' centers.
  262. * <br/>New central station is westernmost central station.
  263. * <br/>Takes list place of bigger city, removing the smaller city from the list (if both same size, first city is considered bigger).
  264. * <br/>
  265. * <br/>Runtime Complexity: O(n)
  266. * <br/>Space Complexity: O(1)
  267. */
  268. public City unifyCities(String city1name, String city2name)
  269. {
  270. CityNode i = _head;
  271. CityNode city1node = null;
  272. CityNode city2node = null;
  273. //Finds nodes of cities by the names of city1name and city2name.
  274. while (i!=null)
  275. {
  276. if(i.getCity().getCityName().equals(city1name))
  277. city1node = i;
  278. if(i.getCity().getCityName().equals(city2name))
  279. city2node = i;
  280. i = i.getNext();
  281. }
  282. //Gets their cities from the citynodes.
  283. City city1 = city1node.getCity();
  284. City city2 = city2node.getCity();
  285. //Combines name, residents, neighborhoods.
  286. String newName = city1name + "-" + city2name;
  287. long newResidents = city1.getNumOfResidents() + city2.getNumOfResidents();
  288. int newHoods = city1.getNoOfNeighborhoods() + city2.getNoOfNeighborhoods();
  289. //New city center is in the middle of the 2 city centers.
  290. double newCenterX = (city1.getCityCenter().getX() + city2.getCityCenter().getX())/2;
  291. double newCenterY = (city1.getCityCenter().getY() + city2.getCityCenter().getY())/2;
  292. double newStationX;
  293. double newStationY;
  294. //Finds westernmost central station to become new central station.
  295. if(city1.getCentralStation().isLeft(city2.getCentralStation()))
  296. {
  297. newStationX = city1.getCentralStation().getX();
  298. newStationY = city1.getCentralStation().getY();
  299. }
  300. else
  301. {
  302. newStationX = city2.getCentralStation().getX();
  303. newStationY = city2.getCentralStation().getY();
  304. }
  305.  
  306. City unified = new City(newName, newCenterX, newCenterY, newStationX, newStationY, newResidents, newHoods);
  307. //Determines which city's node will be removed and which will be replaced by the unified city.
  308. if(city2.getNumOfResidents() > city1.getNumOfResidents())
  309. {
  310. remove(city1node);
  311. city2node.setCity(unified);
  312. }
  313. else
  314. {
  315. remove(city2node);
  316. city1node.setCity(unified);
  317. }
  318. return unified;
  319. }
  320.  
  321. /**
  322. * Removes a city from the node list.
  323. * <br/>Finds the node's previous node on the list, and sets its next node to the removed node's next node.
  324. * <br/>
  325. * <br/>Runtime Complexity: O(n)
  326. * <br/>Space Complexity: O(1)
  327. */
  328. private void remove(CityNode c)
  329. {
  330. if (c == _head)
  331. {
  332. _head = c.getNext();
  333. }
  334. CityNode i = _head;
  335. while (i!=null)
  336. {
  337. if(i.getNext() == c)
  338. {
  339. i.setNext(c.getNext());
  340. }
  341. i = i.getNext();
  342. }
  343. }
  344.  
  345. /**
  346. * Returns string with info of each city in the country, if there are none, it will say that there are none.
  347. * <br/>
  348. * <br/>Runtime Complexity: O(n)
  349. * <br/>Space Complexity: O(1)
  350. */
  351. public String toString()
  352. {
  353. if(_head == null)
  354. return "There are no cities in this country.";
  355. String res = "Cities of " + _name + ":";
  356. CityNode i = _head;
  357. while(i!=null)
  358. {
  359. res = res + "\n\n" + i.getCity().toString();
  360. i = i.getNext();
  361. }
  362. return res;
  363. }
  364.  
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement