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

Commit 0aa1ec9

Browse files
committed
Updates
1 parent 06dcec6 commit 0aa1ec9

File tree

4 files changed

+92
-5
lines changed

4 files changed

+92
-5
lines changed

Complement.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,5 @@ public static void main (String[] args){
6060
}
6161
}
6262
}
63+
64+
// Kayel Calleja

NOTES.TXT

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,30 @@ try{
6161
} catch (NumberFormatException e) { //'e' exception object
6262
JOptionPane.showMessageDialog(null, "Invalid input. Please enter the correct value."); //eg with joptionpane
6363
continue;
64-
}
64+
}
65+
66+
################
67+
################### ###################
68+
# Modulo Operator #
69+
################### ###################
70+
################
71+
72+
*The modulo operator % is a mathematical operator that calculates the remainder of dividing one number by another.
73+
In Java and many other programming languages, the modulo operator is represented by the percent sign %.
74+
75+
Here's an example:
76+
77+
int a = 10;
78+
int b = 3;
79+
int c = a % b; // c = 1
80+
In this example, a is divided by b, which results in a quotient of 3 with a remainder of 1.
81+
The value of c is set to the remainder of this division, which is 1.
82+
83+
The modulo operator is often used in programming to perform tasks like checking if a number is even or odd,
84+
generating random numbers within a range, or cycling through a sequence of values.
85+
86+
By taking the remainder of the decimal value divided by 10, the method extracts the digit in the ones place. For example,
87+
if the decimal value is 1234, taking 1234 % 10 would result in a value of 4, which is the last digit.
88+
89+
// String Builder breaks down mulitple little strings e.g sb.append = group ,
90+
// sb.append = two. and compile them into one whole string acting as a buffer : String Result = sb.toString();

main.class

2.13 KB
Binary file not shown.

main.java

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class main extends JFrame implements ActionListener{
77
private JButton button1;
88
private JButton button2;
99
private JButton button3;
10+
private JButton button4;
1011

1112
public main() {
1213
super("Main Menu");
@@ -15,20 +16,23 @@ public main() {
1516
button1 = new JButton("Unsigned Integers");
1617
button2 = new JButton("Sign+Magnitude");
1718
button3 = new JButton("2's Complement");
19+
button4 = new JButton("Binary Coded Decimal");
1820

1921
// Set action listeners for buttons
2022
button1.addActionListener(this);
2123
button2.addActionListener(this);
2224
button3.addActionListener(this);
25+
button4.addActionListener(this);
2326

2427
// Add buttons to the frame
2528
getContentPane().setLayout(new FlowLayout());
2629
getContentPane().add(button1);
2730
getContentPane().add(button2);
2831
getContentPane().add(button3);
32+
getContentPane().add(button4);
2933

3034
// Set frame properties
31-
setSize(300, 110);
35+
setSize(330, 110);
3236
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
3337
setLocationRelativeTo(null);
3438
}
@@ -38,8 +42,10 @@ public void actionPerformed(ActionEvent e) {
3842
us();
3943
} else if (e.getSource() == button2) {
4044
sm();
41-
} else if (e.getSource()== button3) {
45+
} else if (e.getSource() == button3) {
4246
c();
47+
} else if (e.getSource() == button4) {
48+
bcd();
4349
}
4450
}
4551
private void sm(){
@@ -177,10 +183,63 @@ private void c(){
177183
}
178184
}
179185
}
180-
186+
private void bcd(){
187+
String explanation, explanation2, explanation3, explanation4;
188+
189+
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.";
190+
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.";
191+
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.";
192+
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.";
193+
194+
JOptionPane.showMessageDialog(null, explanation, "Binary Coded Decimal Explanation", JOptionPane.INFORMATION_MESSAGE);
195+
JOptionPane.showMessageDialog(null, explanation2, "Binary Coded Decimal Explanation", JOptionPane.INFORMATION_MESSAGE);
196+
JOptionPane.showMessageDialog(null, explanation3, "Binary Coded Decimal Explanation", JOptionPane.INFORMATION_MESSAGE);
197+
JOptionPane.showMessageDialog(null, explanation4, "Binary Coded Decimal Explanation", JOptionPane.INFORMATION_MESSAGE);
198+
199+
while (true) {
200+
// Read decimal input from the user
201+
String input = JOptionPane.showInputDialog(null, "Please Enter an Integer:");
202+
203+
if (input == null || input.equalsIgnoreCase("q")) {
204+
JOptionPane.showMessageDialog(null, "Operation Cancelled");
205+
break;
206+
}
207+
208+
// Convert input string to integer
209+
int decimal;
210+
try {
211+
decimal = Integer.parseInt(input);
212+
} catch (NumberFormatException e) {
213+
JOptionPane.showMessageDialog(null, "Invalid input. Please enter an integer.");
214+
continue;
215+
}
216+
217+
// Convert decimal to BCD and display the result
218+
String bcd = decimalToBCD(decimal);
219+
JOptionPane.showMessageDialog(null, "BCD binary value: " + bcd);
220+
}
221+
}
222+
public static String decimalToBCD(int decimal){
223+
StringBuilder bcd = new StringBuilder();
224+
225+
while (decimal > 0){
226+
int digit = decimal % 10;
227+
String binary = String.format("%04d", Integer.parseInt(Integer.toBinaryString(digit)));
228+
bcd.insert(0, binary);
229+
decimal /= 10;
230+
}
231+
// Split the BCD representation into groups of four digits
232+
for (int i = bcd.length() - 4; i > 0; i -= 4) {
233+
bcd.insert(i," ");
234+
}
235+
return bcd.toString();
236+
}
237+
181238
public static void main (String[] args){
182239
main menu = new main();
183240
menu.setVisible(true);
184241

185242
}
186-
}
243+
}
244+
245+
// Kayel Calleja

0 commit comments

Comments
 (0)