Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Commit f9c7f35

Browse files
committed
bcd addition update
only source file update
1 parent 96f6a0d commit f9c7f35

File tree

1 file changed

+86
-22
lines changed

1 file changed

+86
-22
lines changed

main.java

Lines changed: 86 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import java.awt.event.*;
44

55

6-
public class Main extends JFrame implements ActionListener{
6+
public class main extends JFrame implements ActionListener{
77

88
private JButton binaryS;
99
private SubMenuBS subMenuBS;
1010
private JButton booleanA;
1111
private SubMenuBA subMenuBA;
1212

13-
public Main() {
13+
public main() {
1414
super("Main Menu");
1515

1616
binaryS = new JButton("Binary Systems");
@@ -485,46 +485,110 @@ private void c(){
485485
}
486486
}
487487
}
488-
private void bcd(){
488+
private void bcd(){
489489
String explanation, explanation2, explanation3, explanation4;
490490

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.";
495495

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);
500500

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() {
502565
// 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:");
504567

505568
if (input == null || input.equalsIgnoreCase("q")) {
506569
JOptionPane.showMessageDialog(null, "Operation Cancelled");
507-
break;
570+
return;
508571
}
509572

510573
// Convert input string to integer
511574
int decimal;
512575
try {
513576
decimal = Integer.parseInt(input);
514-
} catch (NumberFormatException e) {
577+
} catch (NumberFormatException e) {
515578
JOptionPane.showMessageDialog(null, "Invalid input. Please enter an integer.");
516-
continue;
579+
return;
517580
}
518581

519582
// Convert decimal to BCD and display the result
520583
String bcd = decimalToBCD(decimal);
521584
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();
526590

527-
while (decimal > 0){
591+
while (decimal > 0){
528592
int digit = decimal % 10;
529593
String binary = String.format("%04d", Integer.parseInt(Integer.toBinaryString(digit)));
530594
bcd.insert(0, binary);
@@ -617,7 +681,7 @@ private void cr(){
617681
}
618682

619683
public static void main(String[] args){
620-
Main mainMenu = new Main();
684+
main mainMenu = new main();
621685
mainMenu.setVisible(true);
622686
}
623687
}

0 commit comments

Comments
 (0)