fork(2) download
  1. public class WordGram {
  2. private String[] myWords;
  3. private int myHash;
  4.  
  5. public WordGram(String[] source, int start, int size) {
  6. myWords = new String[size];
  7. System.arraycopy(source, start, myWords, 0, size);
  8. myHash = hashCode();
  9. }
  10.  
  11. public String wordAt(int index) {
  12. if (index < 0 || index >= myWords.length) {
  13. throw new IndexOutOfBoundsException("bad index in wordAt "+index);
  14. }
  15. return myWords[index];
  16. }
  17.  
  18. public int length(){
  19. // TODO: Complete this method
  20. return myWords.length;
  21. }
  22.  
  23. public String toString(){
  24. String ret = "";
  25. for (int k =0; k<myWords.length; k++) {
  26. ret += myWords[k] + " " ;
  27. }// TODO: Complete this method
  28.  
  29. return ret.trim();
  30. }
  31.  
  32. public boolean equals(Object o) {
  33. WordGram other = (WordGram) o;
  34. if (length() != other.length()){
  35. return false;
  36. }
  37. for (int k =0; k < myWords.length; k++) {
  38. if(!myWords[k].equals(other.wordAt(k))){
  39. return false;
  40. }
  41. }// TODO: Complete this method
  42. return true;
  43.  
  44. }
  45.  
  46. public WordGram shiftAdd(String word) {
  47. WordGram out = new WordGram(myWords, 0, myWords.length);
  48. for (int k =0; k < myWords.length-1; k++) {
  49. myWords[k]= myWords[k+1];
  50. }
  51. myWords[myWords.length - 1] = word;
  52. // shift all words one towards 0 and add word at the end.
  53. // you lose the first word
  54. // TODO: Complete this method
  55. return out;
  56. }
  57. public int hashCode() {
  58.  
  59. String s = "";
  60. for (int k = 0; k < myWords.length; k++){
  61. s += myWords[k];
  62. }
  63. //System.out.println ("s: " + s);
  64. return s.hashCode();
  65. }
  66.  
  67. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:1: error: class WordGram is public, should be declared in a file named WordGram.java
public class WordGram {
       ^
1 error
stdout
Standard output is empty