/*
=========================================================
			ADDITION.JAVA

  Main: addition of two binary numbers
=========================================================
*/

import java.applet.*;
import java.awt.*;

public class addition extends Applet
{
	//creating new objects that are instances of classes
	Button b1= new Button("0");
	Button b2= new Button("0");
	Button b3= new Button("0");
	Button b0= new Button("0");

	Button a1= new Button("0");
	Button a2= new Button("0");
	Button a3= new Button("0");
	Button a0= new Button("0");

	Label c0=new Label("");
	Label c1=new Label("");
	Label c2=new Label("");
	Label c3=new Label("");
	Label c4=new Label("");

	Button bgo= new Button("Add");

	//constructor
	public addition()
	{
	}

	//information about the applet
	public String getAppletInfo()
	{
		return "Name: addition\r\n" +
		       "Author: Ana Cristina Fernandez\r\n" +
		       "Created with Microsoft Visual J++ Version 1.1";
	}

	// performing initialization 


	public void init()
	{	
		//adding buttons
		add(a0);
    	add(a1);
		add(a2);
		add(a3);
		
		add(b0);
		add(b1);
		add(b2);
		add(b3);

		add(c0);
		add(c1);
		add(c2);
		add(c3);
		add(c4);
		
		add(bgo);

	}

	public void destroy()
	{
	}

	public void paint(Graphics g)

	{	
		//defining the size and position of the buttons
		a0.reshape(160,100,20,20);
		a1.reshape(140,100,20,20);
		a2.reshape(120,100,20,20);
		a3.reshape(100,100,20,20);
		
		b0.reshape(160,125,20,20);
		b1.reshape(140,125,20,20);
		b2.reshape(120,125,20,20);
		b3.reshape(100,125,20,20);

		c0.reshape(170,155,20,20);
		c1.reshape(150,155,20,20);
		c2.reshape(130,155,20,20);
		c3.reshape(110,155,20,20);
		c4.reshape(90,155,20,20);
		
		bgo.reshape(100,60,80,20);

		g.drawLine(80,150,200,150);
		g.drawLine(80,120,90,120);
		g.drawLine(85,115,85,125);

	}

	public void start()
	{
	}
	
	public void stop()
	{
	}

	//Defining the events
	public boolean action(Event e, Object obj) {
        //changing the label of b0 every time this button is pressed 
		if (e.target==b0) {
			if (b0.getLabel()=="0"){
				b0.setLabel("1");}
			else {
				b0.setLabel("0");}
			}
		//changing the label of b1 every time this button is pressed 
		else if (e.target==b1) {
			if (b1.getLabel()=="0"){
				b1.setLabel("1");}
			else {
				b1.setLabel("0");}
			}
		//changing the label of b2 every time this button is pressed 
		else if (e.target==b2) {
			if (b2.getLabel()=="0"){
				b2.setLabel("1");}
			else {
				b2.setLabel("0");}
			}
		//changing the label of b3 every time this button is pressed 
		else if (e.target==b3) {
			if (b3.getLabel()=="0"){
				b3.setLabel("1");}
			else {
				b3.setLabel("0");}
			}
		 //changing the label of a0 every time this button is pressed 
		 if (e.target==a0) {
			if (a0.getLabel()=="0"){
				a0.setLabel("1");}
			else {
				a0.setLabel("0");}
			}
		 //changing the label of a1 every time this button is pressed 
		else if (e.target==a1) {
			if (a1.getLabel()=="0"){
				a1.setLabel("1");}
			else {
				a1.setLabel("0");}
			}
		//changing the label of a2 every time this button is pressed 
		else if (e.target==a2) {
			if (a2.getLabel()=="0"){
				a2.setLabel("1");}
			else {
				a2.setLabel("0");}
			}
		//changing the label of a3 every time this button is pressed 
		else if (e.target==a3) {
			if (a3.getLabel()=="0"){
				a3.setLabel("1");}
			else {
				a3.setLabel("0");}
			}
		//c=a+b everytime the button 'go' is pressed
		else if (e.target==bgo) {
			//mini = true;
			c4.setText("0");
			//c0
			if ((b0.getLabel()=="1") & (a0.getLabel()=="1")){
				c0.setText("0");
				c4.setText("1");}
			else if ((b0.getLabel()=="0") & (a0.getLabel()=="0")){
				c0.setText("0");}
			else {c0.setText("1");}
			//c1
			if ((b1.getLabel()=="1") & (a1.getLabel()=="1") & (c4.getText()=="1"))  {
				c1.setText("1");
				c4.setText("1");}
			else if ((b1.getLabel()=="1") & (a1.getLabel()=="1") & (c4.getText()=="0"))
			{	c1.setText("0");
				c4.setText("1");}

			else if ((b1.getLabel()=="1") & (a1.getLabel()=="0") & (c4.getText()=="1"))
			{	c1.setText("0");
				c4.setText("1");}

			else if ((b1.getLabel()=="0") & (a1.getLabel()=="1") & (c4.getText()=="1"))
			{	c1.setText("0");
				c4.setText("1");}

			else if ((b1.getLabel()=="0") & (a1.getLabel()=="0") & (c4.getText()=="0"))
			{c1.setText("0");}
			else {c1.setText("1");
				c4.setText("0");}

			//c2
			if ((b2.getLabel()=="1") & (a2.getLabel()=="1") & (c4.getText()=="1"))  {
				c2.setText("1");
				c4.setText("1");}
			else if ((b2.getLabel()=="1") & (a2.getLabel()=="1") & (c4.getText()=="0"))
			{	c2.setText("0");
				c4.setText("1");}

			else if ((b2.getLabel()=="1") & (a2.getLabel()=="0") & (c4.getText()=="1"))
			{	c2.setText("0");
				c4.setText("1");}

			else if ((b2.getLabel()=="0") & (a2.getLabel()=="1") & (c4.getText()=="1"))
			{	c2.setText("0");
				c4.setText("1");}

			else if ((b2.getLabel()=="0") & (a2.getLabel()=="0") & (c4.getText()=="0"))
			{c2.setText("0");}
			else {c2.setText("1");
				c4.setText("0");}
			//c3
			if ((b3.getLabel()=="1") & (a3.getLabel()=="1") & (c4.getText()=="1"))  {
				c3.setText("1");
				c4.setText("1");}
			else if ((b3.getLabel()=="1") & (a3.getLabel()=="1") & (c4.getText()=="0"))
			{	c3.setText("0");
				c4.setText("1");}

			else if ((b3.getLabel()=="1") & (a3.getLabel()=="0") & (c4.getText()=="1"))
			{	c3.setText("0");
				c4.setText("1");}

			else if ((b3.getLabel()=="0") & (a3.getLabel()=="1") & (c4.getText()=="1"))
			{	c3.setText("0");
				c4.setText("1");}

			else if ((b3.getLabel()=="0") & (a3.getLabel()=="0") & (c4.getText()=="0"))
			{c3.setText("0");}
			else {c3.setText("1");
				c4.setText("0");}


			if (c4.getText()=="0"){
				c4.setText("");}
			repaint();
		}
		
		
        return true;
    }

}

