-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Добавлена троичная система счисления
- Loading branch information
1 parent
efec290
commit 504a494
Showing
7 changed files
with
207 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
## Поддерживаемые системы счисления | ||
|
||
* Двоичная | ||
* Троичная | ||
* Римская | ||
* Восьмеричная | ||
* Десятеричная | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/dev/kalenchukov/numeralsystem/TernarySystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright © 2022 Алексей Каленчуков | ||
* GitHub: https://github.com/kalenchukov | ||
* E-mail: mailto:aleksey.kalenchukov@yandex.ru | ||
*/ | ||
|
||
package dev.kalenchukov.numeralsystem; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.jetbrains.annotations.Unmodifiable; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Класс троичной системы счисления. | ||
*/ | ||
public class TernarySystem extends AbstractSystem | ||
{ | ||
/** | ||
* Цифры. | ||
*/ | ||
@Unmodifiable | ||
@NotNull | ||
public static final List<@NotNull Character> DIGITS = List.of( | ||
'\u0030', '\u0031', '\u0032' | ||
); | ||
|
||
/** | ||
* Конструктор для {@code TernarySystem}. | ||
*/ | ||
public TernarySystem() | ||
{ | ||
super(DIGITS); | ||
} | ||
|
||
/** | ||
* @see Object#equals(Object) | ||
*/ | ||
@Override | ||
public boolean equals(@Nullable final Object obj) | ||
{ | ||
if (obj == null) { | ||
return false; | ||
} | ||
|
||
if (this == obj) { | ||
return true; | ||
} | ||
|
||
if (this.getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
|
||
final TernarySystem numeralSystem = (TernarySystem) obj; | ||
|
||
if (!Objects.equals(TernarySystem.DIGITS, numeralSystem.get())) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @see Object#hashCode() | ||
*/ | ||
@Override | ||
public int hashCode() | ||
{ | ||
return TernarySystem.DIGITS.hashCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/test/java/dev/kalenchukov/numeralsystem/TernarySystemTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright © 2022 Алексей Каленчуков | ||
* GitHub: https://github.com/kalenchukov | ||
* E-mail: mailto:aleksey.kalenchukov@yandex.ru | ||
*/ | ||
|
||
package dev.kalenchukov.numeralsystem; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class TernarySystemTest | ||
{ | ||
@NotNull | ||
public static final Numerable NUMERAL_SYSTEM = new TernarySystem(); | ||
|
||
/** | ||
* Проверка цифр. | ||
*/ | ||
@Test | ||
public void testGet() | ||
{ | ||
List<Character> digits = List.of( | ||
'0', '1', '2' | ||
); | ||
|
||
assertArrayEquals(digits.toArray(), NUMERAL_SYSTEM.get().toArray()); | ||
} | ||
|
||
/** | ||
* Проверка с содержащейся цифрой. | ||
*/ | ||
@Test | ||
public void testContains() | ||
{ | ||
assertTrue(NUMERAL_SYSTEM.contains('2')); | ||
} | ||
|
||
/** | ||
* Проверка с не содержащейся цифрой. | ||
*/ | ||
@Test | ||
public void testContainsNot() | ||
{ | ||
assertFalse(NUMERAL_SYSTEM.contains('3')); | ||
} | ||
|
||
/** | ||
* Проверка получения массива цифр. | ||
*/ | ||
@Test | ||
public void testToArray() | ||
{ | ||
Character[] digits = new Character[] { | ||
'0', '1', '2' | ||
}; | ||
|
||
assertArrayEquals(digits, NUMERAL_SYSTEM.toArray()); | ||
} | ||
|
||
/** | ||
* Проверка преобразования цифр в строку с разделителем. | ||
*/ | ||
@Test | ||
public void testToStringSeparator() | ||
{ | ||
String string = "0,1,2"; | ||
|
||
assertEquals(string, NUMERAL_SYSTEM.toString(",")); | ||
} | ||
|
||
/** | ||
* Проверка преобразования цифр в строку. | ||
*/ | ||
@Test | ||
public void testToString() | ||
{ | ||
String string = "012"; | ||
|
||
assertEquals(string, NUMERAL_SYSTEM.toString()); | ||
} | ||
|
||
/** | ||
* Проверка сравнения объектов. | ||
*/ | ||
@Test | ||
public void testEquals() | ||
{ | ||
assertFalse(NUMERAL_SYSTEM.equals(null)); | ||
|
||
assertTrue(NUMERAL_SYSTEM.equals(NUMERAL_SYSTEM)); | ||
|
||
assertFalse(NUMERAL_SYSTEM.equals(new BinarySystem())); | ||
|
||
assertTrue(NUMERAL_SYSTEM.equals(new TernarySystem())); | ||
} | ||
|
||
/** | ||
* Проверка хэш-кода. | ||
*/ | ||
@Test | ||
public void testHashCode() | ||
{ | ||
assertEquals(NUMERAL_SYSTEM.hashCode(), NUMERAL_SYSTEM.hashCode()); | ||
|
||
assertEquals(NUMERAL_SYSTEM.hashCode(), new TernarySystem().hashCode()); | ||
|
||
assertNotEquals(NUMERAL_SYSTEM.hashCode(), new BinarySystem().hashCode()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters