|
3 | 3 | import java.awt.event.*;
|
4 | 4 |
|
5 | 5 |
|
6 |
| -public class Main extends JFrame implements ActionListener{ |
| 6 | +public class main extends JFrame implements ActionListener{ |
7 | 7 |
|
8 | 8 | private JButton binaryS;
|
9 | 9 | private SubMenuBS subMenuBS;
|
10 | 10 | private JButton booleanA;
|
11 | 11 | private SubMenuBA subMenuBA;
|
12 | 12 |
|
13 |
| - public Main() { |
| 13 | + public main() { |
14 | 14 | super("Main Menu");
|
15 | 15 |
|
16 | 16 | binaryS = new JButton("Binary Systems");
|
@@ -485,46 +485,110 @@ private void c(){
|
485 | 485 | }
|
486 | 486 | }
|
487 | 487 | }
|
488 |
| - private void bcd(){ |
| 488 | + private void bcd(){ |
489 | 489 | String explanation, explanation2, explanation3, explanation4;
|
490 | 490 |
|
491 |
| - 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."; |
492 |
| - 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."; |
493 |
| - 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."; |
494 |
| - 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."; |
| 491 | + 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."; |
| 492 | + 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."; |
| 493 | + 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."; |
| 494 | + 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."; |
495 | 495 |
|
496 |
| - JOptionPane.showMessageDialog(null, explanation, "Binary Coded Decimal Explanation", JOptionPane.INFORMATION_MESSAGE); |
497 |
| - JOptionPane.showMessageDialog(null, explanation2, "Binary Coded Decimal Explanation", JOptionPane.INFORMATION_MESSAGE); |
498 |
| - JOptionPane.showMessageDialog(null, explanation3, "Binary Coded Decimal Explanation", JOptionPane.INFORMATION_MESSAGE); |
499 |
| - JOptionPane.showMessageDialog(null, explanation4, "Binary Coded Decimal Explanation", JOptionPane.INFORMATION_MESSAGE); |
| 496 | + JOptionPane.showMessageDialog(null, explanation, "Binary Coded Decimal Explanation", JOptionPane.INFORMATION_MESSAGE); |
| 497 | + JOptionPane.showMessageDialog(null, explanation2, "Binary Coded Decimal Explanation", JOptionPane.INFORMATION_MESSAGE); |
| 498 | + JOptionPane.showMessageDialog(null, explanation3, "Binary Coded Decimal Explanation", JOptionPane.INFORMATION_MESSAGE); |
| 499 | + JOptionPane.showMessageDialog(null, explanation4, "Binary Coded Decimal Explanation", JOptionPane.INFORMATION_MESSAGE); |
500 | 500 |
|
501 |
| - while (true) { |
| 501 | + |
| 502 | + while (true) { |
| 503 | + String choice = JOptionPane.showInputDialog(null, "Please choose an option:\n1. Example Integer\n2. Addition"); |
| 504 | + |
| 505 | + if (choice == null || choice.equalsIgnoreCase("q")) { |
| 506 | + JOptionPane.showMessageDialog(null, "Operation Cancelled"); |
| 507 | + break; |
| 508 | + } |
| 509 | + |
| 510 | + if (choice.equals("1")) { |
| 511 | + exampleInteger(); |
| 512 | + } else if (choice.equals("2")) { |
| 513 | + performAddition(); |
| 514 | + } else { |
| 515 | + JOptionPane.showMessageDialog(null, "Invalid choice. Please enter '1', '2'."); |
| 516 | + } |
| 517 | + } |
| 518 | + } |
| 519 | + |
| 520 | + public static void performAddition() { |
| 521 | + while (true) { |
| 522 | + String input1 = JOptionPane.showInputDialog(null, "Please Enter the First Integer:"); |
| 523 | + |
| 524 | + if (input1 == null || input1.equalsIgnoreCase("q")) { |
| 525 | + JOptionPane.showMessageDialog(null, "Operation Cancelled"); |
| 526 | + break; |
| 527 | + } |
| 528 | + |
| 529 | + // Convert the first input string to an integer |
| 530 | + int firstInt; |
| 531 | + try { |
| 532 | + firstInt = Integer.parseInt(input1); |
| 533 | + } catch (NumberFormatException e) { |
| 534 | + JOptionPane.showMessageDialog(null, "Invalid input. Please enter an integer."); |
| 535 | + continue; |
| 536 | + } |
| 537 | + |
| 538 | + // Prompt the user to enter the second integer |
| 539 | + String input2 = JOptionPane.showInputDialog(null, "Please Enter the Second Integer:"); |
| 540 | + |
| 541 | + if (input2 == null || input2.equalsIgnoreCase("q")) { |
| 542 | + JOptionPane.showMessageDialog(null, "Operation Cancelled"); |
| 543 | + break; |
| 544 | + } |
| 545 | + |
| 546 | + // Convert the second input string to an integer |
| 547 | + int secondInt; |
| 548 | + try { |
| 549 | + secondInt = Integer.parseInt(input2); |
| 550 | + } catch (NumberFormatException e) { |
| 551 | + JOptionPane.showMessageDialog(null, "Invalid input. Please enter an integer."); |
| 552 | + continue; |
| 553 | + } |
| 554 | + |
| 555 | + // Perform BCD addition |
| 556 | + int sum = firstInt + secondInt; |
| 557 | + |
| 558 | + // Convert decimal sum to BCD and display the result |
| 559 | + String bcd2 = decimalToBCD(sum); |
| 560 | + JOptionPane.showMessageDialog(null, "BCD binary value of sum: " + bcd2); |
| 561 | + } |
| 562 | + } |
| 563 | + |
| 564 | + public static void exampleInteger() { |
502 | 565 | // Read decimal input from the user
|
503 |
| - String input = JOptionPane.showInputDialog(null, "Please Enter an Integer:"); |
| 566 | + String input = JOptionPane.showInputDialog(null, "Please Enter an example Integer:"); |
504 | 567 |
|
505 | 568 | if (input == null || input.equalsIgnoreCase("q")) {
|
506 | 569 | JOptionPane.showMessageDialog(null, "Operation Cancelled");
|
507 |
| - break; |
| 570 | + return; |
508 | 571 | }
|
509 | 572 |
|
510 | 573 | // Convert input string to integer
|
511 | 574 | int decimal;
|
512 | 575 | try {
|
513 | 576 | decimal = Integer.parseInt(input);
|
514 |
| - } catch (NumberFormatException e) { |
| 577 | + } catch (NumberFormatException e) { |
515 | 578 | JOptionPane.showMessageDialog(null, "Invalid input. Please enter an integer.");
|
516 |
| - continue; |
| 579 | + return; |
517 | 580 | }
|
518 | 581 |
|
519 | 582 | // Convert decimal to BCD and display the result
|
520 | 583 | String bcd = decimalToBCD(decimal);
|
521 | 584 | JOptionPane.showMessageDialog(null, "BCD binary value: " + bcd);
|
522 |
| - } |
523 |
| - } |
524 |
| - public static String decimalToBCD(int decimal){ |
525 |
| - StringBuilder bcd = new StringBuilder(); |
| 585 | + } |
| 586 | + |
| 587 | + |
| 588 | + public static String decimalToBCD(int decimal){ |
| 589 | + StringBuilder bcd = new StringBuilder(); |
526 | 590 |
|
527 |
| - while (decimal > 0){ |
| 591 | + while (decimal > 0){ |
528 | 592 | int digit = decimal % 10;
|
529 | 593 | String binary = String.format("%04d", Integer.parseInt(Integer.toBinaryString(digit)));
|
530 | 594 | bcd.insert(0, binary);
|
@@ -617,7 +681,7 @@ private void cr(){
|
617 | 681 | }
|
618 | 682 |
|
619 | 683 | public static void main(String[] args){
|
620 |
| - Main mainMenu = new Main(); |
| 684 | + main mainMenu = new main(); |
621 | 685 | mainMenu.setVisible(true);
|
622 | 686 | }
|
623 | 687 | }
|
|
0 commit comments