/* * [Martian.java] * * Summary: Display a numbmer in Martian (an artificial language that uses base 16 numbers). * * Copyright: (c) 1999-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 1999-01-17 * 1.1 1999-01-21 make all language specific String literals into constants. */ package com.mindprod.inwords; /** * Display a numbmer in Martian (an artificial language that uses base 16 numbers). *

* e.g. * -12345 -> minus cocu aocu iu. * This is just having fun, inventing a rational number naming system. *

* Martian counting is very simple. * Every power of 16 has a name ending in o: ao, bo, co etc. * Every digit has a name ending in u: au, bu, cu etc. * A number is formed of a pairs, written as one word, a power of 16 and a * multiplier, the reverse of usual languages, in order to put the most significant * information first. *

* Why is this code written in an unusual way? see inwords.use *

* * @author Roedy Green, Canadian Mind Products * @version 1.1 1999-01-21 make all language specific String literals into constants. * @noinspection UnusedDeclaration * @since 1999-01-17 */ public final class Martian implements ToWords { private static final int FIRST_COPYRIGHT_YEAR = 1999; /** * undisplayed copyright notice */ private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 1999-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; private static final String MINUS = "minus"; private static final String ZERO = "au"; private static final String[] digitName = { /* names of digits 0..15 */ "au", "bu", "cu", "du", "eu", "fu", "gu", "hu", "iu", "ju", "ku", "lu", "mu", "nu", "ou", "pu" }; private static final String[] groupName = { // every power of 16 is named a0=16^0 = 1 "", "bo", "co", "do", "eo", "fo", "go", "ho", "io", "jo", "ko", "lo", "mo", "no", "po", "qo" }; /** * test harness * * @param args not used */ public static void main( String[] args ) { Test.test( new Martian() ); } // end main /** * convert long integer into Martian words e.g. -12345 -> minus cocu aocu iu. Handles negative and positive integers * on range -Long.MAX_VALUE .. Long.MAX_VALUE; It cannot handle Long.MIN_VALUE; * * @param num number to convert to words * * @return words */ @SuppressWarnings( { "WeakerAccess" } ) public String toWords( long num ) { if ( num == 0 ) { return ZERO; } boolean negative = ( num < 0 ); if ( negative ) { num = -num; } String s = ""; // Work least significant digit to most, right to left. // until high order part is all 0s. for ( int group = 0; num > 0; group++ ) { int remdr = ( int ) ( num % 16 ); num = num / 16; if ( remdr == 0 ) { continue; } s = groupName[ group ] + digitName[ remdr ] + " " + s; } // end for s = s.trim(); if ( negative ) { s = MINUS + " " + s; } return s; } // end inWords } // end Martian