From f4d94e228963e2c51a4e9cef00708e8e149939ca Mon Sep 17 00:00:00 2001 From: vsilverman Date: Mon, 16 Jan 2017 17:42:28 -0800 Subject: [PATCH 1/2] initial release --- RemoveCharFromString.java | 63 +++++++++++++++++++++++++++++++++ TestRemoveCharFromString.java | 66 +++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 RemoveCharFromString.java create mode 100644 TestRemoveCharFromString.java diff --git a/RemoveCharFromString.java b/RemoveCharFromString.java new file mode 100644 index 0000000..503b447 --- /dev/null +++ b/RemoveCharFromString.java @@ -0,0 +1,63 @@ +/** +Define a Java class to solve this problem: +Given a String and a Character, remove all instances of the Character in the String + +Solve this two ways: +1. Iterate through the String, one character at a time +2. Find a method in the String class that can solve this in one line + +Write methods for each solution. + +Afterwards, write a TestNG or JUnit class for each solution which tests these methods. +Include both positive and negative cases for validations. + */ + +/** + * @author Vlad + * + */ +public class RemoveCharFromString { + + public static String str = "Apple interview"; + public static char ch = 'p'; + + public String RemCharMethod1(String s, char c) { + if (s == null) + return null; + String res = ""; + char[] chstr = s.toCharArray(); + for (char celem : chstr) { + if (celem != c) + res += celem; + } + return res; + } + + public String RemCharMethod2(String s, char c) { + if (s == null) + return null; + String res = ""; + res = s.replace(String.valueOf(c), ""); + return res; + } + + + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + if (args.length < 2) + System.out.println("Using default params"); + else { + str = args[0]; + ch = args[1].charAt(0); + } + System.out.println("Original string = " + str); + RemoveCharFromString rcfs = new RemoveCharFromString(); + System.out.println("Result for method 1 = " + rcfs.RemCharMethod1(str, ch)); + System.out.println("Result for method 2 = " + rcfs.RemCharMethod2(str, ch)); + + } + +} diff --git a/TestRemoveCharFromString.java b/TestRemoveCharFromString.java new file mode 100644 index 0000000..e4b230e --- /dev/null +++ b/TestRemoveCharFromString.java @@ -0,0 +1,66 @@ +/** + * This class is using JUnit4 + */ + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * @author Vlad + * + */ +public class TestRemoveCharFromString { + + public static RemoveCharFromString rcfs; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + rcfs = new RemoveCharFromString(); + } + + @AfterClass + public static void tearDownAfterClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + + @Test + public void test() { + assertEquals("Ale interview", rcfs.RemCharMethod1("Apple interview", 'p')); + assertEquals("Ale interview", rcfs.RemCharMethod2("Apple interview", 'p')); + + assertEquals("Appl intrviw", rcfs.RemCharMethod1("Apple interview", 'e')); + assertEquals("Appl intrviw", rcfs.RemCharMethod2("Apple interview", 'e')); + + assertEquals("Apple interview", rcfs.RemCharMethod1("Apple interview", 'x')); + assertEquals("Apple interview", rcfs.RemCharMethod2("Apple interview", 'x')); + + assertEquals("bc bc", rcfs.RemCharMethod1("abc abc", 'a')); + assertEquals("bc bc", rcfs.RemCharMethod2("abc abc", 'a')); + + assertEquals("ab ab", rcfs.RemCharMethod1("abc abc", 'c')); + assertEquals("ab ab", rcfs.RemCharMethod2("abc abc", 'c')); + + assertEquals("", rcfs.RemCharMethod1("", ' ')); + assertEquals("", rcfs.RemCharMethod2("", ' ')); + + assertEquals("", rcfs.RemCharMethod1(" ", ' ')); + assertEquals("", rcfs.RemCharMethod2(" ", ' ')); + + assertEquals(null, rcfs.RemCharMethod1(null, ' ')); + assertEquals(null, rcfs.RemCharMethod2(null, ' ')); + } + +} From 73c64215cd2c9ae2a3acdede21cceca89eee59dc Mon Sep 17 00:00:00 2001 From: vsilverman Date: Tue, 17 Jan 2017 08:16:36 -0800 Subject: [PATCH 2/2] QA plan is included Included a brief QA plan as part of a README --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index a3c01a6..b2ddf7f 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,13 @@ Solve this two ways:
2. Find a method in the String class that can solve this in one line
Write methods for each solution. +Written Java class should have a main method, allowing to execute it without parameters (default execution) and with parameters. This class should allow accepting as parameters original string and a character to be removed. Afterwards, write a TestNG or JUnit class for each solution which tests these methods.
Include both positive and negative cases for validations. +All tests may be combined in a one or several test cases. +Positive tests should include at least two words as original string. +Negative tests should include at least the case when removable character is not part of original string, string with spaces only, empty string, null string. + +Provide a screenshot showcasing results of executing JUnit tests for both methods.