P005 -- LCD Display

Please restrict discussions in this forum to project related topics.

P005 -- LCD Display

Postby DrJump » Thu Sep 09, 2010 5:43 pm

Popularity: A
Success rate: average
Level: 1

A friend of yours has just bought a new computer. Before this, the most powerful machine he ever used was a pocket calculator. He is a little disappointed because he liked the LCD display of his calculator more than the screen on his new computer! To make him happy, write a program that prints numbers in LCD display style.

The Input
The input file contains several lines, one for each number to be displayed. Each line contains integers s and n, where n is the number to be displayed (0 <= n <= 99,999,999) and s is the size in which it shall be displayed (0 <= s <= 10). The input will be terminated by a line containing two zeros, which should not be processed.

The Output
Print the numbers specified in the input file in an LCD display-style using s "-" signs for the horizontal segments and s "|" signs for the vertical ones. Each digit occupies exactly s+2 columns and 2s+3 rows. Be sure to fill all the white space occupied by the digits with blanks, including the last digit. There must be exactly one column of blanks between two digits.

Output a blank line after each number. You will find an example of each digit in the sample output below.

Sample Input
Code: Select all
2 12345
3 67890
0 0


Sample Output
Code: Select all
      --   --        --
   |    |    | |  | |   
   |    |    | |  | |   
      --   --   --   -- 
   | |       |    |    |
   | |       |    |    |
      --   --        --

 ---   ---   ---   ---   ---
|         | |   | |   | |   |
|         | |   | |   | |   |
|         | |   | |   | |   |
 ---         ---   ---       
|   |     | |   |     | |   |
|   |     | |   |     | |   |
|   |     | |   |     | |   |
 ---         ---   ---   ---
DrJump
 
Posts: 19
Joined: Sun Mar 07, 2010 6:08 pm

Re: P005 -- LCD Display

Postby MichaelVasquez » Fri Sep 17, 2010 1:57 am

It is working but there are two things that I may cause it to be wrong in the competition. One is that how should I keep the program running until the terminal is exited or until 0 0 is entered. I did figure out a way to stop processing after 0 0 is entered but I am not sure if the machine judging it will accept it. Another thing is how the numbers are visually displayed, but that is an easy fix by tweeking the methods controlling each numbers graphics. If anyone see's any errors or has trouble understanding the code I am glad to help :)

Code: Select all
import java.util.Scanner;

/*
* KCCMain.java
* Programming shell to start us out with at King's Coding Club
*/

public class KCCMain
{
    /* Scanner.java is in the Java Class Library and you can look it up to see how to use it
     * but for brevity here are some of the methods:
     *
     * boolean hasNext()      Returns true if this scanner has another token in its input
     * boolean nextBoolean()  Scans next token of the input into a boolean value and returns it
     * double nextDouble()    Scans next token of the input into a double value and returns it
     * float nextFloat()      Scans next token of the input into a float value and returns it
     * int nextInt()          Scans next token of the input into a int value and returns it
     * String nextLine()      Advances the scanner past the current line and
     *                                 returns the input that was skipped
     */
    Scanner input;
   
    String lnOne = "", lnTwo= "", lnThree= "", lnFour= "", lnFive= "";
    String space = " ";
    String dash = "-";
    String col = "|";
    static boolean runn = true;
   
   
    public KCCMain()
    {
        input = new Scanner(System.in);           
     
    }
    // entry point for running
    public static void main(String args[])
    {
        KCCMain program = new KCCMain();
        while(runn){
            program.run();
        }
        System.exit(0);
       
    }
   
    public void zero(int s){
        //TDraws 0
         lnOne += space;
        lnTwo += col;
        lnThree += space;
        lnFour += col;
        lnFive += space;
       
       
        for(int i = 1; i<s+1; i++){
            lnOne += dash;
            lnTwo += space;
            lnThree += space;
            lnFour += space;
            lnFive += dash;
        }
       
        lnOne += space;
        lnTwo += col;
        lnThree += space;
        lnFour += col;
        lnFive += space;
    }
   
    public void one(int s){
        //Draws 1
       
        lnOne += space;
        lnTwo += space;
        lnThree += space;
        lnFour += space;
        lnFive += space;
       
        for(int i = 1; i<s+1; i++){
            lnOne += space;
            lnTwo += space;
            lnThree += space;
            lnFour += space;
            lnFive += space;
        }
       
        lnOne += space;
        lnTwo += col;
        lnThree += space;
        lnFour += col;
        lnFive += space;
    }
   

    public void two(int s){
        //Draws 2
        lnOne += space;
        lnTwo += space;
        lnThree += space;
        lnFour += col;
        lnFive += space;
       
       
        for(int i = 1; i<s+1; i++){
            lnOne += dash;
            lnTwo += space;
            lnThree += dash;
            lnFour += space;
            lnFive += dash;
        }
       
        lnOne += space;
        lnTwo += col;
        lnThree += space;
        lnFour += space;
        lnFive += space;
    }
   
    public void three(int s){
        //draws 3
        lnOne += space;
        lnTwo += space;
        lnThree += space;
        lnFour += space;
        lnFive += space;
       
       
        for(int i = 1; i<s+1; i++){
            lnOne += dash;
            lnTwo += space;
            lnThree += dash;
            lnFour += space;
            lnFive += dash;
        }
       
        lnOne += space;
        lnTwo += col;
        lnThree += space;
        lnFour += col;
        lnFive += space;               
       
   
    }
   
