Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes here is a question and i have a very similar code but i need to convert it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "here is a question and i have a very similar code but i need to convert it" Watch "here is a question and i have a very similar code but i need to convert it" New topic
Author

here is a question and i have a very similar code but i need to convert it

alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
here is a question and i have a very similar code but i need to convert it
some how in order to fit this question

question:
In order to make their phone numbers more memorable, service providers like to find numbers that spell out some word (called a mnemonic) appropriate to their business that makes that phone number easier to remember. For example, the phone number for a recorded time-of-day message in some localities is 637-8687 (NERVOUS). Imagine that you have just been hired by a local telephone company to write a method listMnemonics that will generate all possible letter combinations that correspond to a given number, represented as a string of digits. For example, if you call
recursiveObject.listMnemonics("723")
your method shall generate and print out (using recursion) the following 27 possible letter combinations that correspond to that prefix:
PAD PBD PCD RAD RBD RCD SAD SBD SCD
PAE PBE PCE RAE RBE RCE SAE SBE SCE
PAF PBF PCF RAF RBF RCF SAF SBF SCF

my efforts in solving it:
i know that in order to solve this question is need to use a recursive methos that
prints out all the subsets of a given string letters with a given resolt string size

i have built this code

[ March 18, 2008: Message edited by: donaldth smithts ]
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
anyone??
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3415
    
  12
It may just be me, but i can't see how the code you've posted is in any way related to the question.

But the more important question is - why is NERVOUS a good mnemonic for a recorded time-of-day message service


Joanne
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
it is related because it prints all the combinations of the chars
in some given length

this recurtion is as far as i can go
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
for one number for example 2=d e f 1=a b c

then it will print out
a
b
c

if (n==1){
for (i=0;i<3;i++){
System.out.println(phone(num_string,n)+str.charAt[i]);
}

if we have n+1 digits then it will print 3 conbinations
where the first part is the n length string
and the second part is the chars inside the last number

for example if the last number is 2 then it will print
string_n_long+ d
string_n_long+ e
string_n_long+ f



can you give me a tip on what to do next
in order to have a working code??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

Originally posted by Joanne Neal:
It may just be me, but i can't see how the code you've posted is in any way related to the question.



I agree. Beside both being solved by recursion, the two solutions are not very similar.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
neither one of them is working
i this solution i am going a different
way
can you help me finish it??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

Originally posted by donaldth smithts:
neither one of them is working
i this solution i am going a different
way
can you help me finish it??


Well, where are you stuck? And what are you currently trying to do -- and failing?

We can't give you a hint in the right direction, if we don't know what you are trying to do. And throwing out code with "it doesn't work", doesn't give an idea what what you are trying to do.

Henry
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
i tried to think of the simplest case of this problem
when i have only one digit

and in the case of that i have all the combinations for n character
and we want to complete it to make n+1 length characters

as i show further my code
it aimes there but i need hep with explaining
wether i did it rihjt

for one number for example 2=d e f 1=a b c

then it will print out
a
b
c

if (n==1){
for (i=0;i<3;i++){
System.out.println(phone(num_string,n)+str.charAt[i]);
}

if we have n+1 digits then it will print 3 conbinations
where the first part is the n length string
and the second part is the chars inside the last number

for example if the last number is 2 then it will print
string_n_long+ d
string_n_long+ e
string_n_long+ f



can you give me a tip on what to do next
in order to have a working code??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

can you give me a tip on what to do next
in order to have a working code??


Post the complete code for this one simple case -- and we'll help you convert that method to a more general case. Just make sure that it compiles and runs first.

Henry
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
this is the simplest case of only one digit
what to do next??

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

this is the simplest case of only one digit
what to do next??


Well, first you need to modify the code to *not* print, but to generate the answer, since you can't print until you have the complete answer. (but I am getting ahead of myself -- you have to understand the hint first)

You have to think recursively (since this is a recursive homework problem). If the string is one character, the answer is three different answers, you are done and can print.


Hint: However, if the string is not one character, then you have three different answers for the first character that you pull off, but you need to deal with the rest of the string (minus the first character). How would you do that?

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

BTW, have you even tried your simpliest case with one digit? If you ran it, you will notice that you will get an exception.

Before you move on to generalizing the solution, I would recommend that you get the one-digit case working first.

Henry
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
here is my fixes base "one digit" case code
i know that this recurstion for each additional digit adds 3 new
posibilities where in each one a different digit is added from the group
of the last digit
but there is a printout part
where do i store all these endless strings??

here is my base case(tested)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40


but there is a printout part
where do i store all these endless strings??


Well, what is the method was passed the "printout" so far -- the printout for the one recursive route? Then when it is time to print, you can print the printout so far, along with the last case. If it isn't the last digit, then you can add to the printout so far...

Henry
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
i know that our recursive line needed too look something like

for (i=0;i<3;i++){
phone(n-1)+phone[n][i];
}
that as far as i can go

helpppppppppppppp

[ March 22, 2008: Message edited by: donaldth smithts ]
[ March 23, 2008: Message edited by: donaldth smithts ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40



Well, that's your problem. The solution looks nothing like what you think. Just because it looks recursive doesn't mean that it's the right answer.

Hint #1: You need to think about how to get to the solution instead of how to make it recursive.

In this case, you are saying the recursive solution should take a digit and do something related to one less than the digit. Does that even make sense? Isn't it more related to another number in the phone number?

Hint #2: Did you pay attention to my last post? What if you keep track of what you "print so far"? Don't print it, but build it as a parameter, until it is ready to be printed?

Henry
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
well how do i know its ready
how do i prinout this
as at ones or part by part
how do i need to print it??

i can guess that we need some counter that its starting value
represents original length of our phone number
and that it diminishes till 1
and when its one
we start printing
thats a lot of nice thought but i dont know what
to put in there
[ March 23, 2008: Message edited by: donaldth smithts ]
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
here i have a solution but i cant understand it
can any one help me interpritate this code??

i was told that
For example, if each telephone digit corresponds to 3 letters, then the first time you call ListMnemonics("1234567", "") it calls itself 3 times, each one of those calls will call itself 3 times, which will call itself 3 times again and so on 7 times (seven being the length of the telephone number 1234567).

Each of the function calls checks to see if the mnemonic it received is of length 7, if it is that mnemonic is complete, if it's not then it calls itself 3 times appending a new character to the mnemonic.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

here i have a solution but i cant understand it
can any one help me interpritate this code??


Well, did you do this assignment? Recursion is not just a pattern. It can be argued that it is a way of thinking out a solution. You can't learn it (thoroughly) by looking at a solution.

If you wrote it, you should understand it. And you should have a very cool technique in your programming arsenal. If you didn't do this assignment, then you just cheated yourself of a very powerful technique.

Henry
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
no i didnt do it
and this is a solution some one gave me

can you tell me whats the technique
this is not an assigment this is a question i am trying to solve
for the test

from the step of creating the basic case i didnt know how to got further

can you explain to me this technique??
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
i tried to biuld the method i was told to implement in the solution code
but i get out of bounds exception
i cant understant why
???

Karthik Krishnamurthy
Ranch Hand

Joined: Feb 04, 2005
Posts: 118
Your GetNumbers.... method takes an int while you are passing a character in String letters=.....
The int value of '4' is 52, so you are trying to access array[52][0] etc. and hence getting indexoutofbounds

Please change that. Use logging or printlns to debug.

Also look at your naming conventions too with respect to method naming.


Karthik
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
i cant understand how do i get 52
i passed th decoder mode 10 times
how does the value of num gets to be 52
??
Karthik Krishnamurthy
Ranch Hand

Joined: Feb 04, 2005
Posts: 118
You have :
String letters = GetLettersCorrespondingToNumber(phoneNum.charAt(mnem.length()));

The value in phoneNum is 4443220 based on ListMnemonics("4443220","")

mnem is "" and mnem.length therefore is 0.
phoneNum.charAt(0) is '4'. But it gets passed to an "int" - int value of character '4' is the value 52

Karthik
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
so how can you suggest me to change the code
in order to overcome this problem??
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: here is a question and i have a very similar code but i need to convert it
 
Similar Threads
i want to try and solve this question..
you are my last resort for this question
Recursion of Fibonacci Numbers
Problem from the book
i want to try and solve this question..