Tuesday 8 May 2012

A Java Question asked by Goldmans Sachs

Hello everyone,
This is my first attempt to write a blog. As my first article I would like to share an answer to a question which was asked for "Goldmans Sachs" interview. I was not able to write this program in the given time @ the written test...but successfully executed with desired results on my laptop....you can try it out.Any comments or suggestions or any other better and optimised approach to solve the same are most welcome.


Problem statement: Design and code a digital printer which takes a numeric user input (max 10 digit) and prints it in its digital format. ie: if user enters 23 then output should be:   -     -
                                                                                                          _|    _|
                                                                                                          |_    _|


Answer:


/*Program to take a numeric input (max 10 digits) and print it in digital format
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class DigitalPrint
{
  int number = 0;


  boolean[] digitStore ={true,true,false,true,true,true,true,
                           false,false,false,true,false,false,true,
                           true,false,true,true,true,true,false,
                           true,false,true,true,false,true,true,
                           false,true,true,true,false,false,true,
                           true,true,true,false,false,true,true,
                           true,true,true,false,true,true,true,
                           true,false,false,true,false,false,true,
                           true,true,true,true,true,true,true,
                           true,true,true,true,false,true,true,
                        
                           };
                       


  /**
   *
   * @param args
   */
  public static void main(String[] args)
  {
    String input = "";
    DigitalPrint digital = new DigitalPrint();
 
    try{
    InputStreamReader inp = new InputStreamReader(System.in);
    BufferedReader reader = new BufferedReader(inp);
    System.out.print("Please enter the number: ");
    input = reader.readLine();
    }
    catch(IOException e)
    {
      e.printStackTrace();
    }
    digital.parseNum(input);
  }


  public void parseNum(String num)
  {
    int[ ] numArray = new int[10] ;
    try{
    number = Integer.parseInt(num);
    }
    catch(NumberFormatException n)
    {
     System.out.println("Input is a non Integer.Please check the Input!");
     n.printStackTrace();
     System.exit(1);
    }
    int temp = number, i=0;
    for(i=0; i< num.length();i++){
       numArray[i] = temp%10;
       temp = number/10;
       number = temp;
    }
    digitalPrinting(numArray, i);
  
  }




  public void digitalPrinting(int[] numArray, int arrayLength)
  {
   boolean[] digit = new boolean[arrayLength*7];
   int pos = 0;
   int s = 0;
    for(int k = arrayLength -1 ; k>=0; k--)
    {
          System.arraycopy(digitStore,numArray[k]*7,digit,s,7);
          s = s+7;  
       
    }
    int x = 0;
    System.out.print("\n");
    while( x<=digit.length-1 )
    {
      if(digit[x])
       System.out.print("  _ ");
      else
       System.out.print("    ");
      x += 7;
    }
   printLine(digit,1);
   printLine(digit,4);
  }




  public void printLine(boolean[] digit, int pos ){
    System.out.print("\n");
    int z = pos;
   while( z <= digit.length-1)
    {
      if(digit[z])
      System.out.print(" |");
      else
      System.out.print("  ");
      if(digit[z+1])
      System.out.print("_");
      else
      System.out.print(" ");
      if(digit[z+2])
      System.out.print("|");
      else
      System.out.print(" ");
   
    z+=7;
    }


  }
}



Output:

No comments:

Post a Comment