From 5ca1856e6b3d6e7718e239b036df0da57da763e3 Mon Sep 17 00:00:00 2001 From: Oleksii Aleshchenko Date: Thu, 8 Jun 2023 15:19:36 +0300 Subject: [PATCH] update lab5 --- src/lab5/Letter.java | 15 +++++++++++++++ src/lab5/Main.java | 11 ++++++++++- src/lab5/Word.java | 15 +++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/lab5/Letter.java b/src/lab5/Letter.java index f4c7a9f..0d63952 100644 --- a/src/lab5/Letter.java +++ b/src/lab5/Letter.java @@ -1,9 +1,24 @@ package lab5; +import java.util.Objects; + public class Letter { private char symbol; public Letter(char 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); + } } diff --git a/src/lab5/Main.java b/src/lab5/Main.java index 54bb4cc..27181ab 100644 --- a/src/lab5/Main.java +++ b/src/lab5/Main.java @@ -2,8 +2,17 @@ package lab5; public class Main { 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!"); } diff --git a/src/lab5/Word.java b/src/lab5/Word.java index 504f806..f54864d 100644 --- a/src/lab5/Word.java +++ b/src/lab5/Word.java @@ -1,5 +1,7 @@ package lab5; +import java.util.Arrays; + public class Word { private Letter[] letters; @@ -15,4 +17,17 @@ public class Word { 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); + } }