- Hands-On Design Patterns with Java
- Dr. Edward Lavieri
- 234字
- 2021-06-24 14:58:07
The Conversion class
The Conversion class applies when there is conversion from numbers to letters (int to char). The class has a single String class variable and a constructor method that is passed to the user's input. The class has one additional method, the convertToCharacters() method. That method converts the user's input into a character array and then processes that array, one character at a time. The processing is implemented with a simple switch statement. Each numerical value, from 0 through 9, has a corresponding character. The characters are printed one at a time until the entire character array has been processed.
The first section of our Conversion class follows:
public class Conversion {
// class variable
public String userInput;
// constructor
public Conversion(String userInput) {
this.userInput = userInput;
}
public void convertToCharacters(String userInput) {
this.userInput = userInput;
System.out.print("Decrypted Message: ");
char answer[] = userInput.toCharArray();
The remaining code from the Conversion class is provided here:
for (int i=0; i < answer.length; i++) {
switch (answer[i]) {
case '0':
System.out.print("A");
break;
case '1':
System.out.print("E");
break;
case '2':
System.out.print("I");
break;
case '3':
System.out.print("Y");
break;
case '4':
System.out.print("O");
break;
case '5':
System.out.print("L");
break;
case '6':
System.out.print("R");
break;
case '7':
System.out.print("T");
break;
case '8':
System.out.print("C");
break;
case '9':
System.out.print("S");
break;
}
}
}
}
The preceding code implements a 10-character mapping of integers. A complete implementation would account for the entire alphabet or, depending on the language, alphabets.
- 企業(yè)數(shù)字化創(chuàng)新引擎:企業(yè)級PaaS平臺HZERO
- 數(shù)據(jù)庫應(yīng)用實(shí)戰(zhàn)
- 數(shù)亦有道:Python數(shù)據(jù)科學(xué)指南
- OracleDBA實(shí)戰(zhàn)攻略:運(yùn)維管理、診斷優(yōu)化、高可用與最佳實(shí)踐
- SQL優(yōu)化最佳實(shí)踐:構(gòu)建高效率Oracle數(shù)據(jù)庫的方法與技巧
- 深入淺出 Hyperscan:高性能正則表達(dá)式算法原理與設(shè)計
- Python數(shù)據(jù)分析與挖掘?qū)崙?zhàn)(第3版)
- 數(shù)據(jù)科學(xué)實(shí)戰(zhàn)指南
- Hadoop大數(shù)據(jù)開發(fā)案例教程與項目實(shí)戰(zhàn)(在線實(shí)驗(yàn)+在線自測)
- 一本書講透Elasticsearch:原理、進(jìn)階與工程實(shí)踐
- MySQL技術(shù)內(nèi)幕:SQL編程
- 工業(yè)大數(shù)據(jù)融合體系結(jié)構(gòu)與關(guān)鍵技術(shù)
- Arquillian Testing Guide
- 深入理解Flink:實(shí)時大數(shù)據(jù)處理實(shí)踐
- 數(shù)字孿生