Solved

Need a program using permutations

Posted on 2004-10-26
188 Views
Last Modified: 2010-03-31
I need a program to convert a seven digit phone number eg(7777777) into its corresponding letters for all different permutations for those numbers.  If the number includes a 1 or 0 the output should return Invalid phone number.  For any confusion the numbers have to correspond to the letters on the number for the phone button eg(2-abc,3-def,.........).
The program must use certain methods, here is what it should look like,

public class WordsModel

public TelephoneWordsModel()  :class must initialize all of your instance variables. This must set the initial value of the
                                                phone number to an empty string.
                                 

public void setTelephonephoneNumber(String)
         Method, which will accept a single String as a parameter and set the value of the phone number to that String. This  
         method should NOT change the value of the phone number if it is provided a null value.

public String getTelephoneWords()
         Method, which will return either an error String or all of the permutations of the letters representing the phone
         number as a single String.

public static void main(String[] args)
         Main method which should contain your test code to show you have tested your model. This will prove your code has
         been tested and is a REQUIRED method.

Also the program should include a GUI to implement the following public methods:

Your program must create the class TelephoneWordsGUI and implement the following public methods:

publicTelephoneWordsGUI()
         Null constructor, which must initialize all of your instance variables. This must create the GUI and set it up.
public void actionPerformed (ActionEvent)
        This method is required to handle the actions generated from the button.
public void main(String[])
        This method must be exactly as given below:

  public static void main (String args[]) {
    TelephoneWordsGUI myGUI = new TelephoneWordsGUI();
    myGUI.show();
  }

No other public methods can be used in the program, all others must be private.

Thanks for any and all help! :)



0
Question by:DreamWeaver777
    6 Comments
     
    LVL 18

    Expert Comment

    by:bobbit31
    how bout you help us help you by doing some of this yourself so we can guide you along... this is obviously a homework question and it is against the membership agreement to provide a full solution to this type of question
    0
     

    Author Comment

    by:DreamWeaver777
    oh my fault, i do have this i just not sure how to include it into each of my methods for my model;

    import java.util.*;

    public class test {    
         static String phonenumber="2334567";
         public static void main(String[] args){
              int i=transferr();
         }
         private static int transferr(){
              ArrayList a1=new ArrayList();
              ArrayList a2=new ArrayList();
              a2.add("a");
              a2.add("b");
              a2.add("c");
              ArrayList a3=new ArrayList();
              a3.add("d");
              a3.add("e");
              a3.add("f");
              ArrayList a4=new ArrayList();
              a4.add("g");
              a4.add("h");
              a4.add("i");
              ArrayList a5=new ArrayList();
              a5.add("j");
              a5.add("k");
              a5.add("l");
              ArrayList a6=new ArrayList();
              a6.add("m");
              a6.add("n");
              a6.add("o");
              ArrayList a7=new ArrayList();
              a7.add("p");
              a7.add("q");
              a7.add("r");
              a7.add("s");
              ArrayList a8=new ArrayList();
              a8.add("t");
              a8.add("u");
              a8.add("v");
              ArrayList a9=new ArrayList();
              a9.add("w");
              a9.add("x");
              a9.add("y");
              a9.add("z");
              ArrayList a0=new ArrayList();    

              ArrayList b=new ArrayList();
             
              for(int i=0;i<phonenumber.length();i++){
                   char c=phonenumber.charAt(i);
                   switch(c){
                        case '1':
                             System.out.println("Invalid number");
                             return -1;                                                  
                        case '2':
                             b.add(a2);
                             break;
                        case '3':
                             b.add(a3);
                             break;
                        case '4':
                             b.add(a4);
                             break;
                        case '5':
                             b.add(a5);
                             break;
                        case '6':
                             b.add(a6);
                             break;
                        case '7':
                             b.add(a7);
                             break;
                        case '8':
                             b.add(a8);
                             break;
                        case '9':
                             b.add(a9);
                             break;
                        case '0':
                             System.out.println("Invalid number");
                             return -1;                                            
                   }              
              }    
             
              System.out.println(b);
              ArrayList result=new ArrayList();          
              ArrayList result1=new ArrayList();
              String s=new String();
              for(int m=0;m<b.size();m++){              
                   ArrayList thisresult=(ArrayList)b.get(m);
                   for(int n=0;n<thisresult.size();n++){                    
                        if(result.size()<thisresult.size()){
                             result.add(thisresult.get(n));
                        }else{          
                             int l=result.size();
                             for(int k=0;k<l;k++){
                                  s=(String)result.get(k);
                                  s=s+(String)thisresult.get(n);
                                  result.add(s);    
                             }                        
                        }                    
                   }              
                   for (int mm=0;mm<result.size();mm++){
                        if(((String)result.get(mm)).length()==m+1){                        
                             result1.add(result.get(mm));
                        }
                   }
                   result=new ArrayList(result1);
                   result1=new ArrayList();
              }
                   
              System.out.println(result);
              return 0;
         }
    }
    0
     
    LVL 21

    Assisted Solution

    by:MogalManic
    Your first step should be to conform to the interface described for the class WordsModel.  Start with just an empty class
    public WordsModel
    {
       public WordsModel()
       {
       }
    //Include all of the other functions here
    }

    Then put your initialization logic in the TelephoneWordsModel() method.
    0
     
    LVL 15

    Accepted Solution

    by:
    Arrays are nice for organizing such correspondences:

    char[][]  keyBoard  =  {  {},                                // key 0
                                        {},                                // key 1
                                        { 'a', 'b', 'c' },                  // key 2
                                        { 'd', 'e', 'f' },                  // key 3
                                        // etc for each key
                                    }

    with a datastructure like that the 'generate all permutes is easy

    char[]  charStack[]  =  new char[10];

    void permute( String  phoneNumber, int level ) {          // original call have level == 0
         if ( level >= phoneNumber.length() ) {
              // we have reached a complete permutation
              System.out.println ( charStack.toString() );
         } else {
              int key = Integer.valueOf( ""+phoneNumber.charAt(level) );     // get the digit value
              if ( keyBoard[key].length == 0 ) {
                   // illegal key. output error message
              } else {
                   for (int i=0; i<keyBoard[key].length; i++ ) {
                        charStack[level] = keyBoard{key][i];
                        permute(level+1);
                   }
              }
         }
    }

    //called by:
    permute( "2345678", 0 );

    regards JakobA
    0
     

    Author Comment

    by:DreamWeaver777
    Thanks for the feedback Mogal and JakobA!
    0
     
    LVL 15

    Expert Comment

    by:JakobA
    thanks for points.

    PS. I am pretty certain that this is not the way those 'just press the digits' word-recognizers work in the new mobiles. For one thing it generates 3^7 strings of each 7 characters. That takes some 25 kilobytes just to store them. and we generate them all before we even make the first comparison. wastefull.
      Much better to do a 'refine the list' incremental searc in the phones list of 'known words', ie: start with a list of all known words, and when user presses the '2' remove from the list all that do not start with a, b or c.  That way the processing time is hidden in the users fingertime. No visible delays at all.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    845 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    5 Experts available now in Live!

    Get 1:1 Help Now