From 5cb1f6f5992659b76ef1e7960a221b665af667e1 Mon Sep 17 00:00:00 2001 From: DonSylver <141275471+DonSylver@users.noreply.github.com> Date: Sun, 14 Jan 2024 22:11:29 +0000 Subject: [PATCH 1/5] Complete the test class for addFive TRYING again --- .../gettingstarted/GettingStartedTest.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/test/java/tudelft/gettingstarted/GettingStartedTest.java b/src/test/java/tudelft/gettingstarted/GettingStartedTest.java index dca51b750..56fc3779e 100644 --- a/src/test/java/tudelft/gettingstarted/GettingStartedTest.java +++ b/src/test/java/tudelft/gettingstarted/GettingStartedTest.java @@ -13,15 +13,17 @@ public void addFiveTo20() { // UNCOMMENT THE CODE BELOW, AND FILL THE GAPS! -// @Test -// public void addFiveToZero() { -// int result = new GettingStarted().addFive(???); -// Assertions.assertEquals(???, result); -// } -// -// @Test -// public void addFiveToMinus20() { -// int result = new GettingStarted().addFive(???); -// Assertions.assertEquals(????,result); -// } + @Test + // filling this in with 0 and 5 + public void addFiveToZero() { + int result = new GettingStarted().addFive(0); + Assertions.assertEquals(5, result); + } + + @Test + // filling this in with -20 and -15 + public void addFiveToMinus20() { + int result = new GettingStarted().addFive(-20); + Assertions.assertEquals(-15,result); + } } From df2594d324cce04454f028d62d47272945e677cf Mon Sep 17 00:00:00 2001 From: DonSylver <141275471+DonSylver@users.noreply.github.com> Date: Sat, 10 Feb 2024 04:54:43 +0000 Subject: [PATCH 2/5] Complete the test class for addFive --- src/main/java/tudelft/numfinder/NumFinder.java | 2 +- src/test/java/tudelft/gettingstarted/GettingStartedTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/tudelft/numfinder/NumFinder.java b/src/main/java/tudelft/numfinder/NumFinder.java index 4577ef259..3d0cf32c0 100644 --- a/src/main/java/tudelft/numfinder/NumFinder.java +++ b/src/main/java/tudelft/numfinder/NumFinder.java @@ -9,7 +9,7 @@ public void find(int[] nums) { if(n < smallest) smallest = n; - else if (n > largest) + if (n > largest) largest = n; } diff --git a/src/test/java/tudelft/gettingstarted/GettingStartedTest.java b/src/test/java/tudelft/gettingstarted/GettingStartedTest.java index 56fc3779e..aedc919c5 100644 --- a/src/test/java/tudelft/gettingstarted/GettingStartedTest.java +++ b/src/test/java/tudelft/gettingstarted/GettingStartedTest.java @@ -6,7 +6,7 @@ public class GettingStartedTest { @Test - public void addFiveTo20() { + public void addFiveToTwenty() { int result = new GettingStarted().addFive(20); Assertions.assertEquals(25,result); } From c3385182e5059f5e42a8a493045f8a491e15df41 Mon Sep 17 00:00:00 2001 From: DonSylver <141275471+DonSylver@users.noreply.github.com> Date: Mon, 12 Feb 2024 18:44:49 +0000 Subject: [PATCH 3/5] romanNumerals, leapYear, chocolateBars modified --- src/main/java/tudelft/numfinder/NumFinder.java | 6 ++---- src/main/java/tudelft/numfinder/NumFinderMain.java | 2 +- src/test/java/tudelft/roman/RomanNumeralTest.java | 14 +++++++++++++- .../roman/RomanNumeralTestWithBeforeEach.java | 14 +++++++++++--- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/tudelft/numfinder/NumFinder.java b/src/main/java/tudelft/numfinder/NumFinder.java index 3d0cf32c0..9b0ebef2d 100644 --- a/src/main/java/tudelft/numfinder/NumFinder.java +++ b/src/main/java/tudelft/numfinder/NumFinder.java @@ -15,11 +15,9 @@ public void find(int[] nums) { } } - public int getSmallest () { - return smallest; + public int getSmallest () {return smallest; } - public int getLargest () { - return largest; + public int getLargest () {return largest; } } diff --git a/src/main/java/tudelft/numfinder/NumFinderMain.java b/src/main/java/tudelft/numfinder/NumFinderMain.java index 32912d5eb..f29654ef3 100644 --- a/src/main/java/tudelft/numfinder/NumFinderMain.java +++ b/src/main/java/tudelft/numfinder/NumFinderMain.java @@ -9,7 +9,7 @@ public static void main (String[] args) { // nf.find(new int[] {4, 25, 7, 9}); // this crashes - nf.find(new int[] {4, 3, 2, 1}); + nf.find(new int[] {4, 3, 2, 1, 0,}); System.out.println(nf.getLargest()); System.out.println(nf.getSmallest()); diff --git a/src/test/java/tudelft/roman/RomanNumeralTest.java b/src/test/java/tudelft/roman/RomanNumeralTest.java index 6c04c104f..6c43b478c 100644 --- a/src/test/java/tudelft/roman/RomanNumeralTest.java +++ b/src/test/java/tudelft/roman/RomanNumeralTest.java @@ -1,11 +1,11 @@ package tudelft.roman; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class RomanNumeralTest { - @Test public void singleNumber() { RomanNumeral roman = new RomanNumeral(); @@ -33,4 +33,16 @@ public void numberWithAndWithoutSubtractiveNotation() { int result = roman.convert("XLIV"); Assertions.assertEquals(44, result); } + @Test + public void numberWithandWithoutLargeSubtractiveNotation() { + RomanNumeral roman = new RomanNumeral(); + int result = roman.convert("XCIX"); + Assertions.assertEquals(99, result); + } + @Test + public void numberWithLargeSubtractiveNotation() { + RomanNumeral roman = new RomanNumeral(); + int result = roman.convert("CD"); + Assertions.assertEquals(400, result); + } } diff --git a/src/test/java/tudelft/roman/RomanNumeralTestWithBeforeEach.java b/src/test/java/tudelft/roman/RomanNumeralTestWithBeforeEach.java index 0925df792..86698ffa0 100644 --- a/src/test/java/tudelft/roman/RomanNumeralTestWithBeforeEach.java +++ b/src/test/java/tudelft/roman/RomanNumeralTestWithBeforeEach.java @@ -9,9 +9,7 @@ public class RomanNumeralTestWithBeforeEach { private RomanNumeral roman; @BeforeEach - public void initialize() { - this.roman = new RomanNumeral(); - } + public void initialize() {this.roman = new RomanNumeral();} @Test public void singleNumber() { @@ -36,4 +34,14 @@ public void numberWithAndWithoutSubtractiveNotation() { int result = roman.convert("XLIV"); Assertions.assertEquals(44, result); } + @Test + public void numberWithandWithoutLargeSubtractiveNotation() { + int result = roman.convert("XCIX"); + Assertions.assertEquals(99, result); + } + @Test + public void numberWithLargeSubtractiveNotation() { + int result = roman.convert("CD"); + Assertions.assertEquals(400, result); + } } From ed7096ec0b9bd350bb03426a0f7d5aa26532c31e Mon Sep 17 00:00:00 2001 From: DonSylver <141275471+DonSylver@users.noreply.github.com> Date: Tue, 13 Feb 2024 11:52:10 +0000 Subject: [PATCH 4/5] GHappyTest and CaesarShiftCipherTest problems solved and updated --- .../caesarshift/CaesarShiftCipher.java | 16 ++++--- src/main/java/tudelft/ghappy/GHappy.java | 3 +- .../caesarshift/CaesarShiftCipherTest.java | 43 +++++++++++++++++++ src/test/java/tudelft/ghappy/GHappyTest.java | 42 ++++++++++++++++++ 4 files changed, 97 insertions(+), 7 deletions(-) diff --git a/src/main/java/tudelft/caesarshift/CaesarShiftCipher.java b/src/main/java/tudelft/caesarshift/CaesarShiftCipher.java index 299405e70..f62ca6e10 100644 --- a/src/main/java/tudelft/caesarshift/CaesarShiftCipher.java +++ b/src/main/java/tudelft/caesarshift/CaesarShiftCipher.java @@ -2,18 +2,24 @@ public class CaesarShiftCipher { - public String CaesarShiftCipher(String message, int shift){ + public String caesarShiftCipher(String message, int shift){ StringBuilder sb = new StringBuilder(); char currentChar; int length = message.length(); - shift = shift%26; + shift = shift % 26; - for(int i = 0; i < length; i++){ + for (int i = 0; i < length; i++){ currentChar = message.charAt(i); - sb.append(currentChar); - if (currentChar > 'z' || currentChar < 'a') { +// sb.append(currentChar); + //The above line is wrong as it tries to + //appending the original character and the shifted character to the StringBuilder + // within the loop. This results in each character being appended twice. + // You should append only the shifted character after the necessary checks. + + if (currentChar < 'a' || currentChar > 'z') { + // Handle invalid characters here, either throw an exception or return a special value return "invalid"; } else if ((char) (currentChar + shift) > 'z') { currentChar = (char) (currentChar - 26); diff --git a/src/main/java/tudelft/ghappy/GHappy.java b/src/main/java/tudelft/ghappy/GHappy.java index 62136ea2b..13139fc8e 100644 --- a/src/main/java/tudelft/ghappy/GHappy.java +++ b/src/main/java/tudelft/ghappy/GHappy.java @@ -6,12 +6,11 @@ public boolean gHappy(String str) { assert str!=null; for(int i = 0; i < str.length(); i++) { if(str.charAt(i) == 'g') { - if (i >= 0 && str.charAt(i-1) == 'g') { continue; } + if (i > 0 && str.charAt(i-1) == 'g') { continue; } if (i+1 < str.length() && str.charAt(i+1) == 'g') { continue; } return false; } } - return true; } diff --git a/src/test/java/tudelft/caesarshift/CaesarShiftCipherTest.java b/src/test/java/tudelft/caesarshift/CaesarShiftCipherTest.java index f11db8abd..4bc68de91 100644 --- a/src/test/java/tudelft/caesarshift/CaesarShiftCipherTest.java +++ b/src/test/java/tudelft/caesarshift/CaesarShiftCipherTest.java @@ -1,5 +1,48 @@ package tudelft.caesarshift; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import tudelft.ghappy.GHappy; + public class CaesarShiftCipherTest { + private CaesarShiftCipher encrypt; + @BeforeEach + public void initialize() {this.encrypt = new CaesarShiftCipher();} + + @Test + public void encryptTest_01() { + String encrypt_A = encrypt.caesarShiftCipher("abc",3); + Assertions.assertEquals("def", encrypt_A); + } + @Test + public void encryptTest_02() { + String encrypt_A = encrypt.caesarShiftCipher("xyz",3); + Assertions.assertEquals("abc", encrypt_A); + } + @Test + public void encryptTest_03() { + String encrypt_A = encrypt.caesarShiftCipher("ABC",3); + Assertions.assertEquals("invalid", encrypt_A); + //Capital letters should return invalid + } + @Test + public void encryptTest_04() { + String encrypt_A = encrypt.caesarShiftCipher("abc",4); + Assertions.assertEquals("efg", encrypt_A); + // shifting at 4 spaces + } + @Test + public void encryptTest_05() { + String encrypt_A = encrypt.caesarShiftCipher("ace",3); + Assertions.assertEquals("dfh", encrypt_A); + //a plus 3 shifts equals d, c plus 3 shifts is f and e plus 3 shifts is h, "dfh" + } + @Test + public void encryptTest_06() { + String encrypt_A = encrypt.caesarShiftCipher("123",3); + Assertions.assertEquals("invalid", encrypt_A); + // "456" is not expected as anything out small letters 'a to z' is "invalid" + } } diff --git a/src/test/java/tudelft/ghappy/GHappyTest.java b/src/test/java/tudelft/ghappy/GHappyTest.java index db30fa765..c42a5c5d3 100644 --- a/src/test/java/tudelft/ghappy/GHappyTest.java +++ b/src/test/java/tudelft/ghappy/GHappyTest.java @@ -1,4 +1,46 @@ package tudelft.ghappy; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + + public class GHappyTest { + private GHappy G; + + @BeforeEach + public void initialize() {this.G = new GHappy();} + + /** + * Test the following conditions + * checkG_001 gHappy("xxggxx") ==> true + * checkG_002 gHappy("xxgxx") ==> false + * checkG_003 gHappy("xxggyygxx") ==> false + */ + @Test + public void checkG_001() { + boolean check001 = G.gHappy("ABggCD"); + Assertions.assertTrue(check001); + } + @Test + public void checkG_002() { + boolean check001 = G.gHappy("ABgCD"); + Assertions.assertFalse(check001); + } + @Test + public void checkG_003() { + boolean check001 = G.gHappy("ABggCDgEF"); + Assertions.assertFalse(check001); + } + @Test + public void checkG_004() { + boolean check001 = G.gHappy("ggABggCD"); + Assertions.assertTrue(check001); + } + @Test + public void checkG_005() { + boolean check001 = G.gHappy("ABggCDgg"); + Assertions.assertTrue(check001); + } } + From ded51fd305c7a4794a055accb743503a53f383a3 Mon Sep 17 00:00:00 2001 From: DonSylver <141275471+DonSylver@users.noreply.github.com> Date: Tue, 13 Feb 2024 12:03:55 +0000 Subject: [PATCH 5/5] The exercise questions for HappyTest and CaesarShiftCipherTest have been added as comments to the classes --- .../caesarshift/CaesarShiftCipherTest.java | 14 ++++++++++++++ src/test/java/tudelft/ghappy/GHappyTest.java | 18 +++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/test/java/tudelft/caesarshift/CaesarShiftCipherTest.java b/src/test/java/tudelft/caesarshift/CaesarShiftCipherTest.java index 4bc68de91..a0b756ab7 100644 --- a/src/test/java/tudelft/caesarshift/CaesarShiftCipherTest.java +++ b/src/test/java/tudelft/caesarshift/CaesarShiftCipherTest.java @@ -5,6 +5,20 @@ import org.junit.jupiter.api.Test; import tudelft.ghappy.GHappy; +/** + * A Caesar Cipher is a simply way to encrypt messages. + * There are two inputs: A message and a shift. + * The message can be any string containing lower case letters and spaces, + * any other characters will return 'invalid' The shift can be any positive or negative integer. + * Each letter of the message is then shifted by the specified amount + * (if shift is '3' then 'abc' becomes 'def' and 'xyz' becomes 'abc'). + * + * See our implementation in the CaesarCipherShift class (package tudelft.caesarcipher). + * There's a bug in this implementation. Apply everything you learned and do your best to find the bug!! + * Write your automated tests inside CaeserCipherShiftTest class. + * At the end, push your solution to your GitHub repository. + */ + public class CaesarShiftCipherTest { private CaesarShiftCipher encrypt; @BeforeEach diff --git a/src/test/java/tudelft/ghappy/GHappyTest.java b/src/test/java/tudelft/ghappy/GHappyTest.java index c42a5c5d3..a994de0b1 100644 --- a/src/test/java/tudelft/ghappy/GHappyTest.java +++ b/src/test/java/tudelft/ghappy/GHappyTest.java @@ -12,10 +12,22 @@ public class GHappyTest { public void initialize() {this.G = new GHappy();} /** + * We'll say that a lowercase 'g' in a string is "happy" if there is another 'g' immediately to its left or right. + * Return true if all the g's in the given string are happy. + * + * Examples: + * gHappy("xxggxx") ==> true + * gHappy("xxgxx") ==> false + * gHappy("xxggyygxx") ==> false + * See our implementation in the GHappy class (package tudelft.ghappy). + * There's a bug in this implementation. Apply everything you learned and do your best to find the bug!! + * Write your automated tests inside GHappyTest class. + * At the end, push your solution to your GitHub repository. + * * Test the following conditions - * checkG_001 gHappy("xxggxx") ==> true - * checkG_002 gHappy("xxgxx") ==> false - * checkG_003 gHappy("xxggyygxx") ==> false + * checkG_001 gHappy("xxggxx") ==> true + * checkG_002 gHappy("xxgxx") ==> false + * checkG_003 gHappy("xxggyygxx") ==> false */ @Test public void checkG_001() {