The following gives a partial implementation of a class names XyLint in C ++ and
ID: 3583070 • Letter: T
Question
The following gives a partial implementation of a class names XyLint in C ++ and Java. A MyList object is a singly linked list, where the first node is referenced by the variable head, and the last node is referenced by the variable tail // Java class ListNode { public ListNode (E datd, ListNode next){ this.data = data; this.next = next; } public E data; public ListNode next; } class MyList implements List{ private ListNode head, tail; } // C++ template class ListNode { public: E data; ListNode* next; ListNode (Ek item, ListNode * ptr = NULL): data (item), next (ptr) {} }; template class MyList{ // private: LisNode * head, *tail; }; implement the following methods (in C++ or Java) in the class MyList. index Of(elm): This method returns the index of the first occurrence of elm in this list. It returns -1 if elm does not occur in the list. hashCode(): Returns the hash code of this list. Let the size be n, and has Cose (e_i) be the hash code of the ith element (i = 0, 1, ..., n-1). Then the hash code of this list is sigma_i^n (i + 1)x hash Code (e_i). It is assumed that the element type E provides a method called hash Code.Explanation / Answer
package com.java1.algos;
import java.util.HashMap;
public class MyHashcodeImpl {
public static void main(String a[]){
HashMap<Price, String> hm = new HashMap<Price, String>();
hm.put(new Price("Banana", 20), "Banana");
hm.put(new Price("Apple", 40), "Apple");
hm.put(new Price("Orange", 30), "Orange");
Price key = new Price("Banana", 20);
System.out.println("Hashcode of the key: "+key.hashCode());
System.out.println("Value from map: "+hm.get(key));
}
}
class Price{
private String item;
private int price;
public Price(String itm, int pr){
this.item = itm;
this.price = pr;
}
public int hashCode(){
System.out.println("In hashcode");
int hashcode = 0;
hashcode = price*20;
hashcode += item.hashCode();
return hashcode;
}
public boolean equals(Object obj){
System.out.println("In equals");
if (obj instanceof Price) {
Price pp = (Price) obj;
return (pp.item.equals(this.item) && pp.price == this.price);
} else {
return false;
}
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String toString(){
return "item: "+item+" price: "+price;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.