general conversion error

edited February 2014 in Using Processing

Hi, I am writing this simple program. I am getting conversion errors.. Tried casting; no luck! I ll post part of the code, if you want I ll post it in full! I have marked in the code where exactly I am getting the error

class WordFreq {
  //each word is a pair:  the word and its frequency
  ArrayList<Word> wordfrequency;
  String [] stopWords = loadStrings("stopwords-500.txt");


  WordFreq(String[] tokens) {
    wordfrequency = new ArrayList();

    //computing the word frequency
    for (String t : tokens) {
      if (!_isStopWord(t)) {
        int index = _search(t, wordfrequency);
        if (index >= 0) {
          (wordfrequency.get(index)).incr();
        }
        else { 
          wordfrequency.add(new Word(t));
        }
      }
    }
    //  _sort(wordfrequency);
  }

  void arange(int N) {
    for (int i =0; i < N; i++) {
      WordTile tile = wordfrequency.get(i);   <---- this is where I am getting the error: cannot convert (class) Word to (class)wordTile

      tile.setFontSize();
      tile.setSize();
      tile.setXY(random(width), random(height));
    }
  }

  void display(int n) {
    for (int i =0; i < n; i++) {
      WordTile tile = wordfrequency.get(i);     <---- this is where I am getting the error
      tile.display();
    }
  }



class WordTile extends Word {

  PVector location;
  float tileW, tileH; //top left corner of tile
  color tileColor;
  float tileFS=24; //DEFAULT IS 24

  WordTile(String newWord) {
    super(newWord);
    setSize();
    location = new PVector(0, 0);
    tileColor = color(0);
  }

  void setXY(float x, float y) {
     location.x = x;
      location.y = y;

  }

  void setFontSize() {
    tileFS = map(freq, 1, 30, 10, 120);
    setSize();
  }

  void setSize() {
    textSize(tileFS);
    tileW = textWidth(word);
    tileH = textAscent();
  }

  void display() {
    fill(tileColor);
    textSize(tileFS);
    text(word, location.x, location.y);
  }
}

class Word implements Comparable<Word>{

  String word;
  int freq;

  Word(String newWord) {
    word = newWord;
    freq = 1;
  }

  public int compareTo(Word w) {
   return freq - w.freq; 
  }

  String getWord() {
    return word;
  }

  int getFreq() {
    return freq;
  }

  void incr() {
    freq++;
  }

  String toString() {
    return "<" + word+ "," + freq + ">";
  }
}

Answers

  • edited February 2014 Answer ✓

    Although a WordTile is also considered a Word too; the opposite isn't true! @-)

    • Inheritance is always from parent to child.
    • A child has its own properties plus what it's got from inheritance.
    • A parent just has its own. It doesn't get what is exclusive from a child.

    Seems like you need to instantiate objects from a more complete WordTile rather than a less featured Word 1s! (~~)

  • edited February 2014

    Better: you should not use a IS-A relation (inheritance), a WordTitle is not really a Word. You should use a HAS-A relation instead (composition, often preferred to inheritance), where WordTile has a Word field.

    To be more concrete, drop the "extends Word" part, declare a Word inside the class, and instead of:

    WordTile tile = wordfrequency.get(i);
    

    do:

    WordTile tile = new WordTile(wordfrequency.get(i));
    

    supposing the constructor can get a Word and assign it to the corresponding field.

    Of course, then, you need to store the WordTiles in a list, to be used in the display() method.

Sign In or Register to comment.