    public void four(int s){
        lnOne += space;
        lnTwo += col;
        lnThree += space;
        lnFour += space;
        lnFive += space;
       
       
        for(int i = 1; i<s+1; i++){
            lnOne += space;
            lnTwo += space;
            lnThree += dash;
            lnFour += space;
            lnFive += space;
        }
       
        lnOne += space;
        lnTwo += col;
        lnThree += space;
        lnFour += col;
        lnFive += space;
               
    }
   
    public void five(int s){
       lnOne += space;
        lnTwo += col;
        lnThree += space;
        lnFour += space;
        lnFive += space;
       
       
        for(int i = 1; i<s+1; i++){
            lnOne += dash;
            lnTwo += space;
            lnThree += dash;
            lnFour += space;
            lnFive += dash;
        }
       
        lnOne += space;
        lnTwo += space;
        lnThree += space;
        lnFour += col;
        lnFive += space;
   
    }
   
    public void six(int s){
        lnOne += space;
        lnTwo += col;
        lnThree += space;
        lnFour += col;
        lnFive += space;
       
       
        for(int i = 1; i<s+1; i++){
            lnOne += dash;
            lnTwo += space;
            lnThree += dash;
            lnFour += space;
            lnFive += dash;
        }
       
        lnOne += space;
        lnTwo += space;
        lnThree += space;
        lnFour += col;
        lnFive += space;
     
    }
   
    public void seven(int s){
        lnOne += space;
        lnTwo += space;
        lnThree += space;
        lnFour += space;
        lnFive += space;
       
       
        for(int i = 1; i<s+1; i++){
            lnOne += dash;
            lnTwo += space;
            lnThree += space;
            lnFour += space;
            lnFive += space;
        }
       
        lnOne += space;
        lnTwo += col;
        lnThree += space;
        lnFour += col;
        lnFive += space;
   
    }
    public void eight(int s){
        lnOne += space;
        lnTwo += col;
        lnThree += space;
        lnFour += col;
        lnFive += space;
       
       
        for(int i = 1; i<s+1; i++){
            lnOne += dash;
            lnTwo += space;
            lnThree += dash;
            lnFour += space;
            lnFive += dash;
        }
       
        lnOne += space;
        lnTwo += col;
        lnThree += space;
        lnFour += col;
        lnFive += space;
   
    }
   
    public void nine(int s){
        lnOne += space;
        lnTwo += col;
        lnThree += space;
        lnFour += space;
        lnFive += space;
       
       
        for(int i = 1; i<s+1; i++){
            lnOne += dash;
            lnTwo += space;
            lnThree += dash;
            lnFour += space;
            lnFive += dash;
        }
       
        lnOne += space;
        lnTwo += col;
        lnThree += space;
        lnFour += col;
        lnFive += space;
   
    }
   
    // your solutions should begin in this method
    public void run()
    {
       KCCMain m = new KCCMain();
       int s = input.nextInt();
       
       if(s != 0){
       
       String numbers = input.nextLine().trim();
       
       for(int i = 0; i < numbers.length(); i++){
       
            char b = numbers.charAt(i);
            //System.out.println(numbers.length());
       

       
            switch(b){
         
                case '0': {
                    //System.out.println("0");
                    zero(s);
                    break;
                }
         
                case '1': {
                    //System.out.println("1");
                    one(s);
                    break;
                }
       
                case '2': {
                    two(s);
                    break;
                }
       
                case '3': {
                    three(s);
                    break;
                }

                case '4': {
                    four(s);
                    break;
                }
       
                case '5': {
                    five(s);
                    break;
                }
       
                case '6': {
                    six(s);
                    break;
                }
       
                case '7': {
                    seven(s);
                    break;
                }
       
                case '8': {
                    eight(s);
                    break;
                }
       
                case '9': {
                    nine(s);
                    break;
                }
            }
           
            lnOne += space;
            lnTwo += space;
            lnThree += space;
            lnFour  += space;
            lnFive  += space;
    }
       
       System.out.println(lnOne);
       for(int i = 1; i < s +1; i++){
            System.out.println(lnTwo);
       }
        System.out.println(lnThree);
        for(int i = 1; i < s +1; i++){
            System.out.println(lnFour);
        }
        System.out.println(lnFive);
       
       runn = true;
       
       lnOne = "";
       lnTwo= "";
       lnThree= "";
       lnFour= "";
       lnFive= "";
       
    }else runn = false;
   
}
   

}
MichaelVasquez
 
Posts: 2
Joined: Tue Apr 13, 2010 8:46 pm

Re: P005 -- LCD Display

Postby DrJump » Fri Sep 17, 2010 7:57 pm

Good job MikeV ...

As far as how to keep the process running until "0 0" is entered ... how about using a loop around the code you have here. Something like this:

Code: Select all
do {
   ... // code for reading the "0 0"
   if (s == 0) {
      finished = true;
   } else {
      ... // all the rest of your code
   }
} while (!finished);


Dr Jump
DrJump
 
Posts: 19
Joined: Sun Mar 07, 2010 6:08 pm


Return to Problems

Who is online

Users browsing this forum: No registered users and 1 guest

cron