Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Exe class (the one I run)
- import java.util.Scanner;
- public class Exe {
- public static void main(String args[]) {
- PetList List = new PetList();
- Pet creature = new Pet();
- Scanner userInput = new Scanner(System.in);
- while(userInput.hasNextLine()) {
- String command = userInput.next();
- if(command.equalsIgnoreCase("add")) {
- creature.setLat(userInput.nextFloat());
- creature.setLong(userInput.nextFloat());
- creature.setName(userInput.next());
- List.insertFront(creature);
- String junk = userInput.nextLine();
- }
- if(command.equalsIgnoreCase("print")) {
- List.printAll();
- String junkTwo = userInput.nextLine();
- }
- }
- }
- }
- // Pet class
- public class Pet {
- private String name;
- private int treats;
- private Coordinate coor;
- public Pet() {
- name = "";
- treats = 0;
- coor = new Coordinate();
- }
- public Pet(Pet copy) {
- if(copy == null) {
- name = "";
- treats = 0;
- coor = new Coordinate();
- return;
- }
- newPet(copy);
- }
- public void newPet(Pet copyTwo) {
- setName(copyTwo.name);
- setTreats(copyTwo.treats);
- setLat(copyTwo.getLat());
- setLong(copyTwo.getLong());
- }
- public void setName(String newName) {
- name = newName;
- }
- public void setTreats(int newTreats) {
- treats = newTreats;
- }
- public void setLat(float newLat) {
- coor.setLatitude(newLat);
- }
- public void setLong(float newLong) {
- coor.setLongitude(newLong);
- }
- public float getLat() {
- return coor.getLatitude();
- }
- public float getLong() {
- return coor.getLongitude();
- }
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append(name);
- sb.append("{");
- sb.append(treats);
- sb.append("} ");
- sb.append(coor.toString());
- sb.append(" ");
- return sb.toString();
- }
- }
- // PetList class
- public class PetList {
- protected class LinkNode {
- protected LinkNode next;
- protected LinkNode prev;
- protected Pet animal;
- public LinkNode() {
- next = prev = null;
- }
- }
- protected LinkNode first;
- protected LinkNode last;
- public PetList() {
- first = last = null;
- }
- public boolean isEmpty() {
- return first == null;
- }
- public void insertFront(Pet PetTwo) {
- LinkNode temp = new LinkNode();
- temp.animal = new Pet(PetTwo);
- if (first == null) {
- first = last = temp;
- return;
- }
- first.prev = temp;
- temp.next = first;
- first = temp;
- }
- public void printAll() {
- LinkNode temp = first;
- while(temp != null) {
- System.out.print(temp.animal.toString());
- System.out.println();
- temp = temp.next;
- }
- }
- }
- //Coordinate class
- public class Coordinate{
- public Coordinate(){ this(0.0f, 0.0f); }
- public Coordinate(float lat, float lon){
- setLatitude(lat);
- setLongitude(lon);
- }
- public Coordinate(Coordinate source){
- setLatitude(source._lat);
- setLongitude(source._lon);
- }
- public void setLatitude(float lat){
- _lat = (lat>=-90.0f && lat<=90.0f)?lat:0.0f;
- }
- public void setLongitude(float lon){
- _lon = (lon>=-180.0f && lon<=180.0f)?lon:0.0f;
- }
- public float longitude(){ return getLongitude(); }
- public float latitude(){ return getLatitude(); }
- public float getLongitude(){ return _lon; }
- public float getLatitude(){ return _lat; }
- // calculate distance using approx Haversine formula
- // https://en.wikipedia.org/wiki/Haversine_formula
- // and
- // http://www.movable-type.co.uk/scripts/gis-faq-5.1.html
- // distance is returned in meters
- public float distanceTo(Coordinate target){
- final float PI = 3.14159265f;
- final float RADIUS = 6373000.0f; // ~Earth radius (meters)
- float distance = 0.0f;
- // convert degrees to radians
- float lon1 = _lon *PI/180.0f;
- float lat1 = _lat *PI/180.0f;
- float lon2 = target._lon *PI/180.0f;
- float lat2 = target._lat *PI/180.0f;
- float dlon = lon2 - lon1;
- float dlat = lat2 - lat1;
- float a = (float) Math.pow(Math.sin(dlat/2.0f),2.0f)+
- (float) (Math.cos(lat1)*Math.cos(lat2)*Math.pow(Math.sin(dlon/2.0f),2.0f));
- float c = 2.0f * (float) Math.asin( Math.min( 1.0f, Math.sqrt(a)));
- distance = c * RADIUS;
- return distance;
- }
- public String toString(){
- String result="";
- if (_lat>0.0) result +="+";
- result += _lat;
- result += ", ";
- if (_lon>0.0) result +="+";
- result += _lon;
- return result;
- }
- private float _lat;
- private float _lon;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement