Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need a program using permutations

Posted on 2004-10-26
6
Medium Priority
?
190 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
Comment
Question by:DreamWeaver777
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 12414173
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
ID: 12414400
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
MogalManic earned 450 total points
ID: 12417783
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Accepted Solution

by:
JakobA earned 1050 total points
ID: 12417830
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
ID: 12426960
Thanks for the feedback Mogal and JakobA!
0
 
LVL 15

Expert Comment

by:JakobA
ID: 12427572
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

610 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