Skip to content

Banhat #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,23 @@ public class RecursiveDigitSum {
* @return recursive sum of the digits
*/
private static int superDigit(String n, int k) {
if(n == null) throw new Exception("by input exception [" + n + "]");
if (n.length() == 1 && k == 0) {
return Integer.parseInt(n);
try {
return Integer.parseInt(n);
}catch (Exception e) {
throw new Exception("by input exception [" + n + "]");
}
}

Long sum = 0L;
char[] num = n.toCharArray();
for (int i = 0; i < num.length; i++) {
sum += Long.parseLong(String.valueOf(num[i]));
try {
sum += Long.parseLong(String.valueOf(num[i]));
} catch(Exception ex) {
throw new Exception("by input exception [" + n + "]");
}
}

if (k != 0) sum *= k;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package recursion;

import static org.junit.jupiter.api.Assertions.assertThrows;

import javax.annotation.processing.Generated;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

@Generated(value = "org.junit-tools-1.1.0")
public class RecursiveDigitSumTest {

@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("BeforeClass");
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("AfterClass");
}

@Test
public void testSuperDigitWithNull() throws Exception {
// TC1
int k = 0;
String n = null;
String expected = "by input exception [" + n + "]";
Exception result = assertThrows(Exception.class,
() -> RecursiveDigitSum.superDigit(n, k));

Assert.assertEquals(expected, result.getMessage());
}

@Test
public void testSuperDigitWithEmpty() throws Exception {
// TC2
int k = 1;
String n = "";
int expected = 0;
int result = RecursiveDigitSum.superDigit(n, k);

Assert.assertEquals(expected, result);
}

@Test
public void testSuperDigitWithNegative1() throws Exception {
// TC3
int k = -1;
String n = "1";
String expected = "by input exception [" + k + "]";
Exception result = assertThrows(Exception.class,
() -> RecursiveDigitSum.superDigit(n, k));

Assert.assertEquals(expected, result.getMessage());
}

@Test
public void testSuperDigit1() throws Exception {
// TC4
int k = 0;
String n = "1";
int expected = 1;
int result = RecursiveDigitSum.superDigit(n, k);

Assert.assertEquals(expected, result);
}

@Test
public void testSuperDigitMax1() throws Exception {
// TC5
int k = 999999999;
String n = "1";
int expected = 9;
int result = RecursiveDigitSum.superDigit(n, k);

Assert.assertEquals(expected, result);
}

@Test
public void testSuperDigit3() throws Exception {
// TC6
int k = 0;
String n = "12";
int expected = 3;
int result = RecursiveDigitSum.superDigit(n, k);

Assert.assertEquals(expected, result);
}

@Test
public void testSuperDigit4() throws Exception {
// TC7
int k = 1;
String n = "12";
int expected = 3;
int result = RecursiveDigitSum.superDigit(n, k);

Assert.assertEquals(expected, result);
}

@Test
public void testSuperDigitMax2() throws Exception {
// TC8
int k = 1;
String n = "999999999";
int expected = 9;
int result = RecursiveDigitSum.superDigit(n, k);

Assert.assertEquals(expected, result);
}

@Test
public void testSuperDigitWithNegative2() throws Exception {
// TC9
int k = 1;
String n = "-a";
String expected = "by input exception [" + n + "]";
Exception result = assertThrows(Exception.class,
() -> RecursiveDigitSum.superDigit(n, k));

Assert.assertEquals(expected, result.getMessage());
}

}