Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Ternary-JUnit
- Program
- package PRM_20180125;
- public class Ternary {
- public static String fromDecadicToTernary(int a) {
- String vysledek = "";
- String konec = "";
- int pom = a;
- int cislo = 0;
- if(pom == 0) {
- return "0";
- } else {
- while(pom > 0) {
- cislo = pom % 3;
- vysledek += cislo;
- pom = pom / 3;
- }
- return new StringBuilder(vysledek).reverse().toString();
- }
- }
- public static int fromTernaryToDecadic(String a) {
- String pom = a;
- int vysledek = 0;
- double cislo = 0;
- for (int i = 0; i < pom.length(); i++) {
- //cislo = Integer.parseInt(String.valueOf(pom.charAt(i)));
- cislo = Character.getNumericValue(pom.charAt(i));
- int moc = pom.length()-1-i;
- vysledek += cislo * Math.pow(3, moc);
- }
- return vysledek;
- }
- public static void main(String[] args) {
- int a = 3;
- String aa = "101";
- System.out.println(fromDecadicToTernary(a));
- System.out.println(fromTernaryToDecadic(aa));
- //System.out.println(Math.pow(3, 0));
- }
- }
- Test
- package PRM_20180125;
- import org.junit.Test;
- import static org.junit.Assert.*;
- public class TernaryTest {
- private final int expectedDekadic[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 30, 99, 100, 666, 1000, 1000000};
- private final String expectedTernary[] = {"0", "1", "2", "10", "11", "12", "20", "21", "22", "100", "101", "120", "1010", "10200", "10201", "220200", "1101001", "1212210202001"};
- @Test
- public void testFromDecadicToTernary() {
- for (int i = 0; i < expectedDekadic.length; i++) {
- assertEquals(expectedTernary[i], Ternary.fromDecadicToTernary(expectedDekadic[i]));
- }
- }
- @Test
- public void testFromTernaryToDecadic() {
- for (int i = 0; i < expectedDekadic.length; i++) {
- assertEquals(expectedDekadic[i], Ternary.fromTernaryToDecadic(expectedTernary[i]));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement