Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Octal in the Java Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Convert an octal number, represented as a string (e.g. '1735263'), to its decimal equivalent using first principles (i.e. no, you may not use built-in or external libraries to accomplish the conversion).

Implement octal to decimal conversion. Given an octal input string, your program should produce a decimal output.

Note

  • Implement the conversion yourself. Do not use something else to perform the conversion for you.
  • Treat invalid input as octal 0.

About Octal (Base-8)

Decimal is a base-10 system.

A number 233 in base 10 notation can be understood as a linear combination of powers of 10:

  • The rightmost digit gets multiplied by 10^0 = 1
  • The next number gets multiplied by 10^1 = 10
  • ...
  • The nth number gets multiplied by 10^(n-1).
  • All these values are summed.

So:

   233 # decimal
 = 2*10^2 + 3*10^1 + 3*10^0
 = 2*100  + 3*10   + 3*1

Octal is similar, but uses powers of 8 rather than powers of 10.

So:

   233 # octal
 = 2*8^2 + 3*8^1 + 3*8^0
 = 2*64  + 3*8   + 3*1
 = 128   + 24    + 3
 = 155

Running the tests

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.

Source

All of Computer Science http://www.wolframalpha.com/input/?i=base+8

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

OctalTest.java

import org.junit.Test;
import org.junit.Ignore;

import static org.junit.Assert.assertEquals;

public class OctalTest {

    @Test
    public void testCovertDecimalOneToOctal() {
        Octal octal = new Octal("1");
        assertEquals(1, octal.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testCovertDecimalTenToOctal() {
        Octal octal = new Octal("10");
        assertEquals(8, octal.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testCovertDecimalSeventeenToOctal() {
        Octal octal = new Octal("17");
        assertEquals(15, octal.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testCovertElevenToOctal() {
        Octal octal = new Octal("11");
        assertEquals(9, octal.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testCovertDecimalElevenWithZeroToOctal() {
        Octal octal = new Octal("011");
        assertEquals(9, octal.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testThreeDigitDecimalToOctal() {
        Octal octal = new Octal("130");
        assertEquals(88, octal.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testSmallFourDigitDecimalToOctal() {
        Octal octal = new Octal("2047");
        assertEquals(1063, octal.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testLargeFourDigitDecimalToOctal() {
        Octal octal = new Octal("7777");
        assertEquals(4095, octal.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testSevenDigitDecimalToOctal() {
        Octal octal = new Octal("1234567");
        assertEquals(342391, octal.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testInvalidStringFirst() {
        Octal octal = new Octal("carrot");
        assertEquals(0, octal.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testInvalidStringSecond() {
        Octal octal = new Octal("abc1z");
        assertEquals(0, octal.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testInvalidDecimalEight() {
        Octal octal = new Octal("8");
        assertEquals(0, octal.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testInvalidNumberNine() {
        Octal octal = new Octal("9");
        assertEquals(0, octal.getDecimal());
    }

}
import java.lang.Math;

public final class Octal {

  private String input;
  private int decimal;

  public Octal(String input) {
    this.input = input;
    decimal = calculateDecimal();
  }

  public int getDecimal() {
    return decimal;
  }

  private int calculateDecimal() {
    int decimal = 0;
    if (!input.matches("[0-7]+")) { return decimal; }

    for (int i = 0; i < input.length(); i++) {
      int curr = Character.getNumericValue(input.charAt(i));
      int mult =  (int) Math.pow(8, input.length() - 1 - i);
      decimal += (curr * mult);
    }
    return decimal;
  }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?