In: Computer Science
package homework; // 1 point penalty for not restoring the package before turning this in
// CSC 403 W20 HW6
// Fix the toDo items
/* AINSLEY SOANE */
import java.time.LocalDate;
public class GpsTime {
private double longitude;
private double latitude;
private LocalDate when;
//ToDo 1 implement the required constructor (see other file)
public GpsTime( double longitude, double latitude, LocalDate when) {
//super();
this.longitude = longitude;
this.latitude = latitude;
this.when = when;
}
//ToDo 2 update the hashcode function below
// You can try your answer from hw5, or make up a new one
public int hashCode() {
int hash = 17;
hash = 31* hash + ((Double) longitude).hashCode();
hash = 31* hash + ((Double) latitude).hashCode();
hash = 31* hash + when.hashCode();
return hash;
}
//ToDo 3 use the textbook 'recipe' to implement the equals function for this class
public boolean equals( Object x) {
return false; // fix this
}
public String toString() {
return Double.toString(longitude)+ " "+Double.toString(latitude)+" "+when.toString();
}
}
How do I do toDo 3? I've been trying to do it but can't figure it out.
Hi,
Hope you are doing fine. You have done a solid job with the code. I have finished the TODO 3. It is a method that takes in an other object and checks if the passed object and the calling object are equal or not. It returns true if the objects are equal and false if not. The code has been clearly explained usingg comments that have been highlighted in bold. I have also coded a file test.java that will show give you the output of the equal() method.
Program:
GpsTime.java
// CSC 403 W20 HW6
// Fix the toDo items
/* AINSLEY SOANE */
import java.time.LocalDate;
public class GpsTime {
private double longitude;
private double latitude;
private LocalDate when;
//ToDo 1 implement the required constructor (see other file)
public GpsTime( double longitude, double latitude, LocalDate when)
{
//super();
this.longitude = longitude;
this.latitude = latitude;
this.when = when;
}
//ToDo 2 update the hashcode function below
// You can try your answer from hw5, or make up a new one
public int hashCode() {
int hash = 17;
hash = 31* hash + ((Double)
longitude).hashCode();
hash = 31* hash + ((Double)
latitude).hashCode();
hash = 31* hash + when.hashCode();
return hash;
}
//ToDo 3 use the textbook 'recipe' to implement the
equals function for this class
public boolean equals( Object x) {
// If the object is compared with itself then
return true
if (x == this) {
return true;
}
// Check if x is an instance of GpsTime or not "null
instanceof [type]" also returns false
if (!(x instanceof GpsTime)) {
return false;
}
// typecast x to GpsTime so that we can compare data
members
GpsTime g = (GpsTime) x;
// Compare the data members and return
accordingly
return Double.compare(longitude, g.longitude) == 0 &&
Double.compare(latitude, g.latitude) == 0 &&
when.compareTo(g.when)==0;
}
public String toString() {
return Double.toString(longitude)+ "
"+Double.toString(latitude)+" "+when.toString();
}
}
Program:
Test.java
import java.time.LocalDate;
import java.util.Random;
public class Test {
public static void main(String[] args) {
//creating a LocalDate
object gtime using LocalDate.now() method
LocalDate gtime =
LocalDate.now();
//creating three objects
g1, g2 and g3
//g1 and g2 are initialized with
same parameters, g3 is initialized with different parameters
(except time)
GpsTime g1=new GpsTime(23.2,
224.7,gtime);
GpsTime g2=new GpsTime(23.2,
224.7,gtime);
GpsTime g3=new GpsTime(345.2344,
98.7,gtime);
//Printing values of g1,
g2, g3
System.out.println("g1:
"+g1.toString());
System.out.println("g2:
"+g2.toString());
System.out.println("g3:
"+g3.toString());
System.out.println();
//Calling .equal() method
and printing if g1 and g2 are equal and also if g1 and g3 are
equal.
System.out.println("Is g1 equal to
g2? : "+g1.equals(g2));
System.out.println("Is g1 equal to
g3? : "+g1.equals(g3));
}
}
Executable code snippets:
GpsTime.java
Test.java
Output: