|
| 1 | +import javax.swing.*; |
| 2 | + |
| 3 | +public class BinaryCodedDecimal { |
| 4 | + public static void main (String[] args){ |
| 5 | + String explanation, explanation2, explanation3, explanation4; |
| 6 | + |
| 7 | + explanation = "BCD stands for Binary-Coded Decimal, which is a way of representing decimal digits using a binary code.\nIn BCD, each decimal digit is represented by a four-bit binary code, which can take on values from 0000 to 1001."; |
| 8 | + explanation2 = "For example, the decimal number 57 would be represented in BCD as 0101 0111,\nwhere the first four bits (0101) represent the digit 5 and the second four bits (0111) represent the digit 7."; |
| 9 | + explanation3 = "BCD is based on the idea of representing each decimal digit as a binary number.\nSince there are 10 possible values for a decimal digit (0-9), it requires four bits to represent each digit, because four bits can represent 16 possible values.\nTherefore, in BCD, each decimal digit is represented by a unique four-bit code, with no overlap between codes."; |
| 10 | + explanation4 = "One potential drawback of BCD is that it requires more storage space than traditional binary representation.\nFor example, a 16-bit binary number can represent values up to 65535, while a 16-bit BCD number can only represent values up to 9999.\nHowever, for applications that require precise decimal arithmetic or easy human readability, BCD can be a useful encoding scheme."; |
| 11 | + |
| 12 | + JOptionPane.showMessageDialog(null, explanation, "Binary Coded Decimal Explanation", JOptionPane.INFORMATION_MESSAGE); |
| 13 | + JOptionPane.showMessageDialog(null, explanation2, "Binary Coded Decimal Explanation", JOptionPane.INFORMATION_MESSAGE); |
| 14 | + JOptionPane.showMessageDialog(null, explanation3, "Binary Coded Decimal Explanation", JOptionPane.INFORMATION_MESSAGE); |
| 15 | + JOptionPane.showMessageDialog(null, explanation4, "Binary Coded Decimal Explanation", JOptionPane.INFORMATION_MESSAGE); |
| 16 | + |
| 17 | + while (true) { |
| 18 | + // Read decimal input from the user |
| 19 | + String input = JOptionPane.showInputDialog(null, "Please Enter an Integer:"); |
| 20 | + |
| 21 | + if (input == null || input.equalsIgnoreCase("q")) { |
| 22 | + JOptionPane.showMessageDialog(null, "Operation Cancelled"); |
| 23 | + break; |
| 24 | + } |
| 25 | + |
| 26 | + // Convert input string to integer |
| 27 | + int decimal; |
| 28 | + try { |
| 29 | + decimal = Integer.parseInt(input); |
| 30 | + } catch (NumberFormatException e) { |
| 31 | + JOptionPane.showMessageDialog(null, "Invalid input. Please enter an integer."); |
| 32 | + continue; |
| 33 | + } |
| 34 | + |
| 35 | + // Convert decimal to BCD and display the result |
| 36 | + String bcd = decimalToBCD(decimal); |
| 37 | + JOptionPane.showMessageDialog(null, "BCD binary value: " + bcd); |
| 38 | + } |
| 39 | + } |
| 40 | + public static String decimalToBCD(int decimal){ |
| 41 | + StringBuilder bcd = new StringBuilder(); |
| 42 | + |
| 43 | + while (decimal > 0){ |
| 44 | + int digit = decimal % 10; |
| 45 | + String binary = String.format("%04d", Integer.parseInt(Integer.toBinaryString(digit))); |
| 46 | + bcd.insert(0, binary); |
| 47 | + decimal /= 10; |
| 48 | + } |
| 49 | + // Split the BCD representation into groups of four digits |
| 50 | + for (int i = bcd.length() - 4; i > 0; i -= 4) { |
| 51 | + bcd.insert(i," "); |
| 52 | + } |
| 53 | + return bcd.toString(); |
| 54 | + } |
| 55 | +} |
| 56 | +// Kayel Calleja |
| 57 | + |
| 58 | +// // ######### LOOP EXPLANATION ######### \\ |
| 59 | +// initialization: int i = bcd.length() -4; it sets a counter variable i to the index of the fourth-last character in the string. |
| 60 | +// Condition: i >0; this is checked before each iteration fo the loop as long as i is greater than 0 the lopp will continue to execute. this ensures that the spaces are only added at the beginning of the string. |
| 61 | +// Iteration i -= 4; This is the code that is executed after each iteration of the loop. In this case, we are decrementing i by 4 so that we skip over the previous group of four digits and move to the next group of four digits in the BCD representation. |
| 62 | +// \\ ######### LOOP EXPLANATION ######### // |
0 commit comments