Advertisement
Guest User

Untitled

a guest
Oct 26th, 2013
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.69 KB | None | 0 0
  1. //Program to spell positive 3-digit numberss
  2. //
  3. import java.util.*;
  4. import javax.swing.*;
  5. public class Assignment4
  6. {
  7.    public static void main(String[] args)
  8.    {
  9.       String input;  //input buffer
  10.       boolean anotherInput = true; //repeat flag
  11.      
  12.       while ( anotherInput )
  13.       {
  14.          //User enters a 3-digit number, or Cancel to quit
  15.          input = JOptionPane.showInputDialog(null, "Enter a Positive Number, 1 ... 999" +
  16.                                                    "\nOR click on \"Cancel\" to quit" );
  17.          if (input == null)   //User hit "Cancel"
  18.             anotherInput = false;
  19.          
  20.          else                 //Number entered
  21.          {
  22.             //Parse input as an integer
  23.             int positive = Integer.parseInt( input );
  24.            
  25.             //Reject integers that are non-positive, or more than 3 digits
  26.             if (positive <= 0 || positive > 999)
  27.                throw new RuntimeException("Invalid input: " + positive);
  28.            
  29.             //Display the spelling (in words) of the input integer  
  30.             String spelling = threeDigitSpelling( positive );
  31.            
  32.             JOptionPane.showMessageDialog(null, positive + "  " + spelling);
  33.          }
  34.       }
  35.    }
  36.      
  37.    //Return the spelling (in words) of any positive number up to 3 digits
  38.    //Parameter number : any positive whole number 1 ... 999
  39.    private static String threeDigitSpelling(int number)
  40.    {
  41.       //Obtain the individual digits of the number  
  42.       int hundreds = decimalDigit(2, number);
  43.       int tens = decimalDigit(1, number);
  44.       int ones = decimalDigit(0, number);
  45.      
  46.       //The spelling that will be returned
  47.       String words = "";
  48.      
  49.       //Intrepret the hundreds digit to extend the spelling
  50.    switch (hundreds)
  51.       {
  52.          case 1: words = "one hundred";
  53.          break;
  54.          case 2: words = "two hundred";
  55.          break;
  56.          case 3: words = "three hundred";
  57.          break;
  58.          case 4: words = "four hundred";
  59.          break;
  60.          case 5: words = "five hundred";
  61.          break;
  62.          case 6: words = "six hundred";
  63.              break;
  64.          case 7: words = "seven hundred";
  65.              break;
  66.          case 8: words = "eight hundred";
  67.              break;
  68.          case 9: words = "nine hundred";
  69.              break;
  70.      
  71.    }
  72.       //Interpret the tens digit to extend the spelling.
  73.       //There are three different cases:
  74.       //    a) tens > 1, b) tens == 1, c) tens == 0
  75.       if (tens !=1){
  76.            switch (tens){
  77.                case 2: words = "twenty";
  78.                    break;
  79.                case 3: words = "thirty";
  80.                    break;
  81.                case 4: words = "forty";
  82.                    break;
  83.                case 5: words = "fifty";
  84.                    break;
  85.                case 6: words = "sixty";
  86.                    break;
  87.                case 7: words = "seventy";
  88.                    break;
  89.                case 8: words = "eighty";
  90.                    break;
  91.                case 9: words = "ninety";
  92.                    break;
  93.                case 0: words = "";
  94.                    break;
  95.            }
  96.       }
  97.        else if (tens==1){
  98.            switch (ones){
  99.                case 0: words = "ten";
  100.                    break;
  101.                case 1: words = "eleven";
  102.                    break;
  103.                case 2: words = "twelve";
  104.                    break;
  105.                case 3: words = "thirteen";
  106.                    break;
  107.                case 4: words = "fourteen";
  108.                    break;
  109.                case 5: words = "fifteen";
  110.                    break;
  111.                case 6: words = "sixteen";
  112.                    break;
  113.                case 7: words = "seventeen";
  114.                    break;
  115.                case 8: words = "eighteen";
  116.                    break;
  117.                case 9: words = "nineteen";
  118.                    break;
  119.            }
  120.    }
  121.    
  122.            
  123.          
  124.       //Interpret the ones digit to extend the spelling
  125.       switch (ones)
  126.       {
  127.          case 1: words = "one";
  128.          break;
  129.          case 2: words = "two";
  130.          break;
  131.          case 3: words = "three";
  132.          break;
  133.          case 4: words = "four";
  134.          break;
  135.          case 5: words = "five";
  136.          break;
  137.          case 6: words = "six";
  138.              break;
  139.          case 7: words = "seven";
  140.              break;
  141.          case 8: words = "eight";
  142.              break;
  143.          case 9: words = "nine";
  144.              break;
  145.          case 0: words = "";
  146.              break;
  147.       }
  148.    
  149.       //Return the spelling  
  150.       String[] verse = {"Hickory Dickory Dock!",  
  151.                         "The Mouse Ran Up The Clock!",  
  152.                         "The Clock Struck One!",  
  153.                         "The Mouse Ran Down!",  
  154.                         "Hickory Dickory Dock!"    };    
  155.       return verse[ (new Random()).nextInt(verse.length) ];
  156.  
  157.    
  158.    }
  159.    
  160.    //Helper method: Returns the ones-word spelling of a single digit
  161.    // Zero, One, Two, ..., Nine
  162.    //Parameter digit : 0 ... 9, identifies the desired ones-word  
  163.    private static String onesWord(int digit)
  164.    {switch ( digit )
  165. {
  166. case 0: return "zero";
  167. case 1: return "one";
  168. case 2: return "two";
  169. case 3: return "three";
  170. case 4: return "four";
  171. case 5: return "five";
  172. case 6: return "six";
  173. case 7: return "seven";
  174. case 8: return "eight";
  175. case 9: return "nine";
  176. default: throw new RuntimeException("Non-Digit " + digit);
  177. }
  178.      
  179.    }
  180.  
  181.    
  182.    //Helper method: Return the teen-word spelling of a single digit
  183.    // Ten, Eleven, Twelve, ..., Nineteen
  184.    //Parameter digit : 0 ... 9, identifies the desired teen-word
  185.    private static String teenWord(int digit)
  186.    {
  187.       return "" + digit;  
  188.    }
  189.  
  190.    
  191.    //Helper method: Return the tens-word spelling of a single digit
  192.    // Zero, Ten, Twenty, ..., Ninety
  193.    //Parameter digit : 0 ... 9, identifies the desired tens-word  
  194.    private static String tensWord(int digit)
  195.    {
  196.       return "tens" + digit;  
  197.    }
  198.  
  199.    //Helper method: Return a digit of any positive integer
  200.    //Parameter index  : indexes the desired digit, 0 = least significant
  201.    //Parameter number : the integer whose digit is required
  202.    //Example: decimalDigit(3, 57419) returns 7
  203.    private static int decimalDigit(int index, int number)
  204.    {
  205.            
  206.       //ALGORITHM:
  207.       //Step 1: Divide number by 10, index times
  208.       //Step 2: Find the remainder of number from Step 1 with 10
  209.       //Step 3: Return the remainder from Step 2
  210.       switch ( index )
  211. {
  212. case 2: number /= 10;
  213. case 1: number /= 10;
  214. case 0: return number % 10;
  215. default: return 0;
  216.    
  217.       }
  218.    }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement