Quindi dici che per non dare errori, la classe dovrebbe implementare Comparable?? (nelle soluzioni non lo fa)..
cmq il codice e'..
public class CodificatoreTester
{ public static void main(String[] args)
{
}
}
class Codificatore implements InvertibleDictionary
{ private StringPair [] v;
private int vSize;
public Codificatore()
{ v = new StringPair[10];
vSize = 0;
}
public String toString()
{ for(int i = 0; i < vSize; i++)
System.out.println(v[i]);
}
public boolean isEmpty()
{ return vSize == 0;
}
public void makeEmpty()
{ vSize = 0;
}
public void insert(Comparable key, Comparable value)
{ if(key == null)
throw new DictionaryItemNotFoundException();
try
{ remove(key);
}
catch (DictionaryItemNotFoundException e)
{}
for(int i = 0; i < vSize; i++)
{ if(v[i].word.equals(key))
{ v[i] = new StringPair((String) key, (String ) value);
return;
}
}
v[vSize++] = new StringPair((String) key, (String) value);
}
public void remove(Comparable key)
{ for(int i = 0; i < vSize; i++)
{ if(v[i].word.equals(key))
{ v[i] = v[vSize - 1];
vSize--;
}
}
throw new DictionaryItemNotFoundException();
}
public Object find(Comparable key)
{ for(int i = 0; i < vSize; i++)
{ if(v[i].word.equals(key))
return v[i].code;
}
throw new DictionaryItemNotFoundException();
}
public InvertibleDictionary invert()
{ InvertibleDictionary i = new Codificatore();
for( int j = 0; j < vSize; j++)
{ Comparable c = v[j].code;
Comparable c1 = v[j].word;
i.insert((Comparable) v[j].code, (Comparable) v[j].word );
}
return i;
}
private class StringPair
{ public StringPair(String word, String code)
{ this.word = word;
this.code = code;
}
public String getWord()
{ return word;
}
public String getCode()
{ return code;
}
public String toString()
{ return word + " " + code;
}
private String word;
private String code;
}
}
interface InvertibleDictionary
{ boolean isEmpty();
void makeEmpty();
void insert(Comparable key, Comparable value);
void remove(Comparable key);
Object find(Comparable key);
InvertibleDictionary invert();
}
class DictionaryItemNotFoundException extends RuntimeException
{}
Manca da scrivere il main ma per ora non è importante..
questo programma dovrebbe creare un dizionario con all' interno coppie di stringhe e farci un codificatore..
cmq il problema sta nei metodi insert e invert, dove i cast a string e comparable non funzionano..
Nel metodo insert, il cast a String può essere fatto con .toString(), ma rimane il fatto che il cast a Comparable non funziona..
incovertible types:
found: Comaparable
required: java.lang.String
e viceversa..
incovertible types:
found: java.lang.String
required: Comaparable

Rispondi quotando