Skip to content

Instantly share code, notes, and snippets.

@FaAway
Created March 19, 2016 12:55
Show Gist options
  • Save FaAway/a88569a1045bc84fd65f to your computer and use it in GitHub Desktop.
Save FaAway/a88569a1045bc84fd65f to your computer and use it in GitHub Desktop.
javarush level30.lesson02.home01
package com.javarush.test.level30.lesson02.home01;
import java.util.Objects;
public class Number {
private NumerationSystem numerationSystem;
private String digit;
public Number(NumerationSystem numerationSystem, String digit) {
this.numerationSystem = numerationSystem;
this.digit = digit;
}
public NumerationSystem getNumerationSystem() {
return numerationSystem;
}
public String getDigit() {
return digit;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Number number = (Number) o;
return Objects.equals(numerationSystem, number.numerationSystem) &&
Objects.equals(digit, number.digit);
}
@Override
public int hashCode() {
return Objects.hash(numerationSystem, digit);
}
@Override
public String toString() {
return "Number{" +
"numerationSystem=" + numerationSystem +
", digit='" + digit + '\'' +
'}';
}
}
package com.javarush.test.level30.lesson02.home01;
public interface NumerationSystem {
int getNumerationSystemIntValue();
}
package com.javarush.test.level30.lesson02.home01;
public enum NumerationSystemType implements NumerationSystem {
_16,
_12,
_10,
_9,
_8,
_7,
_6,
_5,
_4,
_3,
_2;
static char[] numerics = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
@Override
public int getNumerationSystemIntValue() {
return Integer.parseInt(this.name().substring(1));
}
}
package com.javarush.test.level30.lesson02.home01;
import java.math.BigInteger;
/* Конвертер систем счислений
Реализуйте логику метода convertNumberToOtherNumerationSystem, который должен переводить число number.getDigit()
из одной системы счисления(numerationSystem) в другую (expectedNumerationSystem)
бросьте NumberFormatException, если переданное число некорректно, например, число "120" с системой счисления 2
Валидация для - number.getDigit() - целое не отрицательное
Метод main не участвует в тестировании
*/
public class Solution {
public static void main(String[] args) {
Number number = new Number(NumerationSystemType._10, "6");
Number result = convertNumberToOtherNumerationSystem(number, NumerationSystemType._2);
System.out.println(result); //expected 110
}
public static Number convertNumberToOtherNumerationSystem(Number number, NumerationSystem expectedNumerationSystem) throws NumberFormatException {
/* StringBuilder expectedDigit = new StringBuilder();
int base = number.getNumerationSystem().getNumerationSystemIntValue();
long tempValue = Long.parseLong(number.getDigit(), base);
int expectedBase = expectedNumerationSystem.getNumerationSystemIntValue();
if (tempValue == 0)
expectedDigit.append("0");
else while (tempValue > 0) {
expectedDigit.append(NumerationSystemType.numerics[(int)(tempValue % expectedBase)]);
tempValue = tempValue / expectedBase;
}
return new Number(expectedNumerationSystem, expectedDigit.reverse().toString());*/
// 2nd way:
BigInteger a = new BigInteger(number.getDigit(), number.getNumerationSystem().getNumerationSystemIntValue());
return new Number(expectedNumerationSystem, a.toString(expectedNumerationSystem.getNumerationSystemIntValue()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment