Question

In: Computer Science

1) Write a method that takes a string representing a positive binary number, and writes out...

1) Write a method that takes a string representing a positive binary number, and writes out the number in hex. (Extra credit opportunity. Write the method so that the input can be a binary number in two's compliment form.)

2) Write a method that takes an integer and writes out its value out in hex. You cannot use string formatting characters.

3) Write an encodeString method that encodes a string of ASCII Characters.

Input: It takes two parameters:

a) inputString: An string of visible ASCII characters.

b) key: A number must be between 1 and 8.

Output: A new string with the 8 bits of each character shifted left by the key and with random bits in the unused locations

4) Write a decodeString method that decodes a string of encoded characters

Input:

It takes two parameters:

a) inputString: An string of encoded characters.

b) key: A number must be between 1 and 8.

Output: A new string of ASCII character found by shifting right by the value of the key and with random bits cleared.

5) Write a breakCode method that attempts to decode an encoded string without knowing the key

Input: It takes one parameter. An encoded string

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

class Main
{
//Binary to Hexadecimal value
public static String binToHex(String binary)
{
int decimalValue = Integer.parseInt(binary,2);
String hexStr = Integer.toString(decimalValue,16);
return hexStr;
}
  
//Decimal to Hexadecimal value
static String decToHex(int decimalValue)
{
return Integer.toString(decimalValue,16);
}
  
//ENCODING
static String encodeString(String inputString, int key)
{
String encodedString="";
for(int i=0;i<inputString.length();i++)
{
int ch = (int) inputString.charAt(i);
//Left shift
encodedString += ""+(char) (ch<<key);
}
  
return encodedString;
}
  
//DECODING
static String decodeString(String inputString, int key)
{
String decodedString="";
for(int i=0;i<inputString.length();i++)
{
int ch = (int) inputString.charAt(i);
//Right Shift
decodedString += ""+(char)((ch>>key));
}
  
return decodedString;
}
  
//Break CODE
static void breakCode(String encodedString)
{
String possibleSolutions="";
for(int key=1;key<=8;key++)
{
possibleSolutions += decodeString(encodedString,key)+", ";
}
System.out.println("The Possible Solutions are: "+possibleSolutions);
}
  
//Test the methods
   public static void main (String[] args)
   {
   System.out.println("Binary 0110 to Hex: "+binToHex("0110"));
   System.out.println("Decimal 245 to Hex: "+decToHex(245));
  
   String str="012";
   String encodedString = encodeString(str,1);
   System.out.println(str+" is Encoded with key 1 to : "+encodedString);
  
   System.out.println(encodedString+" is decoded with key 1 to : "+decodeString(encodedString,1));

System.out.println("Performing CODE BREAK on "+encodedString);
  
breakCode(encodedString);
   }
}

===============

SCREENSHOT:

NOTE: we will get some no printable characters in the encoded strings...!!


Related Solutions

rogram that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary.
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x // 2Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string.Ex: If the input is:6the output is:110Your program must define and call the following two functions. The function integer_to_reverse_binary() should return a string of 1's...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
USE Coral Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is 6, the output is:011(6 in binary is 110; the algorithm outputs the bits in reverse).
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
In Java  Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is:6the output is:0116 in binary is 110; the algorithm outputs the bits in reverse.
[C] Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
6.19 LAB: Convert to binary - functionsWrite a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string.Ex: If the input is:6the output is:110Your program must define and call the following two functions. The IntegerToReverseBinary function...
(Python) Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
In Python8.21 Program: Convert to binary. Sections 2.7, 3.8, 5.2. Functions.Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x // 2Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string.Your program must define and call the following two functions. The function integer_to_reverse_binary() should return...
Write a recursive method that takes a String argument and recursively prints out each word in...
Write a recursive method that takes a String argument and recursively prints out each word in the String on a different line. Note: You will need to use methods such as indexOf() and substring() within the String class to identify each word and the remaining string. For example, if the input was “the cat purred”, then the method would print the following to the Java console: the cat purred Challenge Problem Write a recursive method that takes a string as...
Write a Java program which takes a String representing an arithmetic expression as an input and...
Write a Java program which takes a String representing an arithmetic expression as an input and displays whether or not the expression is balanced. If the expression is not balanced (i.e. the wrong # of parentheses or in the wrong order) it will display an error message. For Example: Input an expression: ( ( 2 + 4 ) * 2 ) Expression is balanced Input an expression: ( 5 * 7 – 6 ) ) Error: Expression is not balanced.......
Write a C++ program will read in the number of nodes and a binary relation representing...
Write a C++ program will read in the number of nodes and a binary relation representing a graph. The program will create an adjacency matrix from the binary relation. The program will then print the following : 1. The adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists and said so. The sample run of the program is as follows. The output should just like this: It starts by...
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. The code needs to be in Java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT