Question

In: Computer Science

package homework; // 1 point penalty for not restoring the package before turning this in //...

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.

Solutions

Expert Solution

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:


Related Solutions

HOMEWORK 1 This assignment is designed to illustrate how a software package such as Microsoft Excel...
HOMEWORK 1 This assignment is designed to illustrate how a software package such as Microsoft Excel supplemented by an add-in such as PHStat can enable one to calculate minimum sample sizes necessary in order to construct confidence intervals for both population means and proportions and to construct these types of confidence intervals. You should use PHStat in order to accomplish all parts of this assignment. You should not only find the required information, but you should explain the meanings of...
HOMEWORK 1 This assignment is designed to illustrate how a software package such as Microsoft Excel...
HOMEWORK 1 This assignment is designed to illustrate how a software package such as Microsoft Excel supplemented by an add-in such as PHStat can enable one to calculate minimum sample sizes necessary in order to construct confidence intervals for both population means and proportions and to construct these types of confidence intervals. You should use PHStat in order to accomplish all parts of this assignment. You should not only find the required information, but you should explain the meanings of...
1. "The Black Death serves as a major turning point in the history of European civilization."...
1. "The Black Death serves as a major turning point in the history of European civilization." Discuss this statement in the context of late-medieval economics, social relations, popular religion, and art. (in own words)
1)    75% of adult smokers started smoking before turning 18 years in a population. A random...
1)    75% of adult smokers started smoking before turning 18 years in a population. A random sample of 30 smokers 18 years or older are selected and the number of smokers who started smoking before 18 is recorded. 1)   Find the probability that exactly 7 are smokers. 2)   Find the probability that at least 5 are smokers. 3)   Find the probability that fewer than 3 are smokers. 4)   Find the probability that between 4 and 7 of them, inclusive, are smokers. 5)   Find the mean...
1. Explain in full why this chapter marks a turning point for your major character Candide....
1. Explain in full why this chapter marks a turning point for your major character Candide. 2. What is Martin’s assessment of France? List all in their experience there that shows its corruption.
(1) According to the American Lung Association, 90% of adult smokers started smoking before turning 21...
(1) According to the American Lung Association, 90% of adult smokers started smoking before turning 21 years old. Ten smokers 21 years old or older were randomly selected, and the number of smokers who started smoking before 21 is recorded. (a) State the distribution of the random variable of number of smoker of these 10 who started smoking before age 21 and its two parameters. (b) Find the probability that exactly 8 of them started smoking before 21 years of...
What event was the turning point in occupational health and safety that resulted in establishing fundamental...
What event was the turning point in occupational health and safety that resulted in establishing fundamental rights for workers and eventually the Occupational Health & Safety Act of 1979 as we know it today? Question 1 options: A) Passing of Bill C-45 (Amendment of Criminal Code of Canada) B) Passing of Bill C-70 C) Passing of Bill 160 (Resulting from the Tony Dean Report) D) The Ham Commission Report
How did the United States get entangled in the Vietnam war? Was there a turning point...
How did the United States get entangled in the Vietnam war? Was there a turning point in the war that changed public opinion?
1 point) A package delivery service wants to compare the proportion of on-time deliveries for two...
1 point) A package delivery service wants to compare the proportion of on-time deliveries for two of its major service areas. In City A, 366 out of a random sample of 431 deliveries were on time. A random sample of 273 deliveries in City B showed that 216 were on time. 1. Calculate the difference in the sample proportion for the delivery times in the two cities. ?̂ ?????−?̂ ?????p^CityA−p^CityB = 2. What are the correct hypotheses for conducting a...
7.3 (1 point) A package delivery service wants to compare the proportion of on-time deliveries for...
7.3 (1 point) A package delivery service wants to compare the proportion of on-time deliveries for two of its major service areas. In City A, 343 out of a random sample of 403 deliveries were on time. A random sample of 264 deliveries in City B showed that 209 were on time. 1. Calculate the difference in the sample proportion for the delivery times in the two cities. ?̂ ?????−?̂ ?????p^CityA−p^CityB = 2. What are the correct hypotheses for conducting...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT