Melow 0 Newbie Poster

i made a binary calculator... it calculates two numbers in binary code
i'm new in programming so this is what i came up with, it's not the best implementation, but it might help someone

known bugs: when u try to make more calculations in one "session" it doesn't clear the variables(i don't know why) so it gives wrong answers

public class Calculator extends JFrame implements ActionListener {
	public static JButton b[] = new JButton[10];
	public static JButton plus, minus, prod, clear, div, equal;
	public static JTextArea text;
	public static FlowLayout layout;
	public static JFrame frame;
	public static String binaryNumber, newNo, oldNo;
	public static String fieldText;

	public static void main(String args[]) {
		for (int i = 0; i < 10; i++) {
			b[i] = new JButton("" + i + "");
		}
		plus = new JButton("+");
		minus = new JButton("-");
		prod = new JButton("*");
		div = new JButton("/");
		equal = new JButton("=");
		clear = new JButton("C");
		text = new JTextArea(1, 15);
		frame = new JFrame("Calculator");

		JFrame.setDefaultLookAndFeelDecorated(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container container = frame.getContentPane();
		container.setLayout(new FlowLayout());

		container.add(text);
		for (int i = 0; i < 10; i++) {
			container.add(b[i]);
		}
		container.add(plus);
		container.add(minus);
		container.add(div);
		container.add(prod);
		container.add(equal);
		container.add(clear);

		frame.setSize(200, 180);
		frame.setResizable(false);
		frame.setVisible(true);
		//when the button is pressed it prints in the textArea the number in binary code
		b[0].addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String sum;
				if (e.getSource() == b[0])
					binaryNumber = Integer.toBinaryString(0); //1,2,3.. for each number
				fieldText = text.getText();
				if (fieldText == null || fieldText.equals("")) {
					text.setText(binaryNumber);
				} else {
					sum = addBinary(fieldText, binaryNumber);
					text.setText(sum);
				}
			}
		});
`// same ActionListener, for each number
		b[9].addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String sum;
				if (e.getSource() == b[9])
					binaryNumber = Integer.toBinaryString(9);
				fieldText = text.getText();
				if (fieldText == null || fieldText.equals("")) {
					text.setText(binaryNumber);
				} else {
					sum = addBinary(fieldText, binaryNumber);
					text.setText(sum);
				}
			}
		});

		//when the plus button is pressed the first given number is stored in a variable
		// and it waits for the next number
		//it calculates the result when '='button is pressed
		plus.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == plus)
					oldNo = text.getText();

				text.setText("");
				System.out.println("old  " + oldNo);

				equal.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						if (e.getSource() == equal) {
							//takes the second number
							newNo = text.getText();
							//call the function to calculate the two numbers
							binaryNumber = plus(newNo, oldNo); // call the function for minus and the rest
							text.setText(binaryNumber);
							
							//for debugging
							System.out.println("old  " + oldNo);
							System.out.println("new " + newNo);
							int t = 0;
							t = Integer.parseInt(binaryNumber, 2);
							System.out.println("sum " + binaryNumber);
							System.out.println("sum " + t);
							
						}

					}
				});
			}

		});

// same.. ActionListener for each operation

		//clears the textArea
		clear.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == clear)
					text.setText("");
					binaryNumber = null;
					newNo = null;
					oldNo = null;

			}
		});

	}

	// method to add two binary numbers
	// gets 2 strings, convert them to decimal
	// from decimal glue the two numbers in a string
	// then from the glued string to int
	// finally, from int to binary
	public static String addBinary(String no1, String no2) {

		Integer sum = 0, var1 = 0, var2 = 0;
		String result = null, sumInInt = null;
		
		var1 = Integer.parseInt(no1, 2);
		var2 = Integer.parseInt(no2, 2);

		sumInInt = var1.toString() + var2.toString();
		sum = Integer.parseInt(sumInInt);
		result = Integer.toBinaryString(sum);
		/*
		 * System.out.println("stringToInt1  " + stringToInt1);
		 * System.out.println("var1  " + var1); 
		 * System.out.println("var2  " + var2); 
		 * System.out.println("sumInInt " + sumInInt);
		 * System.out.println("sum  " + sum); 
		 * System.out.println("result  " + result);
		 */
		return result;

	}

	//same logic as 'addBinary' but calculates the sum of two numbers
	public static String produs(String no1, String no2) {

		Integer sum = 0, var1 = 0, var2 = 0;
		String result = null;

		var1 = Integer.parseInt(no1, 2);
		var2 = Integer.parseInt(no2, 2);

		sum = var1 * var2; // sum = var1 [+,-,/] var2; ... for each operation
		result = Integer.toBinaryString(sum);

		return result;

	}


	@Override
	public void actionPerformed(ActionEvent e) {

	}

}

i didn't post the whole code because is too long:P

NormR1 commented: Why post only part of the code? +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.