Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This task is designed for implementation within a single file. Conversions: Deci

ID: 3702670 • Letter: T

Question

This task is designed for implementation within a single file.


Conversions:
Decimal to hex, Binary to Decimal, Hex to Decimal, and String permutation:
Write a recursive method that converts a decimal number into a hex number as a string. The method
header is:
. public static String dec2Hex(int value)
Write a recursive method that parses a binary number as a string into a decimal integer. The method
header is:
. public static int bin2Dec(String binaryString)
Write a recursive method that parses a hex number as a string into a decimal integer. The method
header is:
. public static int hex2Dec(String hexString)
Write a recursive method to print all the permutations of a string. For example, for the string abc,
the permutation is:
. abc
. acb
. bac
. bca
. cab
. cba
Define the following two methods. The second is a helper method.
. public static void displayPermutation(String s)
. public static void displayPermutation(String s1, String s2)
The first method simply invokes displayPermutation(” ”, s). The second method uses a loop to move a
character from s2 to s1 and recursively invokes it with a new s1 and s2. The base case is that s2 is empty
and prints s1 to the console.

Explanation / Answer

PROGRAM

import java.util.Scanner;

// Create main class Conversions

class Conversions

{

// Create and implement static method dec2Hex() and return string value

public static String dec2Hex(int value)

{

StringBuilder builder = new StringBuilder(); // Create StringBuilder Object builder

if (value>0) // check condition value>0

{

String hex = dec2Hex(value/16); // hex number recursive dec2Hex() value

String code = "0123456789ABCDEF"; // declare string code for constant Hex numbers

int hexDigit = value % 16; // calculate remainder value and stored into hexDegit

builder.append(hex + code.charAt(hexDigit)); // using builder object append that adding hex and code value

}

return builder.toString(); // return hexdecimal number using builder object

}

// Create and implement static method bin2Dec() and return integer value

public static int bin2Dec(String binaryString)

{

int len=binaryString.length(); // length of the binaryString and assing to len

if(len==0) // check condition len=0 then return 0

return 0;

String a=binaryString.substring(0,1); // take substring value and assign to a

String b=binaryString.substring(1); // take substring value and assign to b

return Integer.parseInt(a)*(int)Math.pow(2,len-1)+bin2Dec(b); // return integer conversion with sum of power of each value and recursive

}

// Create and implement static method hex2Dec() and return integer value

public static int hex2Dec(String hexString)

{

int decimal = 0; // declare constant value decimal=0

String code = "0123456789ABCDEF"; // define constant String code value

hexString = hexString.toUpperCase(); // convert string in upper case

int length = hexString.length(); // length of a string and assign to length

if (length > 0) // check condition length >0

{

char ch = hexString.charAt(0); // assign first character into ch

int digit = code.indexOf(ch); // hex code value store into digit

String sub = hexString.substring(1); // substring is assign to sub

decimal = digit * (int) Math.pow(16, length - 1) + hex2Dec(sub); // calculate decimal value using recusively

}

return decimal; // return decimal value

}

// Create and implement static method displayPermutation()

public static void displayPermutation(String s)

{

displayPermutation("",s); // recursively

}

public static void displayPermutation(String s1,String s2)

{

if(s2.isEmpty()) // check condition second string is empty

{

System.err.println(s1+s2); // Display error

}

else

{

for(int i=0;i<s2.length();i++) // create for loop until length of second string

{

displayPermutation(s1+s2.charAt(i),s2.substring(0,i)+s2.substring(i+1,s2.length())); // calling recusively

}

}

}

public static void main(String args[])

{

Scanner scr=new Scanner(System.in); // declare Scanner object scr

// declare variables

int num;

String binary;

String hex,perm;

// dynamically reading variables in console

System.out.print("Enter Decimal Number: ");

num=scr.nextInt();

System.out.print("Enter Binary Number (Ex:1001): ");

binary=scr.next();

System.out.print("Enter Hexadecimal Number: ");

hex=scr.next();

System.out.print("Enter a String: ");

perm=scr.next();

System.out.println();

String s=dec2Hex(num); // calling dec2Hex() method

System.out.println("Decimal to Hexa: "+s);

int bin= bin2Dec(binary); // calling bin2Dec() method

System.out.println("Binary to Decimal: "+bin);

int dec=hex2Dec(hex); // calling hex2Dec() method

System.out.println("Hex to Decimal: "+dec);

System.out.println(" Permutation is:");

displayPermutation(perm); // calling displayPermutation() method

}

}

OUTPUT-1


F:>javac Conversions.java

F:>java Conversions
Enter Decimal Number: 10
Enter Binary Number (Ex:1001): 1001
Enter Hexadecimal Number: ab
Enter a String: abc

Decimal to Hexa: A
Binary to Decimal: 9
Hex to Decimal: 171

Permutation is:
abc
acb
bac
bca
cab
cba

OUTPUT-2


F:>java Conversions
Enter Decimal Number: 15
Enter Binary Number (Ex:1001): 1111
Enter Hexadecimal Number: 123a
Enter a String: java

Decimal to Hexa: F
Binary to Decimal: 15
Hex to Decimal: 4666

Permutation is:
java
jaav
jvaa
jvaa
jaav
java
ajva
ajav
avja
avaj
aajv
aavj
vjaa
vjaa
vaja
vaaj
vaja
vaaj
ajav
ajva
aajv
aavj
avja
avaj