Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Unhappy Help with AWT CALCULATOR

    My codes ADD SUBTRACT MULTIPLY and DIVIDE sometimes work and sometimes don't. I can't identify what's wrong with my code. Can somebody help me please?

    Java Code:
    import java.io.*;
    import java.lang.Math.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Component.*;
    
    public class SuperCalcu extends Frame implements ActionListener, WindowListener
    
    /* IGUAL
     * PUNTO 
     * DECIMAL 
     * C 
     * MATH */
    
    {
    	//0 Label
    	
    	//1 ventana
    	TextField  Txventana;
    	
    	//29 botones
    	Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0; //10
    	Button b10algo, blog, bealgo, bln; //4
    	Button bpi, braiz, bpotencia, be; //4
    	Button bsin, bcos, btan, btrig; //4
    	Button bclear, bpunto, bigual; //3
    	Button bsuma, bresta, bmulti, bdivi; //4
    	
    	//5 paneles
    	Panel panelNorte, panelSur, panelEste, panelOeste, panelCentro;
    	
    	int v;
    
    	String aux1;
    	String aux2, lastCommand;
    	String valor, calcDisplay;
    	int op, contador=0;
    	
    	boolean punto=false;
    	
    	double n1, n2, n3, n4, preRes, secVal;
    	double respuesta=0,res;
    	
    	public static void main(String[] args) throws IOException
    	{
    		SuperCalcu form=new SuperCalcu(); //crea un objeto de la clase
    		form.setSize(400,600); //establece medidas de ventana en pixeles
    		form.setTitle ("CASIO FX-Superb007"); //Titulo
    		form.setVisible(true); //hace visible la ventana
    		
    	}
    	
    	public SuperCalcu() throws IOException {
    		
    		//Layout Principal
    		setLayout(new BorderLayout(1,1));//principal
    		
    		//Paneles
    		panelNorte= new Panel(new FlowLayout());
    		panelSur= new Panel(new GridLayout(1,1));
    	
    		panelEste= new Panel(new GridLayout(4,1));
    		panelOeste= new Panel(new GridLayout(5,1));
    		panelCentro= new Panel(new GridLayout(6,3));
    		
    		//NORTE, terminado
    		Txventana= new TextField(50);
    		Txventana.setEditable(false);
    		panelNorte.add(Txventana);
    		
    		//SUR, terminado
    		
    		bigual= new Button("=");//igual	
    		panelSur.add(bigual);	
    				//listener
    		bigual.addActionListener(this);
    		
    		//OESTE, terminado
    	
    		b10algo= new Button("10^x");//10^algo
    		blog= new Button("+/-");//Log
    		bealgo= new Button("e^x");//E^algo
    		bln= new Button("Ln");//Ln
    		be= new Button("e");//e	
    		
    		panelOeste.add(b10algo);
    		panelOeste.add(blog);
    		panelOeste.add(bealgo);
    		panelOeste.add(bln);
    		panelOeste.add(be);
    			
    				//listener	
    		b10algo.addActionListener(this);
    		blog.addActionListener(this);
    		bealgo.addActionListener(this);
    		bln.addActionListener(this);
    		be.addActionListener(this);
    			
    		//ESTE, terminado
    		
    		bsuma= new Button ("+");//suma
    		bresta= new Button("-");//resta
    		bmulti= new Button("*");//multi
    		bdivi= new Button("/");//divi		
    		
    		panelEste.add(bsuma);
    		panelEste.add(bresta);
    		panelEste.add(bmulti);
    		panelEste.add(bdivi);
    		
    				//listener
    		bsuma.addActionListener(this);
    		bresta.addActionListener(this);
    		bmulti.addActionListener(this);
    		bdivi.addActionListener(this);
    		
    		//CENTRO, terminado
    
    		b1= new Button("1");//1
    		b2= new Button("2");//2
    		b3= new Button("3");//3
    		b4= new Button("4");//4
    		b5= new Button("5");//5
    		b6= new Button("6");//6	
    		b7= new Button("7");//7
    		b8= new Button("8");//8
    		b9= new Button("9");//9
    		b0= new Button("0");//0
    		
    
    		
    		bpi= new Button("Pi");//pi
    		bpotencia= new Button("Potencia");//potencia
    		braiz= new Button("Raiz");//raiz
    
    
    		bsin= new Button("Sin");//sin
    		bcos= new Button("Cos");//cos
    		btan= new Button("Tan");//tan
    
    		
    		bpunto= new Button(".");//punto
    		bclear= new Button("C");//clear	
    		
    		panelCentro.add(bpi);
    		panelCentro.add(bpotencia);
    		panelCentro.add(braiz);
    		panelCentro.add(bsin);
    		panelCentro.add(bcos);
    		panelCentro.add(btan);
    		panelCentro.add(b1);
    		panelCentro.add(b2);
    		panelCentro.add(b3);
    		panelCentro.add(b4);
    		panelCentro.add(b5);
    		panelCentro.add(b6);
    		panelCentro.add(b7);
    		panelCentro.add(b8);
    		panelCentro.add(b9);
    		panelCentro.add(bpunto);				
    		panelCentro.add(b0);
    		panelCentro.add(bclear);
    		
    				//listener
    		bpi.addActionListener(this);
    		bpotencia.addActionListener(this);
    		braiz.addActionListener(this);
    		bsin.addActionListener(this);
    		bcos.addActionListener(this);
    		btan.addActionListener(this);
    		b1.addActionListener(this);
    		b2.addActionListener(this);
    		b3.addActionListener(this);
    		b4.addActionListener(this);
    		b5.addActionListener(this);
    		b6.addActionListener(this);
    		b7.addActionListener(this);
    		b8.addActionListener(this);
    		b9.addActionListener(this);
    		bpunto.addActionListener(this);
    		b0.addActionListener(this);
    		bclear.addActionListener(this);
    				
    		panelCentro.repaint();
    		
    		
    		//Agregar Paneles al Border Layout
    		
    		add(panelNorte, BorderLayout.NORTH);
    		add(panelSur, BorderLayout.SOUTH);
    		add(panelEste, BorderLayout.EAST);
    		add(panelOeste, BorderLayout.WEST);
    		add(panelCentro, BorderLayout.CENTER);
    
    	
    		//anade a la ventana un listener
    		addWindowListener(this);	}
    		
    		//TODO BIEN HASTA AQUI
    		
    		public void repaint(Graphics g){
    			
    			//intento color
    			g.setColor(Color.red);
    		}
    		
    		public void actionPerformed(ActionEvent e){
    			
    			//input numbers
    			
    			if(e.getSource() == b1){ aux1="1"; valor=Txventana.getText()+aux1; Txventana.setText(valor);		}
    			if(e.getSource() == b2){ aux1="2"; valor=Txventana.getText()+aux1; Txventana.setText(valor);		}
    			if(e.getSource() == b3){ aux1="3"; valor=Txventana.getText()+aux1; Txventana.setText(valor);		}
    			if(e.getSource() == b4){ aux1="4"; valor=Txventana.getText()+aux1; Txventana.setText(valor);		}
    			if(e.getSource() == b5){ aux1="5"; valor=Txventana.getText()+aux1; Txventana.setText(valor);		}
    			if(e.getSource() == b6){ aux1="6"; valor=Txventana.getText()+aux1; Txventana.setText(valor);		}
    			if(e.getSource() == b7){ aux1="7"; valor=Txventana.getText()+aux1; Txventana.setText(valor);		}
    			if(e.getSource() == b8){ aux1="8"; valor=Txventana.getText()+aux1; Txventana.setText(valor);		}
    			if(e.getSource() == b9){ aux1="9"; valor=Txventana.getText()+aux1; Txventana.setText(valor);		}
    			if(e.getSource() == b0){ aux1="0"; valor=Txventana.getText()+aux1; Txventana.setText(valor);		}
    			if(e.getSource() == bpunto&& punto==false){ aux1="."; valor=Txventana.getText()+aux1; Txventana.setText(valor); punto=true;	}
    		
    			//trabajando con funciones
    			if(e.getSource() == bpi){		Txventana.setText(""+Math.PI); valor="";	}
    			if(e.getSource() == be){		Txventana.setText(""+Math.E);  valor="";	}
    				
    			if(e.getSource() == bpotencia){	
    					aux2=Txventana.getText();
    				if(!aux2.equals("")){
    					preRes=Double.parseDouble(Txventana.getText());
    		            lastCommand="potencia";
    		            Txventana.setText("");}}
    		
    			if(e.getSource() == braiz){
    				
    				aux2=Txventana.getText();
    					if(!aux2.equals(""))
    						{n1=Double.parseDouble(Txventana.getText());
    						Txventana.setText(""+Math.sqrt(n1));
    						}	}
    			
    			if(e.getSource() == bsin){	
    				
    				aux2=Txventana.getText();
    					if(!aux2.equals(""))
    					{n1=Double.parseDouble(Txventana.getText());
    					Txventana.setText(""+Math.sin(Math.toRadians(n1)));
    					}}
    				
    			if(e.getSource() == bcos){
    				
    				aux2=Txventana.getText();
    					if(!aux2.equals(""))
    					{n1=Double.parseDouble(Txventana.getText());
    					Txventana.setText(""+Math.cos(Math.toRadians(n1)));
    					}	}
    					
    			if(e.getSource() == btan){
    
    				aux2=Txventana.getText();
    					if(!aux2.equals(""))
    						{n1=Double.parseDouble(Txventana.getText());
    						Txventana.setText(""+Math.tan(Math.toRadians(n1)));
    						}	}
    			
    			if(e.getSource() == b10algo){
    				
    				aux2=Txventana.getText();
    					if(!aux2.equals(""))
    						{n1=Double.parseDouble(Txventana.getText());
    						Txventana.setText(""+Math.pow(10.0,n1));
    						}	}
    						
    			if(e.getSource() == blog){ 				
    				
    				aux2=Txventana.getText();
    					if(!aux2.equals(""))
    						{n1=Double.parseDouble(Txventana.getText());
    						n1=n1*(-1);
    						Txventana.setText(""+n1);
    						}	}
    						
    			if(e.getSource() == bealgo){
    				
    					aux2=Txventana.getText();
    					if(!aux2.equals(""))
    						{n1=Double.parseDouble(Txventana.getText());
    						Txventana.setText(""+Math.pow(Math.E,n1));
    						}	}
    			
    			if(e.getSource() == bln){
    				
    				aux2=Txventana.getText();
    					if(!aux2.equals(""))
    						{n1=Double.parseDouble(Txventana.getText());
    						Txventana.setText(""+Math.log(n1));
    						}	}
    			
    			if(e.getSource() == bsuma){	
    					aux2=Txventana.getText();
    				if(!aux2.equals("")){
    					preRes=Double.parseDouble(Txventana.getText());
    		            lastCommand="+";
    		            Txventana.setText("");}	
    		
    			if(e.getSource() == bresta){ 
    					aux2=Txventana.getText();
    				if(!aux2.equals("")){
    					preRes=Double.parseDouble(Txventana.getText());
    		            lastCommand="-";
    		            Txventana.setText("");}
    				}
    				
    			if(e.getSource() == bmulti){ 
    					aux2=Txventana.getText();
    				if(!aux2.equals("")){
    					preRes=Double.parseDouble(Txventana.getText());
    		            lastCommand="*";
    		            Txventana.setText("");}}
    				
    			if(e.getSource() == bdivi){	
    					aux2=Txventana.getText();
    				if(!aux2.equals("")){
    					preRes=Double.parseDouble(Txventana.getText());
    		            lastCommand="/";
    		            Txventana.setText("");}}
    			
    			if(e.getSource() == bclear){ Txventana.setText("");	}
    			
    			if(e.getSource() == bigual){
              Double secVal=Double.parseDouble(Txventana.getText());
               
               if(lastCommand.equals("+"))
            	   res=preRes+secVal;
               else if(lastCommand.equals("-"))
                    res=preRes-secVal;
               else if(lastCommand.equals("*"))
                    res=preRes*secVal;   
               else if (lastCommand.equals("/"))
       				res=preRes/secVal; 
       			else if (lastCommand.equals("potencia"));
       				res=Math.pow(preRes,secVal);
        					              	             
               	   Txventana.setText(" "+res);
            	   lastCommand="=";
    					
    					}}
    		public double calcOperaciones(int op, double aux2){
    			if(op==1)
    			{}
    			if(op==2)
    			{}
    			if(op==3)
    			{}
    			if(op==4)
    			{}
    			if(op==5)
    			{}
    			return res;	}				
    		
    		public void windowClosing(WindowEvent e){
    			
    			System.exit(0);
    		}
    		public void windowActivated(WindowEvent e){
    		}
    		public void windowClosed(WindowEvent e){
    		}
    		public void windowDeactivated(WindowEvent e){
    		}
    		public void windowDeiconified(WindowEvent e){
    		}
    		public void windowIconified(WindowEvent e){
    		}				
    		public void windowOpened(WindowEvent e){
    		}		
    }
    Last edited by Fubarable; 03-29-2011 at 04:55 AM. Reason: code tags added

  2. #2
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    Java Code:
    if(e.getSource() == bsuma){	
    aux2=Txventana.getText();
    if(!aux2.equals("")){
    preRes=Double.parseDouble(Txventana.getText());
    lastCommand="+";
    Txventana.setText("");}	
    
    if(e.getSource() == bresta){ 
    aux2=Txventana.getText();
    if(!aux2.equals("")){
    preRes=Double.parseDouble(Txventana.getText());
    lastCommand="-";
    Txventana.setText("");}
    }
    
    if(e.getSource() == bmulti){ 
    aux2=Txventana.getText();
    if(!aux2.equals("")){
    preRes=Double.parseDouble(Txventana.getText());
    lastCommand="*";
    Txventana.setText("");}}
    
    if(e.getSource() == bdivi){	
    aux2=Txventana.getText();
    if(!aux2.equals("")){
    preRes=Double.parseDouble(Txventana.getText());
    lastCommand="/";
    Txventana.setText("");}}
    These ones. Cant add more than 2, sometimes stay in 0, howcome?
    Last edited by Fubarable; 03-29-2011 at 04:56 AM. Reason: code tags added

  3. #3
    gandrew is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Dear Madame: If you haven't solved this already, the problem lies in some brackets. For the addition operation:

    if(e.getSource() == bsuma){
    //System.out.println("Sum");
    aux2=Txventana.getText();
    if(!aux2.equals("")){
    preRes=Double.parseDouble(Txventana.getText());
    lastCommand="+";
    Txventana.setText("");}
    } // added this bracket

    The program flow is also falling into the exponent operation in the = button section. Remove the semi-colon so that the following line is part of the above condition.

    // program flow was falling into the exponent
    else if (lastCommand.equals("potencia")) //; problem was this semi-colon
    res=Math.pow(preRes,secVal); // which meant this line was always executed

    The best thing to do is comment out unnecessary functions and verify that addition works. Gradually uncomment more functions until you discover an error.

    Good luck.

Similar Threads

  1. Calculator
    By Moshe22 in forum New To Java
    Replies: 8
    Last Post: 01-17-2011, 06:29 AM
  2. Help in a calculator
    By Ayannie in forum New To Java
    Replies: 6
    Last Post: 01-04-2011, 09:21 PM
  3. Calculator
    By water in forum AWT / Swing
    Replies: 4
    Last Post: 09-23-2009, 07:00 AM
  4. help with calculator
    By kalibballer in forum New To Java
    Replies: 8
    Last Post: 04-01-2009, 01:57 PM
  5. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 08:42 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •