Difference between revisions of "Cellphone Texting"
(Added quotations to student quotes) |
|||
(12 intermediate revisions by one other user not shown) | |||
Line 3: | Line 3: | ||
|ProblemName=Cellphone Texting | |ProblemName=Cellphone Texting | ||
− | |Problem=In non-smart cell phones, text input can be handled by a | + | |Problem=(FROM COMSCI CONTEST 2010)<BR>In non-smart cell phones, text input can be handled by a |
technique called multi-tap(example picture: [http://www.keypadletters.com/ link here]). With multi-tap, you use the | technique called multi-tap(example picture: [http://www.keypadletters.com/ link here]). With multi-tap, you use the | ||
letters on each key, and press a key multiple times to | letters on each key, and press a key multiple times to | ||
Line 14: | Line 14: | ||
The letters 'q' and 'z', not originally part of the letters on a | The letters 'q' and 'z', not originally part of the letters on a | ||
phone, force the keys 7 and 9 to have four characters each. All other keys have 3 | phone, force the keys 7 and 9 to have four characters each. All other keys have 3 | ||
− | characters | + | characters. |
+ | <br> | ||
+ | Write a program which, given a word (a string of upper case letters with no | ||
+ | spaces), outputs the sequence of keystrokes using multi-tap. If a key is pressed | ||
+ | multiple times, it should appear that many times in the input. A pause is indicated | ||
+ | by a single space. | ||
+ | <br> | ||
+ | Examples: | ||
+ | {{OutputBlock | ||
+ | |Code= | ||
+ | Enter code: 222666 67777222444 | ||
+ | Translated Code: COMSCI | ||
+ | Enter code: 7 2 777 2 7777 444 8 444 222 | ||
+ | Translated Code: PARASITIC | ||
+ | }} | ||
− | |||
− | |||
− | |||
− | |||
<br> | <br> | ||
This problem will cover the following topics: | This problem will cover the following topics: | ||
*[[Control_Structures|If and nested if statements]] | *[[Control_Structures|If and nested if statements]] | ||
− | *[[ | + | *[[Processing_Arrays_(using for loops)|Arrays]] |
*[[Input using Scanner|Scanner]] | *[[Input using Scanner|Scanner]] | ||
*[[Output_using_System.out.|System.out]] | *[[Output_using_System.out.|System.out]] | ||
− | + | *[[Arithmetic_Operators|Modulus]] | |
− | + | *[[While Loops| While loops]] | |
− | + | ||
− | + | ||
− | |SideSectionTitle= | + | |SideSectionTitle=...By Students |
− | |SideSection= | + | |SideSection="COMP 1010 is where I first learned how to make a program. I was only taking that course for prerequisite and do not have interest on it, but as time goes by, I notice that I was enjoying myself making a program. |
+ | I learned that commenting important parts of the code really helps a lot especially to myself since I can track down why I did that code. Also, if you want to check or want to debug your code, try commenting out the parts you don't need yet and try to output some parts of the code to see what your code is doing. And the last but not the least. Don't do all the coding in one shot, it will take you more time to debug it than running the program while the coding is still small." | ||
<BR> | <BR> | ||
Line 47: | Line 57: | ||
− | + | Next is to think all the step necessary for the program to work.<br> | |
− | {{ | + | *Get user input |
− | + | *Check if the input are all numbers or numbers with spaces | |
− | + | *Process the input and change them to letters | |
− | + | {{note}}This solution will show 3 method to do all these steps to make the program look neat. You can always put all codes in main but it will be messy<br> | |
− | + | ||
− | }} | + | |
− | + | First to get the Input of the user.<br> | |
+ | In order to get the user input, you have to use the Scanner's method .nextLine() in order to get the whole input. Since you do not know the when user will stop, you have to put the scanner inside a while loop. You also want to check validation of the input but let another method handle the validation. If input is not valid, output an error message to alert the user. | ||
+ | |||
{{CodeBlock | {{CodeBlock | ||
|Code= | |Code= | ||
− | + | Scanner scan = new Scanner(System.in); // To enter the input | |
− | + | String input = " "; // Store the input | |
− | Scanner | + | String convert = ""; // Store the translated input |
− | + | System.out.print("Enter code: "); | |
− | // | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | while(!input.equals("")) | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
{ | { | ||
− | + | input = scan.nextLine(); // scan will get the user input and store in in the String input | |
− | + | if(checkInput(input)) // calls the method checkInput to check for validation | |
− | if(input | + | |
{ | { | ||
− | + | convert = codeEncription(input); //calls the method codeEcription to translate the input into letters | |
− | // | + | System.out.print("Translated Code: "+convert); //output message |
− | + | System.out.print("\nEnter code: "); | |
− | + | } | |
else | else | ||
{ | { | ||
− | System.out.println(" | + | System.out.println("Invalid Input"); //error message |
+ | System.out.print("Enter code: "); | ||
} | } | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
}} | }} | ||
{{OutputBlock | {{OutputBlock | ||
|Code= | |Code= | ||
− | + | Enter code: 23a43 | |
− | + | Invalid Input | |
− | Invalid | + | Enter code: 77 88 444 9999 9999 444 222 2 555 |
− | + | Translated Code: QUIZZICAL | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
}} | }} | ||
− | + | Next is to make a method that will validate the user input.It will have a String as a parameter to pass the input to this method<br> | |
− | + | You have to check each character in the input to see if there is an invalid input. While loops is still necessary for this task since you don't know if you will find an error or not in the input. This method will return true if its valid or false if invalid. | |
− | + | ||
{{CodeBlock | {{CodeBlock | ||
|Code= | |Code= | ||
− | + | public static boolean checkInput(String input) | |
− | + | { | |
− | + | boolean check = true; | |
− | + | int counter = 0; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | while( check && counter < input.length()) | ||
+ | { | ||
+ | if(input.charAt(counter) == ' ') //if space is found in the input its still valid | ||
+ | counter++; | ||
+ | else if(input.charAt(counter) < '0' {{!}}{{!}} input.charAt(counter) > '9') //check if its a number or not | ||
+ | check = false; | ||
+ | else | ||
+ | counter++; | ||
+ | } | ||
− | + | return check; //returns true if valid or false if not valid | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
}} | }} | ||
− | + | Next step is to make a method that will translate the input.It has String as a parameter as well.<br> | |
+ | In order to translate the input, you have to check each character in the String. This means you can use for loops in this method since you will know went exactly to stop. Every time you check a character, store the current character and count how many are there after the current character change. Then pass the current character and the number of count you had to another method what will change the number into Letters | ||
{{CodeBlock | {{CodeBlock | ||
|Code= | |Code= | ||
− | + | public static String codeEncription(String input) | |
{ | { | ||
− | + | String output = ""; //Store each number that is translated into letter | |
− | + | int counter = 1; //count the number of same consecutive number | |
− | + | char last = input.charAt(0); //Initialize the last to the first character of the string | |
− | { | + | char cur = input.charAt(0); //Initialize the cur to the first character of the string |
− | + | for(int i = 1 ; i <= input.length();i++) | |
− | + | { | |
− | + | if(i < input.length()) | |
− | + | { | |
− | + | cur = input.charAt(i); //store the current character to cur | |
+ | } | ||
+ | else | ||
+ | { | ||
+ | cur = '.'; //this else statement is necessary to complete the last letter of the input | ||
+ | } | ||
+ | if(cur == last) | ||
+ | { | ||
+ | counter++; //number of same consecutive number | ||
+ | } | ||
+ | else | ||
+ | { | ||
− | + | output += converter(last,counter) +""; //this will call converter() method that will change the number into letter | |
− | + | last = cur; //to set the last into the current character | |
− | + | counter = 1; | |
− | + | } | |
− | + | } | |
− | + | return output; // return the translated code | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
}} | }} | ||
− | + | Last step is to make a method that will convert the number into letter using String array and Modulus.<br> | |
+ | In this method, there are two parameter ,char and int, the char represent the number and the in represent how many consecutive number was found in the current search from the other method. This will return the equivalent letter or if its space it will return empty String. | ||
{{CodeBlock | {{CodeBlock | ||
|Code= | |Code= | ||
− | + | public static String converter(char find,int count) | |
− | + | ||
{ | { | ||
− | + | //String array are manually entered. The arrangement of letters is necessary in order to get the right letter | |
+ | String[] two = {"C","A","B"}; | ||
+ | String[] three = {"F","E","D"}; | ||
+ | String[] four = {"I","G","H"}; | ||
+ | String[] five = {"L","J","K"}; | ||
+ | String[] six = {"O","M","N"}; | ||
+ | String[] seven = {"S","P","Q","R"}; | ||
+ | String[] eight = {"V","T","U"}; | ||
+ | String[] nine = {"Z","W","X","Y"}; | ||
+ | String convert = ""; | ||
− | + | //doing a modulus will return either 1,2 or 0 if its mod 3 and 1,2,3 or 0 if its mod 4 | |
− | if ( | + | if(find == '2'){ |
− | + | convert = two[(count%3)]; | |
− | else | + | }else if(find == '3') |
− | + | { | |
− | } | + | convert = three[(count%3)]; |
− | } | + | }else if(find == '4') |
− | { | + | { |
− | + | convert = four[(count%3)]; | |
− | + | }else if(find == '5') | |
− | + | { | |
− | + | convert = five[(count%3)]; | |
− | } | + | }else if(find == '6') |
− | { | + | { |
− | + | convert = six[(count%3)]; | |
− | + | }else if(find == '7') | |
− | + | { | |
− | + | convert = seven[(count%4)]; | |
− | + | }else if(find == '8') | |
+ | { | ||
+ | convert = eight[(count%3)]; | ||
+ | }else if(find == '9') | ||
+ | { | ||
+ | convert = nine[(count%4)]; | ||
+ | } | ||
− | + | return convert; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
}} | }} | ||
− | + | This program will helps you how to make a clean code and make sure you make it simple as possible. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | This | + | |
|SolutionCode= | |SolutionCode= | ||
+ | import java.util.Scanner; | ||
import javax.swing.*; | import javax.swing.*; | ||
− | |||
− | public class | + | public class CellText |
{ | { | ||
− | + | public static void main(String[] args) | |
− | + | { | |
− | + | Scanner scan = new Scanner(System.in); // To enter the input | |
+ | String input = " "; // Store the input | ||
+ | String convert = ""; // Store the translated input | ||
+ | System.out.print("Enter code: "); | ||
− | + | while(!input.equals("")) | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
{ | { | ||
− | + | input = scan.nextLine(); // scan will get the user input and store in in the String input | |
− | + | if(checkInput(input)) // calls the method checkInput to check for validation | |
− | if(input | + | |
{ | { | ||
− | + | convert = codeEncription(input); //calls the method codeEcription to translate the input into letters | |
− | + | System.out.print("Translated Code: "+convert); //output message | |
− | + | System.out.print("\nEnter code: "); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
− | + | else | |
− | + | { | |
− | + | System.out.println("Invalid Input"); //error message | |
− | + | System.out.print("Enter code: "); | |
− | + | } | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | } | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | } | |
− | + | ||
− | + | public static boolean checkInput(String input) | |
− | + | { | |
− | + | boolean check = true; | |
+ | int counter = 0; | ||
+ | |||
+ | while( check && counter < input.length()) | ||
+ | { | ||
+ | if(input.charAt(counter) == ' ') //if space is found in the input its still valid | ||
+ | counter++; | ||
+ | else if(input.charAt(counter) < '0' {{!}}{{!}} input.charAt(counter) > '9') //check if its a number or not | ||
+ | check = false; | ||
+ | else | ||
+ | counter++; | ||
+ | } | ||
+ | |||
+ | return check; //returns true if valid or false if not valid | ||
+ | } | ||
+ | |||
+ | public static String codeEncription(String input) | ||
+ | { | ||
+ | String output = ""; //Store each number that is translated into letter | ||
+ | int counter = 1; //count the number of same consecutive number | ||
+ | char last = input.charAt(0); //Initialize the last to the first character of the string | ||
+ | char cur = input.charAt(0); //Initialize the cur to the first character of the string | ||
+ | for(int i = 1 ; i <= input.length();i++) | ||
+ | { | ||
+ | if(i < input.length()) | ||
+ | { | ||
+ | cur = input.charAt(i); //store the current character to cur | ||
} | } | ||
else | else | ||
{ | { | ||
− | + | cur = '.'; //this else statement is necessary to complete the last letter of the input | |
+ | } | ||
+ | |||
+ | if(cur == last) | ||
+ | { | ||
+ | counter++; //number of same consecutive number | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | |||
+ | output += converter(last,counter) +""; //this will call converter() method that will change the number into letter | ||
+ | last = cur; //to set the last into the current character | ||
+ | counter = 1; | ||
} | } | ||
} | } | ||
− | else | + | return output; // return the translated code |
+ | } | ||
+ | |||
+ | |||
+ | public static String converter(char find,int count) | ||
+ | { | ||
+ | //String array are manually entered. The arrangement of letters is necessary in order to get the right letter | ||
+ | String[] two = {"C","A","B"}; | ||
+ | String[] three = {"F","E","D"}; | ||
+ | String[] four = {"I","G","H"}; | ||
+ | String[] five = {"L","J","K"}; | ||
+ | String[] six = {"O","M","N"}; | ||
+ | String[] seven = {"S","P","Q","R"}; | ||
+ | String[] eight = {"V","T","U"}; | ||
+ | String[] nine = {"Z","W","X","Y"}; | ||
+ | String convert = ""; | ||
+ | |||
+ | //doing a modulus will return either 1,2 or 0 if its mod 3 and 1,2,3 or 0 if its mod 4 | ||
+ | if(find == '2'){ | ||
+ | convert = two[(count%3)]; | ||
+ | }else if(find == '3') | ||
{ | { | ||
− | + | convert = three[(count%3)]; | |
+ | }else if(find == '4') | ||
+ | { | ||
+ | convert = four[(count%3)]; | ||
+ | }else if(find == '5') | ||
+ | { | ||
+ | convert = five[(count%3)]; | ||
+ | }else if(find == '6') | ||
+ | { | ||
+ | convert = six[(count%3)]; | ||
+ | }else if(find == '7') | ||
+ | { | ||
+ | convert = seven[(count%4)]; | ||
+ | }else if(find == '8') | ||
+ | { | ||
+ | convert = eight[(count%3)]; | ||
+ | }else if(find == '9') | ||
+ | { | ||
+ | convert = nine[(count%4)]; | ||
} | } | ||
+ | |||
+ | return convert; | ||
} | } | ||
} | } | ||
}} | }} |
Latest revision as of 15:33, 8 December 2011
Back to the Program-A-Day homepage
Problem(FROM COMSCI CONTEST 2010) Enter code: 222666 67777222444 Translated Code: COMSCI Enter code: 7 2 777 2 7777 444 8 444 222 Translated Code: PARASITIC
|
...By Students"COMP 1010 is where I first learned how to make a program. I was only taking that course for prerequisite and do not have interest on it, but as time goes by, I notice that I was enjoying myself making a program. I learned that commenting important parts of the code really helps a lot especially to myself since I can track down why I did that code. Also, if you want to check or want to debug your code, try commenting out the parts you don't need yet and try to output some parts of the code to see what your code is doing. And the last but not the least. Don't do all the coding in one shot, it will take you more time to debug it than running the program while the coding is still small."
| |
---|---|---|
SolutionStart by importing the swing java package. import javax.swing.*; import java.util.Scanner;
Scanner scan = new Scanner(System.in); // To enter the input String input = " "; // Store the input String convert = ""; // Store the translated input System.out.print("Enter code: "); while(!input.equals("")) { input = scan.nextLine(); // scan will get the user input and store in in the String input if(checkInput(input)) // calls the method checkInput to check for validation { convert = codeEncription(input); //calls the method codeEcription to translate the input into letters System.out.print("Translated Code: "+convert); //output message System.out.print("\nEnter code: "); } else { System.out.println("Invalid Input"); //error message System.out.print("Enter code: "); } } Enter code: 23a43 Invalid Input Enter code: 77 88 444 9999 9999 444 222 2 555 Translated Code: QUIZZICAL
public static boolean checkInput(String input) { boolean check = true; int counter = 0; while( check && counter < input.length()) { if(input.charAt(counter) == ' ') //if space is found in the input its still valid counter++; else if(input.charAt(counter) < '0' || input.charAt(counter) > '9') //check if its a number or not check = false; else counter++; } return check; //returns true if valid or false if not valid }
public static String codeEncription(String input) { String output = ""; //Store each number that is translated into letter int counter = 1; //count the number of same consecutive number char last = input.charAt(0); //Initialize the last to the first character of the string char cur = input.charAt(0); //Initialize the cur to the first character of the string for(int i = 1 ; i <= input.length();i++) { if(i < input.length()) { cur = input.charAt(i); //store the current character to cur } else { cur = '.'; //this else statement is necessary to complete the last letter of the input } if(cur == last) { counter++; //number of same consecutive number } else { output += converter(last,counter) +""; //this will call converter() method that will change the number into letter last = cur; //to set the last into the current character counter = 1; } } return output; // return the translated code }
public static String converter(char find,int count) { //String array are manually entered. The arrangement of letters is necessary in order to get the right letter String[] two = {"C","A","B"}; String[] three = {"F","E","D"}; String[] four = {"I","G","H"}; String[] five = {"L","J","K"}; String[] six = {"O","M","N"}; String[] seven = {"S","P","Q","R"}; String[] eight = {"V","T","U"}; String[] nine = {"Z","W","X","Y"}; String convert = ""; //doing a modulus will return either 1,2 or 0 if its mod 3 and 1,2,3 or 0 if its mod 4 if(find == '2'){ convert = two[(count%3)]; }else if(find == '3') { convert = three[(count%3)]; }else if(find == '4') { convert = four[(count%3)]; }else if(find == '5') { convert = five[(count%3)]; }else if(find == '6') { convert = six[(count%3)]; }else if(find == '7') { convert = seven[(count%4)]; }else if(find == '8') { convert = eight[(count%3)]; }else if(find == '9') { convert = nine[(count%4)]; } return convert; }
| ||
Code |