Here is my Java code:
import java.util.HashMap;
import java.util.Map;
public class Polynomial<K> {
Map<Integer, Object> polynomial;
public Polynomial(){
polynomial = new HashMap<K, V>();
}
public Polynomial(int numberOfMembers){
polynomial = new HashMap<K, V>(numberOfMembers);
}
public void addElm(int power, int coefficient){
if (power < 0) {
power = Math.abs(power);
throw new RuntimeException("ERROR: The power must be an absolute number, converting to absolute");
}
for (Map.Entry m : polynomial.entrySet()) {
if ((Integer) m.getKey() == power){
polynomial.put(power,m.getValue());
}
}
}
}
On this two rows:
polynomial = new HashMap<K, V>();
and this:
polynomial = new HashMap<K, V>(numberOfMembers);
I get this error:
HashMap<K,V> cannot be resolved to a type
Any idea what cause to the error above and how to fix it?
Kwhere did you getV?power = Math.abs(power);is a no-op since that value is promptly ignored. Likely the runtime optimizer will remove the line altogether, heck, the compiler might even do that, so you might as well, too. There's no point in keeping code that does nothing.