Secret Channels A company uses public Internet to carry its phone service. The v
ID: 3903642 • Letter: S
Question
Secret Channels
A company uses public Internet to carry its phone service. The voice data will be encrypted before sending to Internet. The encryption algorithm is as follows: For each four-digit decimal integer, (1) Add 5 to each digit, divide the sum by 10 and use the remainder to replace the digit, (2) Swap 1st-digit with 4th-digit, (3) Swap 2nd-digit with 3rd-digit. (The program must be named as Encrypted.java)
Implement a method as specified below: public static String encoding(String voiceData) This method encrypts voice data and return encrypted data
Input: A series of lines. Each line is a positive four-digit decimal integer.
Output: In each line, print the encrypted four-digit decimal integer.
Sample Input (sample data file is provided for your testing purpose):
0123
5890
9999
Sample Output
8765
5430
4444
Explanation / Answer
Below is the JVA code I hope that i have provided sufficient comments for your better understanding
public class encoding(String voiceData)
{
public static void main(String args[])
{
String s = encoding("5890");
System.oout.println("encode string is " + s);
}
}
public static String encoding(String voiceData)
{
//convert string to integer
int number = Integer.parseInt(voiceData);
int res=0,rem;
char temp;
while(number != 0)
{
rem=number%10 + 5;
res=res*10 + rem%10;
number = number/10;
}
//convert resultant value to string
String numberAsString = Integer.toString(res);
//reverse the string
StringBuilder sb = new StringBuilder(numberAsString);
numberAsString=sb.reverse().toString();
//check whether string is of length 4 or not
while(numberAsString.length()!=4)
{
//Append 0
numberAsString + = '0';
}
//swap the characters
char ch[] = str.toCharArray();
temp = ch[0];
ch[0] = ch[3];
ch[3] = temp;
temp = ch[1];
ch[1] = ch[2];
ch[2] = temp;
return ch;
}
Hope i have answered your question satisfactorily.Leave doubts in comment section if any.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.