1 Replies - 824 Views - Last Post: 27 April 2011 - 06:45 PM Rate Topic: -----

#1 Cliftron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-April 11

Binary Calculator: And/Or

Posted 27 April 2011 - 12:06 PM

I'm trying to make the and and or buttons return the proper value to the JLabel but so far it just returns a 0.
I know its a logical error because the label returns a string of 0's when trying to use the and function but it returns string2 when the Or is used. I'm not asking anyone to do it for me but a little help would be appreciated. Thanks guys.

//************************************************************
//Written by Clifton Carter 
//Date: 04/18/2011
//
//Project Name: Binary Calculator
//
//Project Description:
//This binary calculator contains 9 buttons and a JLabel. It
//store the value input into the label until a button is pressed
//then the first string of numbers is converted into binary and
//stored in a int[].
//
//************************************************************

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class BiCalc extends JFrame implements ActionListener
{
	private static final long serialVersionUID = 1L;

	Container base;
	
	private JLabel txt;
	private JButton add, sub, multi, divide, equals, Or, And, digitOne, digitZero;
	private int total, num1, num2, tempInt;
	private int flag = 0, flag2 = 0;
	private char  temp[], temp2[];
	private String string1 = "", string2 = "", tempStr = "";
	
	public BiCalc()
	{
		//Container added
		base = this.getContentPane();
		base.setLayout(null);
		
		// JLabel and JButtons instantiated
		txt = new JLabel("");
		
		add = new JButton ("+");
		sub = new JButton ("-");
		divide = new JButton ("/");
		multi = new JButton ("x");
		equals = new JButton ("=");
		Or = new JButton ("Or");
		And = new JButton ("&");
		digitOne = new JButton ("1");
		digitZero = new JButton ("0");
		
		//JLabel and JButtons bounds set
		txt.setBounds(0, 0, 300, 60);
		add.setBounds(0,60,100,30);
		sub.setBounds(100,60,100,30);
		divide.setBounds(200,60,100,30);
		multi.setBounds(0,90,100,30);
		equals.setBounds(100,90,100,30);
		Or.setBounds(200,90,100,30);
		And.setBounds(0,120,100,30);
		digitOne.setBounds(100,120,100,30);
		digitZero.setBounds(200,120,100,30);
		
		//All items added to the container
		base.add(txt);
		base.add(add);
		add.addActionListener(this);
		base.add(sub);
		sub.addActionListener(this);
		base.add(divide);
		divide.addActionListener(this);
		base.add(multi);
		multi.addActionListener(this);
		base.add(equals);
		equals.addActionListener(this);
		base.add(Or);
		Or.addActionListener(this);
		base.add(And);
		And.addActionListener(this);
		base.add(digitOne);
		digitOne.addActionListener(this);
		base.add(digitZero);
		digitZero.addActionListener(this);
		
		temp = new char[8];
		temp2 = new char[8];
	}
	
	//The toBinary() method converts the string input to binary with the
	//Integer.parseInt(s,2) method.Which variable depends on if a 
	//button has been pressed or not.
	public void toBinary()
	{
		num1 = Integer.parseInt(string1, 2);
		num2 = Integer.parseInt(string2, 2);	
	}
	
	public void toArray()
	{
	  for (int x=0;x < string1.length();x++)	
	  {temp[x] = string1.charAt(x);}
	  for (int x=0;x < string2.length();x++)	
	  {temp2[x] = string1.charAt(x);}
	}
	
	public void toOutput()
	{
		txt.setText(Integer.toBinaryString(total));
	}
		
	public void actionPerformed (ActionEvent e)
	{
		Object source = e.getSource();

		
		if (source == digitZero) 
		{
			if (flag == 0)
			{
				txt.setText(txt.getText() + 0);
				string1 = txt.getText();
			}
			if (flag == 1)
			{
				txt.setText(txt.getText() + 0);
				string2 = txt.getText();
			}
		}
		
		if (source == digitOne) 
		{
			if (flag == 0)
			{
				txt.setText(txt.getText() + 1);
				string1 = txt.getText();
			}
			if (flag == 1)
			{
				txt.setText(txt.getText() + 1);
				string2 = txt.getText();
			}
		}
		
		if (source == multi) 
		{
			flag = 1;
			flag2 = 3;
			txt.setText("");
		}
		
		if (source == divide) 
		{
			flag = 1;
			flag2 = 4;
			txt.setText("");
		}
		
		if (source == add) 
		{
			flag = 1;
			flag2 = 1;
			txt.setText("");
		}
		
		if (source == sub) 
		{
			flag = 1;
			flag2 = 2;
			txt.setText("");
		}
		
		if (source == And) 
		{
			flag2 = 5;
			txt.setText("");
		}
		
		if(source == Or)
		{	
			flag2 = 6;
			txt.setText("");
		}
		
		if (source == equals) 
		{
			
			switch (flag2)
			{
			case 1:
				toBinary();
				total = (num1 + num2);
				toOutput();
				break;
			case 2:
				toBinary();
				total = (num1 - num2);
				toOutput();
				break;
			case 3:
				toBinary();
				total = (num1*num2);
				toOutput();
				break;
			case 4:
				toBinary();
				total = (num1/num2);
				toOutput();
				break;
			case 5:
				if(string1.length() > string2.length())
				{ tempInt = string1.length();}
				else
				{ tempInt = string2.length();}
				
				toArray();
				
				for (int x = 0; x < tempInt; x++)
				{
					if(temp[x] == '1' && temp2[x] == '1')
					{
						tempStr += "1";
					}
					else
					{
						tempStr += "0";
					}
				}
				txt.setText(tempStr);
				break;
			case 6:
				if(string1.length() > string2.length())
				{ tempInt = string1.length();}
				else
				{ tempInt = string2.length();}
				
				toArray();
				
				for (int x = 0; x < tempInt; x++)
				{
					if(temp[x] == '1' || temp2[x] == '1')
					{
						tempStr += "1";
					}
					else
					{
						tempStr += "0";
					}
				}
				txt.setText(tempStr);
				break;
				
			}
			
			string1 = "";
			string2 = "";
			flag = 0;
		}
		
	}
		
	
	public static void main(String arg[])

	{

		BiCalc obj = new BiCalc();
		obj.setVisible(true);
		obj.setTitle("Binary Calculator");
		obj.setExtendedState(MAXIMIZED_BOTH);
		obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}

}
 

This post has been edited by Cliftron: 27 April 2011 - 12:13 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Binary Calculator: And/Or

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Binary Calculator: And/Or

Posted 27 April 2011 - 06:45 PM

	public void toArray()
	{
	  for (int x=0;x < string1.length();x++)	
	  {temp[x] = string1.charAt(x);}
	  for (int x=0;x < string2.length();x++)	
	  {temp2[x] = string1.charAt(x);}         // shouldn't that be string2.charAt() 
	}



And you will need to reset tempStr = ""; before doing your && loop
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1