Advertisement
paster117

Untitled

Apr 20th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. // Exe class (the one I run)
  2. import java.util.Scanner;
  3. public class Exe {
  4.  
  5. public static void main(String args[]) {
  6. PetList List = new PetList();
  7. Pet creature = new Pet();
  8. Scanner userInput = new Scanner(System.in);
  9. while(userInput.hasNextLine()) {
  10. String command = userInput.next();
  11. if(command.equalsIgnoreCase("add")) {
  12. creature.setLat(userInput.nextFloat());
  13. creature.setLong(userInput.nextFloat());
  14. creature.setName(userInput.next());
  15. List.insertFront(creature);
  16. String junk = userInput.nextLine();
  17. }
  18. if(command.equalsIgnoreCase("print")) {
  19. List.printAll();
  20. String junkTwo = userInput.nextLine();
  21. }
  22. }
  23. }
  24. }
  25.  
  26. // Pet class
  27. public class Pet {
  28. private String name;
  29. private int treats;
  30. private Coordinate coor;
  31. public Pet() {
  32. name = "";
  33. treats = 0;
  34. coor = new Coordinate();
  35. }
  36.  
  37. public Pet(Pet copy) {
  38. if(copy == null) {
  39. name = "";
  40. treats = 0;
  41. coor = new Coordinate();
  42. return;
  43. }
  44. newPet(copy);
  45. }
  46.  
  47. public void newPet(Pet copyTwo) {
  48. setName(copyTwo.name);
  49. setTreats(copyTwo.treats);
  50. setLat(copyTwo.getLat());
  51. setLong(copyTwo.getLong());
  52. }
  53.  
  54. public void setName(String newName) {
  55. name = newName;
  56. }
  57.  
  58. public void setTreats(int newTreats) {
  59. treats = newTreats;
  60. }
  61.  
  62. public void setLat(float newLat) {
  63. coor.setLatitude(newLat);
  64. }
  65.  
  66. public void setLong(float newLong) {
  67. coor.setLongitude(newLong);
  68. }
  69.  
  70. public float getLat() {
  71. return coor.getLatitude();
  72. }
  73.  
  74. public float getLong() {
  75. return coor.getLongitude();
  76. }
  77. public String toString() {
  78. StringBuilder sb = new StringBuilder();
  79. sb.append(name);
  80. sb.append("{");
  81. sb.append(treats);
  82. sb.append("} ");
  83. sb.append(coor.toString());
  84. sb.append(" ");
  85. return sb.toString();
  86. }
  87. }
  88.  
  89. // PetList class
  90. public class PetList {
  91. protected class LinkNode {
  92. protected LinkNode next;
  93. protected LinkNode prev;
  94. protected Pet animal;
  95.  
  96. public LinkNode() {
  97. next = prev = null;
  98. }
  99. }
  100. protected LinkNode first;
  101. protected LinkNode last;
  102.  
  103. public PetList() {
  104. first = last = null;
  105. }
  106.  
  107. public boolean isEmpty() {
  108. return first == null;
  109. }
  110. public void insertFront(Pet PetTwo) {
  111. LinkNode temp = new LinkNode();
  112. temp.animal = new Pet(PetTwo);
  113. if (first == null) {
  114. first = last = temp;
  115. return;
  116. }
  117. first.prev = temp;
  118. temp.next = first;
  119. first = temp;
  120. }
  121.  
  122. public void printAll() {
  123. LinkNode temp = first;
  124. while(temp != null) {
  125. System.out.print(temp.animal.toString());
  126. System.out.println();
  127. temp = temp.next;
  128. }
  129. }
  130.  
  131. }
  132.  
  133. //Coordinate class
  134. public class Coordinate{
  135. public Coordinate(){ this(0.0f, 0.0f); }
  136. public Coordinate(float lat, float lon){
  137. setLatitude(lat);
  138. setLongitude(lon);
  139. }
  140. public Coordinate(Coordinate source){
  141. setLatitude(source._lat);
  142. setLongitude(source._lon);
  143. }
  144. public void setLatitude(float lat){
  145. _lat = (lat>=-90.0f && lat<=90.0f)?lat:0.0f;
  146. }
  147. public void setLongitude(float lon){
  148. _lon = (lon>=-180.0f && lon<=180.0f)?lon:0.0f;
  149. }
  150. public float longitude(){ return getLongitude(); }
  151. public float latitude(){ return getLatitude(); }
  152. public float getLongitude(){ return _lon; }
  153. public float getLatitude(){ return _lat; }
  154. // calculate distance using approx Haversine formula
  155. // https://en.wikipedia.org/wiki/Haversine_formula
  156. // and
  157. // http://www.movable-type.co.uk/scripts/gis-faq-5.1.html
  158. // distance is returned in meters
  159. public float distanceTo(Coordinate target){
  160. final float PI = 3.14159265f;
  161. final float RADIUS = 6373000.0f; // ~Earth radius (meters)
  162. float distance = 0.0f;
  163. // convert degrees to radians
  164. float lon1 = _lon *PI/180.0f;
  165. float lat1 = _lat *PI/180.0f;
  166. float lon2 = target._lon *PI/180.0f;
  167. float lat2 = target._lat *PI/180.0f;
  168.  
  169. float dlon = lon2 - lon1;
  170. float dlat = lat2 - lat1;
  171. float a = (float) Math.pow(Math.sin(dlat/2.0f),2.0f)+
  172. (float) (Math.cos(lat1)*Math.cos(lat2)*Math.pow(Math.sin(dlon/2.0f),2.0f));
  173. float c = 2.0f * (float) Math.asin( Math.min( 1.0f, Math.sqrt(a)));
  174. distance = c * RADIUS;
  175. return distance;
  176. }
  177.  
  178. public String toString(){
  179. String result="";
  180. if (_lat>0.0) result +="+";
  181. result += _lat;
  182. result += ", ";
  183. if (_lon>0.0) result +="+";
  184. result += _lon;
  185. return result;
  186. }
  187.  
  188. private float _lat;
  189. private float _lon;
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement