Difference between revisions of "Cellphone Texting"
Line 15: | Line 15: | ||
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 | 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 | 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 | multiple times, it should appear that many times in the input. A pause is indicated | ||
by a single space. | by a single space. | ||
− | + | <br> | |
Examples: | Examples: | ||
{{OutputBlock | {{OutputBlock | ||
Line 33: | Line 33: | ||
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=By Students... | |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. |
− | + | ||
<BR> | <BR> | ||
Line 57: | Line 55: | ||
− | + | 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 4 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 | |
− | + | 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); | ||
+ | System.out.print("\nEnter code: "); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | System.out.println("Invalid Input"); | ||
+ | System.out.print("Enter code: "); | ||
+ | } | ||
+ | |||
+ | } | ||
}} | }} | ||
− | Next define your local variables and initialize them to sentinel values for future error checking described later. | + | Next define your local variables and initialize them to sentinel values for future error checking described later. You will need a year, monthNum and monthName as defined accordingly. |
{{CodeBlock | {{CodeBlock | ||
|Code= | |Code= | ||
Line 103: | Line 129: | ||
else | else | ||
{ | { | ||
− | System.out.println("Invalid month number entered, choose | + | System.out.println("Invalid month number entered, choose betyouen 1 - 12 (Jan - Dec)"); |
} | } | ||
}} | }} | ||
Line 120: | Line 146: | ||
− | Now | + | Now you have to break up the problem into a series of cases and separate the conditions into if statements. The first 3 if statements is for error checking and validate the user input to make sure valid numbers youre entered for month and year. <br> |
First case, if user left both month and year blank. | First case, if user left both month and year blank. | ||
Line 152: | Line 178: | ||
− | Third case, if user entered an invalid year, i.e. negative or in this problem | + | Third case, if user entered an invalid year, i.e. negative or in this problem you assume year 1000 is the first year |
{{CodeBlock | {{CodeBlock | ||
|Code= | |Code= | ||
Line 169: | Line 195: | ||
− | The last case, which is most important since it is processing the valid input will be the next step. In this case there is a set of nested cases. Again, | + | The last case, which is most important since it is processing the valid input will be the next step. In this case there is a set of nested cases. Again, you will have to separate the conditions into statements. In this problem you separated the months by 31 days, 30 days, and February as a case of its own for calculating leap years.<br> |
− | Months with 31 days, since | + | Months with 31 days, since you are assuming Jan = 0, the condition will "or" all integer representations of the months with 31 days. Remember July and August are back to back months with 31 days! The first condition checks for the appropriate monthNum and the nested conditions will assign the appropriate monthName depending on which integer was chosen. There may be alternate solutions and methods to solve this problem but for the sake of example and topics to cover you have chosen this method. Lastly, System.out.println prints an meaningful output message. |
{{CodeBlock | {{CodeBlock | ||
|Code= | |Code= | ||
Line 262: | Line 288: | ||
− | Lastly, for additional robustness, you can check if the user enters a monthNum that is too high. Remember, initially | + | Lastly, for additional robustness, you can check if the user enters a monthNum that is too high. Remember, initially you checked for negative monthNum, but this time is now the opposite end of the spectrum where the monthNum is too beyond the scope. If the monthNum is too high output a meaningful error message. |
{{CodeBlock | {{CodeBlock | ||
|Code= | |Code= | ||
//number is larger than 11 | //number is larger than 11 | ||
else | else | ||
− | System.out.println("Invalid month number entered, choose | + | System.out.println("Invalid month number entered, choose betyouen 1 - 12 (Jan - Dec)"); |
}} | }} | ||
{{OutputBlock | {{OutputBlock | ||
Line 273: | Line 299: | ||
Please enter the month [Jan(1) to Dec(12)]: 13 | Please enter the month [Jan(1) to Dec(12)]: 13 | ||
Please enter the Year: 2009 | Please enter the Year: 2009 | ||
− | Invalid month number entered, choose | + | Invalid month number entered, choose betyouen 1 - 12 (Jan - Dec) |
}} | }} | ||
Line 368: | Line 394: | ||
//number is larger than 11 | //number is larger than 11 | ||
else | else | ||
− | System.out.println("Invalid month number entered, choose | + | System.out.println("Invalid month number entered, choose betyouen 1 - 12 (Jan - Dec)"); |
} | } | ||
else | else |
Revision as of 01:51, 5 December 2011
Back to the Program-A-Day homepage
ProblemIn non-smart cell phones, text input can be handled by a
technique called multi-tap(example picture: link here). With multi-tap, you use the
letters on each key, and press a key multiple times to
access different letters. For instance, to get the letter 'a',
you press the 2 key once, but to get 'n', you press the 6
key twice (the first press gives you 'm', the second
switches 'm' to 'n'). A slight pause by the user will indicate
the end of inputting one character and a beginning of the
input of the next character.
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
characters.
Enter code: 222666 62227777444 Translated Code: COMCSI 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.
| |
---|---|---|
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); System.out.print("\nEnter code: "); } else { System.out.println("Invalid Input"); System.out.print("Enter code: "); } }
int year, monthNum; String monthName; Scanner input = new Scanner(System.in); //sentinel values for error checking monthNum = -1; year = -1; monthName = "";
System.out.print("Please enter the month [Jan(1) to Dec(12)]: "); if(input.hasNextInt()) { monthNum = input.nextInt(); System.out.print("Please enter the Year: "); if(input.hasNextInt()) { //code here } else { System.out.println("Please try again, there was an error with the month or the year you have entered"); } } else { System.out.println("Invalid month number entered, choose betyouen 1 - 12 (Jan - Dec)"); } Please enter the month [Jan(1) to Dec(12)]: abcd Please enter the Year: 2009 Invalid input of month Please enter the month [Jan(1) to Dec(12)]: 12 Please enter the Year: asdf Invalid Input of Year
First case, if user left both month and year blank. if (monthNum <= 0 && year <= 999) System.out.println("Please try again, there was an error with the month and the year you have entered"); Please enter the month [Jan(1) to Dec(12)]: -1 Please enter the Year: 987 Please try again, there was an error with the month and the year you have entered
else if (monthNum <= 0) { System.out.println("Please try again, there was an error with the month you have entered"); } Please enter the month [Jan(1) to Dec(12)]: -1 Please enter the Year: 2009 Please try again, there was an error with the month you have entered
else if (year <= 999) { System.out.println("Please try again, there was an error with the month you have entered"); } Please enter the month [Jan(1) to Dec(12)]: 12 Please enter the Year: 987 Please try again, there was an error with the month you have entered
The last case, which is most important since it is processing the valid input will be the next step. In this case there is a set of nested cases. Again, you will have to separate the conditions into statements. In this problem you separated the months by 31 days, 30 days, and February as a case of its own for calculating leap years. Months with 31 days, since you are assuming Jan = 0, the condition will "or" all integer representations of the months with 31 days. Remember July and August are back to back months with 31 days! The first condition checks for the appropriate monthNum and the nested conditions will assign the appropriate monthName depending on which integer was chosen. There may be alternate solutions and methods to solve this problem but for the sake of example and topics to cover you have chosen this method. Lastly, System.out.println prints an meaningful output message. //JAN //MAR //MAY //JUL //AUG //OCT //DEC if (monthNum == 1 || monthNum == 3 || monthNum == 5 || monthNum == 7 || monthNum == 8 || monthNum == 10 || monthNum == 12) { if (monthNum == 1) monthName = "January"; else if (monthNum == 3) monthName = "March"; else if (monthNum == 5) monthName = "May"; else if (monthNum == 7) monthName = "July"; else if (monthNum == 8) monthName = "August"; else if (monthNum == 10) monthName = "October"; else if (monthNum == 12) monthName = "December"; System.out.println(monthName + " " + year + " has " + MONTH31 + " days."); } Please enter the month [Jan(1) to Dec(12)]: 3 Please enter the Year: 2009 March 2009 has 31 days.
//FEB else if (monthNum == 2) { monthName = "February"; //check for leap if (year % 4 == 0) System.out.println(monthName + " " + year + " has " + (FEB + 1) + " days."); else System.out.println(monthName + " " + year + " has " + FEB + " days."); } Please enter the month [Jan(1) to Dec(12)]: 2 Please enter the Year: 2008 February 2009 has 29 days. Please enter the month [Jan(1) to Dec(12)]: 2 Please enter the Year: 2009 February 2009 has 28 days.
//APR //JUN //SEP //NOV else if (monthNum == 4|| monthNum == 6 || monthNum == 9 || monthNum == 11) { if (monthNum == 4) monthName = "April"; else if (monthNum == 6) monthName = "June"; else if (monthNum == 9) monthName = "September"; else if (monthNum == 11) monthName = "November"; System.out.println(monthName + " " + year + " has " + MONTH30 + " days."); } Please enter the month [Jan(1) to Dec(12)]: 11 Please enter the Year: 2009 November 2009 has 30 days.
Lastly, for additional robustness, you can check if the user enters a monthNum that is too high. Remember, initially you checked for negative monthNum, but this time is now the opposite end of the spectrum where the monthNum is too beyond the scope. If the monthNum is too high output a meaningful error message. //number is larger than 11 else System.out.println("Invalid month number entered, choose betyouen 1 - 12 (Jan - Dec)"); Please enter the month [Jan(1) to Dec(12)]: 13 Please enter the Year: 2009 Invalid month number entered, choose betyouen 1 - 12 (Jan - Dec)
| ||
Code |