This is a two part assignment, i\'ve completed part 1 but part 2 is kicking my b
ID: 3823567 • Letter: T
Question
This is a two part assignment, i've completed part 1 but part 2 is kicking my butt because i missed that class. I will provide the part 1 code. Please leave comments so i can maybe follow along and understand what is going on in this part.
public class KeyValuePair<K,V> {
private K key;
private V value;
/**
* @param key
* @param value
*/
public KeyValuePair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key == null) ? 0 : key.hashCode());
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
KeyValuePair other = (KeyValuePair) obj;
if (key == null) {
if (other.key != null)
return false;
} else if (!key.equals(other.key))
return false;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
@Override
public String toString() {
return "("+ key +"," + value +")";
}
}
public class PairApp {
public static void main(String[] args) {
KeyValuePair<String, Integer> p1 = new KeyValuePair<String, Integer>("SLC", 189899);
KeyValuePair<String, Integer> p2 = new KeyValuePair<String, Integer>("NY", 8244910);
System.out.println("p1:" + p1);
System.out.println("p2"+ p2);
System.out.println("p1.equals p(2): " + p1.equals(p2));
p1 = p2;
System.out.println();
System.out.println("p1: " + p1);
System.out.println("p2: " + p2);
System.out.println("p1.equals(p2): "+p1.equals(p2));
}
}
Explanation / Answer
/usr/lib/jvm/java-8-oracle/bin/java -Didea.launcher.port=7534 -Didea.launcher.bin.path=/home/raghunathk/Desktop/softwares/idea-IC-163.12024.16/bin -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-8-oracle/jre/lib/charsets.jar:/usr/lib/jvm/java-8-oracle/jre/lib/deploy.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jaccess.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jfxrt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-8-oracle/jre/lib/javaws.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jce.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfr.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfxswt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jsse.jar:/usr/lib/jvm/java-8-oracle/jre/lib/management-agent.jar:/usr/lib/jvm/java-8-oracle/jre/lib/plugin.jar:/usr/lib/jvm/java-8-oracle/jre/lib/resources.jar:/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar:/home/raghunathk/Documents/Solve/out/production/Solve:/home/raghunathk/Desktop/softwares/idea-IC-163.12024.16/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain com.pairapp.PairApp
Original List
(SLC,189899)
(NY,8244910)
(LA,3819702)
(SF,812826)
Sorted List
(LA,3819702)
(NY,8244910)
(SF,812826)
(SLC,189899)
p1:(SLC,189899)
p2(NY,8244910)
p1.equals p(2): false
p1: (NY,8244910)
p2: (NY,8244910)
p1.equals(p2): true
Process finished with exit code 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.