From 8ea9af0937e1599275707daed5daa743d96e4ed3 Mon Sep 17 00:00:00 2001 From: bruch-alex Date: Mon, 15 Jul 2024 13:07:02 +0200 Subject: [PATCH] update README and add some tests --- README.md | 9 ++++++++- pom.xml | 12 +++++++++--- src/main/java/Coder.java | 2 +- src/main/java/Decoder.java | 2 +- src/main/java/Helpers.java | 5 ++--- src/test/java/HelpersTest.java | 19 ++++++++++++++++++- 6 files changed, 39 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 201dab7..0d3feee 100644 --- a/README.md +++ b/README.md @@ -13,4 +13,11 @@ Coder - [Coder.java](src%2Fmain%2FCoder.java) Decoder - [Decoder.java](src%2Fmain%2FDecoder.java) -Methods that are used in the encoder and decoder - [Helpers.java](src%2Fmain%2FHelpers.java) \ No newline at end of file +Methods that are used in the encoder and decoder - [Helpers.java](src%2Fmain%2FHelpers.java) + +# How to run it? +1. You must have the JRE (minimum version 8) installed +2. Download latest [release-file.jar](https://github.com/bruch-alex/hamming-code/releases) +3. Open Terminal in directory, where you saved `.jar` file +4. Copy this command `java -cp hamming-code-VERSION-NUMBER.jar Main`, replace VERSION_NUMBER with current version and run it. +5. Just follow instructions on the screen :D diff --git a/pom.xml b/pom.xml index c34dc28..ebf8f84 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.mycompany.app hamming-code - 1.0-SNAPSHOT + 2024071400 11 @@ -41,7 +41,8 @@ - + Main @@ -58,6 +59,11 @@ jline 3.26.2 + + junit + junit + RELEASE + compile + - \ No newline at end of file diff --git a/src/main/java/Coder.java b/src/main/java/Coder.java index 55c5b87..f26c3ce 100644 --- a/src/main/java/Coder.java +++ b/src/main/java/Coder.java @@ -16,7 +16,7 @@ public static String start(String message) { * @param symbol symbol to convert * @return binary string with 12 bits */ - private static StringBuilder convertToBinaryWithParityBits(char symbol){ + public static StringBuilder convertToBinaryWithParityBits(char symbol){ StringBuilder binaryString = new StringBuilder(); int val = symbol; for (int i = 0; i < 12 ; i++) diff --git a/src/main/java/Decoder.java b/src/main/java/Decoder.java index 4d4ea8f..df003ff 100644 --- a/src/main/java/Decoder.java +++ b/src/main/java/Decoder.java @@ -36,7 +36,7 @@ private static String fixError(int y, String messageToFix) { StringBuilder sb = new StringBuilder(messageToFix); if (y != 0) { - sb.replace(y-1,y,Helpers.changeBit(Character.digit(sb.charAt(y-1),10))); + sb.replace(y-1,y,String.valueOf(Helpers.changeBit(Character.digit(sb.charAt(y-1),10)))); } return sb.toString(); } diff --git a/src/main/java/Helpers.java b/src/main/java/Helpers.java index e332907..5148dbc 100644 --- a/src/main/java/Helpers.java +++ b/src/main/java/Helpers.java @@ -9,7 +9,6 @@ public static StringBuilder replaceParityBits(StringBuilder binaryMessageWithEmp } /** - * * @param parityBitPosition - position of parity bit in message (starts with 0) * @return value of parity bit (1 or 0) */ @@ -41,8 +40,8 @@ public static boolean checkPosition(int i){ * @param value binary value to be converted * @return 1, if value is 0 and return 0, if value is 1. */ - public static String changeBit(int value){ - return (String.valueOf((value & 1) == 0 ? 1 : 0)); + public static int changeBit(int value){ + return (value & 1) == 0 ? 1 : 0; } } diff --git a/src/test/java/HelpersTest.java b/src/test/java/HelpersTest.java index 2b55254..26fb340 100644 --- a/src/test/java/HelpersTest.java +++ b/src/test/java/HelpersTest.java @@ -1,5 +1,22 @@ -import static org.junit.Assert.*; +import org.junit.Assert; +import org.junit.Test; public class HelpersTest { + @Test + public void changeBitFrom1to0Test(){ + Assert.assertEquals(1, Helpers.changeBit(0)); + } + + @Test + public void changeBitFrom0to1Test(){ + Assert.assertEquals(0, Helpers.changeBit(1)); + } + + @Test + public void convertToBinaryWithParityBitsTestSymbol(){ + StringBuilder sb = new StringBuilder("000011000001"); + Assert.assertEquals(0, sb.compareTo(Coder.convertToBinaryWithParityBits('a'))); + } + } \ No newline at end of file