This repository acts as a personal archive for my solutions to EdX course *Data Structures and Software Design* from PennX.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.0 KiB

  1. public class Word implements Comparable<Word> {
  2. protected String text;
  3. protected int count;
  4. protected int total;
  5. /*
  6. * Note that this does not set the value or increment the counter.
  7. * It just creates an object with no count and no total so far.
  8. */
  9. public Word(String text) {
  10. this.text = text;
  11. count = 0;
  12. total = 0;
  13. }
  14. public void increaseTotal(int value) {
  15. total += value;
  16. count++;
  17. }
  18. public double calculateScore() {
  19. if (count == 0) {
  20. return 0;
  21. }
  22. return ((double) total) / count;
  23. }
  24. public int getCount() {
  25. return count;
  26. }
  27. public int getTotal() {
  28. return total;
  29. }
  30. public String getText() {
  31. return text;
  32. }
  33. @Override
  34. public boolean equals(Object other) {
  35. if (other instanceof Word == false) return false;
  36. Word otherWord = (Word)other;
  37. return text.equals(otherWord.text);
  38. }
  39. @Override
  40. public int hashCode() {
  41. return text.hashCode();
  42. }
  43. @Override
  44. public int compareTo(Word o) {
  45. return text.compareTo(o.text);
  46. }
  47. }