官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 洛川县| 敖汉旗| 泸水县| 卢湾区| 武冈市| 资源县| 宜昌市| 大竹县| 夏邑县| 彰化市| 克东县| 梅河口市| 拜泉县| 宁陕县| 连城县| 观塘区| 安陆市| 博野县| 漳州市| 乌鲁木齐县| 鹤壁市| 双柏县| 阿巴嘎旗| 文山县| 普兰店市| 乌兰察布市| 莲花县| 宜黄县| 南阳市| 潮州市| 雅江县| 石嘴山市| 锦州市| 辽中县| 东乌珠穆沁旗| 安徽省| 平潭县| 山东省| 铜鼓县| 洪泽县| 德昌县|