1
public class GPSping {

    private double pingLat;
    private double pingLon;
    private int pingTime;

    public GPSping(double Lat, double Lon, int Time)
    {
       pingLat = Lat;
       pingLon = Lon;
       pingTime = Time
    }

    public int timeTo(GPSping anotherPing)

Using this exact method signature above (timeTo) how do I create a secondary GPSping?

My goal is to create a method that calculates the time between two pings.

BinaryGuy
  • 1,246
  • 1
  • 15
  • 29
L0n35
  • 105
  • 3
  • 2
    What do you mean, "create a secondary GPSping"? – azurefrog Sep 15 '19 at 22:16
  • You don't "create a secondary"; inside `timeTo`, `this` will refer to the first "ping" and `anotherPing` will refer to a second "ping". Examine their properties to calculate the time difference. – chrylis -cautiouslyoptimistic- Sep 15 '19 at 22:23
  • (As an aside, in Java it's considered best practice to use `WordCase` for type names and to treat acronyms as words, so the recommended name here would be `GpsPing`.) – chrylis -cautiouslyoptimistic- Sep 15 '19 at 22:26
  • Please excuse my lack of knowledge but I've made the changes but what am I supposed to input when asked for GPSping1.timeTo(GPSping anotherPing)? It only comes up with one field for entry not three like when setting up the first ping. – L0n35 Sep 15 '19 at 22:39
  • Regarding naming: parameters, like variable names should go camelCase, so "double lat, double lon, ..." and you be careful about abbreviations, too. – GhostCat Sep 16 '19 at 04:31

4 Answers4

2

You would simply call the getPingTime method which returns pingTime of the other object. Which means you need a getPingTime method in your GPSping object.

public int getPingTime(){
 return pingTime;
}

Then your timeTo method can look like such

public int timeTo(GPSping anotherPing){
 return getPingTime()-anotherPing.getPingTime();
}
Irelia
  • 3,407
  • 2
  • 10
  • 31
  • A `getPing` would suggest a property `ping` on _some other type_ that isn't a `GPSping`. The appropriate accessor here would be `getPingTime()`. Also, it's not best to use `abs` inside the method like this since you might care about the ordering; let the caller use `abs` if desired. – chrylis -cautiouslyoptimistic- Sep 15 '19 at 22:25
  • Okay. I'll make those edits. The reason I used Math.abs because I used a negative difference would be irrelevant. – Irelia Sep 15 '19 at 22:51
  • On the contrary, the negativeness tells you which of the two objects came first, rather than just how far apart they are – Rogue Sep 16 '19 at 04:30
1

Assuming that you have getters/setters for each variable should can do this

public int timeTo(GPSping anotherPing) {
   return anotherPing.getPingTime() - this.pingTime; // If you want just 
   // magnitude use Math.abs()
}

Your getter should be like this

public int getPingTime() {
 return pingTime;
}

More about getters/setters

Yoshikage Kira
  • 1,070
  • 1
  • 12
  • 20
  • Please excuse my lack of knowledge but I've made the changes but what am I supposed to input when asked for GPSping1.timeTo(GPSping anotherPing)? It only comes up with one field for entry not three like when setting up the first ping. – L0n35 Sep 15 '19 at 22:40
  • @LachieJ Lets say you have two ping objects `gpSping1` and `gpSping2`. To get the difference you would do `gpSping1.timeTo(gpSping2)` You can save the result in a variable or just print it. – Yoshikage Kira Sep 15 '19 at 22:52
0

You can use lombok here

Actually , instead of manually creating , you can use annotation (@Getter,@Setter,@AllArgsConstructor)

Using these annotations remove the need to manually implements the mutator and accessor methods. Although most IDE allows you the generate these methods, using Lombok makes your classes look cleaner, especially when you have a long list of fields

@Getter
@Setter
@AllArgsConstructor
public class GPSping {

    private double pingLat;
    private double pingLon;
    private int pingTime;

public int timeTo(GPSping newPing){
 return getPingTime()-newPing.getPingTime();
}

}
Dulaj Kulathunga
  • 1,248
  • 2
  • 9
  • 19
0

Actually you do not need additional setters or getters. This is one class and you're able to access it's properties directly:

public class GPSping {

    private final int pingTime;
    private final double pingLat;
    private final double pingLon;

    public GPSping(int pingTime, double pingLat, double pingLon) {
        this.pingTime = pingTime;
        this.pingLat = pingLat;
        this.pingLon = pingLon;
    }

    public int timeTo(GPSping anotherPing) {
        return Math.abs(pingTime - anotherPing.pingTime);
    }
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35