- 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.
- MySQL從入門到精通(第3版)
- 大數(shù)據(jù)算法
- Hadoop大數(shù)據(jù)實戰(zhàn)權威指南(第2版)
- 深度剖析Hadoop HDFS
- 一個64位操作系統(tǒng)的設計與實現(xiàn)
- Power BI商業(yè)數(shù)據(jù)分析完全自學教程
- 深入淺出Greenplum分布式數(shù)據(jù)庫:原理、架構和代碼分析
- 達夢數(shù)據(jù)庫運維實戰(zhàn)
- 貫通SQL Server 2008數(shù)據(jù)庫系統(tǒng)開發(fā)
- Scratch 2.0 Game Development HOTSHOT
- PostgreSQL高可用實戰(zhàn)
- AndEngine for Android Game Development Cookbook
- 數(shù)據(jù)迷霧:洞察數(shù)據(jù)的價值與內(nèi)涵
- 大學計算機:理解和運用計算思維
- SQL必知必會(第5版)