update lab5

This commit is contained in:
Oleksii Aleshchenko 2023-06-08 15:19:36 +03:00
parent 1412505d33
commit 5ca1856e6b
3 changed files with 40 additions and 1 deletions

View File

@ -1,9 +1,24 @@
package lab5; package lab5;
import java.util.Objects;
public class Letter { public class Letter {
private char symbol; private char symbol;
public Letter(char symbol) { public Letter(char symbol) {
this.symbol = symbol; this.symbol = symbol;
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Letter letter = (Letter) o;
return symbol == letter.symbol;
}
@Override
public int hashCode() {
return Objects.hash(symbol);
}
} }

View File

@ -2,8 +2,17 @@ package lab5;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Word word = new Word("Hello");
Word word2 = new Word(new Letter[]{
new Letter('H'),
new Letter('e'),
new Letter('l'),
new Letter('l'),
new Letter('o'),
});
System.out.println(word.equals(word2));
System.out.println(word == word2);
System.out.println("Done!"); System.out.println("Done!");
} }

View File

@ -1,5 +1,7 @@
package lab5; package lab5;
import java.util.Arrays;
public class Word { public class Word {
private Letter[] letters; private Letter[] letters;
@ -15,4 +17,17 @@ public class Word {
letters[i] = new Letter(chars[i]); letters[i] = new Letter(chars[i]);
} }
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Word word = (Word) o;
return Arrays.equals(letters, word.letters);
}
@Override
public int hashCode() {
return Arrays.hashCode(letters);
}
} }