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

Can someone help me with this program... in Java, please. Prompt the user to ent

ID: 3794878 • Letter: C

Question

Can someone help me with this program... in Java, please. Prompt the user to enter a string containing a hexadecimal number. Use class Integer to parse the input to a base-10 integer variable and display it. Convert this base-10 variable to a string holding an equivalent number in binary. Print this binary equivalent. Use class Integer to convert the binary string back to base-10 and display it. SAMPLE OUTPUT Enter a string holding a hexadecimal integer less than 256 1a That hex number in base-10 is 26 As a binary number it's 11010 That binary number back to base-10 is 26.

Explanation / Answer

package sample;
import java.util.Scanner;
public class hw1 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Enter a string holding a hexadecimal integer less than 256 : ");
String hex=s.nextLine();
int n=hexaToDecimal(hex);
System.out.println(" Decimal equivalent base-10 is : "+n);
System.out.println(" Binary equivalent base-2 is : "+Integer.toBinaryString(n));
System.out.println(" Decimal equivalent base-10 is : "+binToDec(Integer.parseInt(Integer.toBinaryString(n))));
}
public static int hexaToDecimal(String s)
{
String digits = "0123456789ABCDEF";
s = s.toUpperCase();
int val = 0;
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
int d = digits.indexOf(c);
val = 16*val + d;
}
return val;
}
public static int binToDec(Integer n)
{
   int binnum, decnum=0, i=1, rem;
  
       binnum=n.intValue();
while(binnum != 0)
{
rem = binnum%10;
decnum = decnum + rem*i;
i = i*2;
binnum = binnum/10;
}
      
return decnum;

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote