mbilal1's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
Members
Active Posts:
18 (0.03 per day)
Joined:
20-September 12
Profile Views:
258
Last Active:
User is offline Apr 28 2014 05:05 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   mbilal1 has not set their status

Posts I've Made

  1. In Topic: printing out to GUI textfield.

    Posted 6 May 2013

    View Postschutzzz, on 06 May 2013 - 02:10 PM, said:

    View Postmbilal1, on 06 May 2013 - 01:18 PM, said:

    okay so i have a program here that takes a 7 digit number and tells its prime factors. it runs perfectly. however I want it to print out to a gui textfield, not to system.out.print. can someone please help me figure out how i would do it.


    So are you also trying to have the input in the GUI textfield also?



    Yes, Instead of the long number = kb.nextLong(); that i have, i will get the long from a textfield.
  2. In Topic: Question about combining two programs, Java Calulator using Stacks.

    Posted 1 Dec 2012

    Well i am writing a GUI application, and i was trying to turn the console i found and put it into a GUI.
    Look at my second code, it doesnt have a scanner. The only problem i have with that is the what does the equal button do when pressed.

    View Postpbl, on 29 November 2012 - 04:53 PM, said:

    You are writing a console or a GUI application ?
    Use one or the other but do not mix them... no Scanner if you have a GUI. Switching from one to the other will make any user become sick
  3. In Topic: Question about combining two programs, Java Calulator using Stacks.

    Posted 29 Nov 2012

    View Postipushmycar, on 29 November 2012 - 01:26 PM, said:

    Instead of reading in the input from the user take the input from the GUI. Probably the display box. Parse the input, push and pop operands to a stack and operators to another. I believe with prefix you can use one stack if you start from the end of the input.


    That actually helped a lot and helped make me think of a way to do it. I just essentially merged the two codes into one. Here is the what I have so far. The only problem that I have right now is that I do not know how to make my "equal" sign work.

    import java.awt.*;
    
    import javax.swing.Box;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
    import java.util.*;
    public class Calculator extends JFrame implements ActionListener {
    	
    
    
    	public static void main(String args[])
         {
    		  
                Calculator cal=new Calculator("Calc");
         }
    	 
    	String numbers[]={"0","1","2","3","4","5","6","7","8","9","+","-","*","/",".","CE","C","=","+/-"};
        JButton button[]=new JButton[19];
        Panel p[]=new Panel[6];
        TextField text;
        Box box;
        FlowLayout fv;
         public  Calculator(String string)
        {
           
               this.setDefaultCloseOperation(EXIT_ON_CLOSE);
               box=Box.createVerticalBox();
               fv=new FlowLayout();
               fv.setVgap(20);
    
             this.setLocationRelativeTo(null);       
               for(int i=0;i<numbers.length;i++)
               {
                      button[i]=new JButton(numbers[i]);
                      button[i].addActionListener(this);
                      if(i!=15&&i!=16)
                      {
                             button[i].setPreferredSize(new Dimension(55,35));
                      }
                      else
                      {
                               button[i].setPreferredSize(new Dimension(85,35));
                      }
                      if(i<6)
                      {
                             p[i]=new Panel();
                             box.add(p[i]);
                      }
                      if(i==0)
                      {
                             text=new TextField("0",30);
                             p[0].add(text);
                      }
               }
               p[1].add(button[7]);p[1].add(button[8]);p[1].add(button[9]);p[1].add(button[10]); //7,8,9,+
               p[2].add(button[4]);p[2].add(button[5]);p[2].add(button[6]);p[2].add(button[11]); //4,5,6,-
               p[3].add(button[1]);p[3].add(button[2]);p[3].add(button[3]);p[3].add(button[12]); //1,2,3, *
               p[4].add(button[0]);p[4].add(button[14]);p[4].add(button[18]);p[4].add(button[13]); //0,., +/-, /
               p[5].add(button[15]);p[5].add(button[16]);p[5].add(button[17]); //CE, C, =
               this.add(box);
               this.setBounds(520,200,280,320);
               this.setResizable(false);
               this.setVisible(true);
               this.validate();
        }
           int press=0;                            
           int press1=0;                          
           int press2=0;                           
           int strlen=0;                           
           StringBuffer str=new StringBuffer("0"); 
           StringBuffer str2=new StringBuffer("0"); 
           double sum1=0;                          
    	
    	 public void actionPerformed(ActionEvent e){
    		 
    		 //display numbers
    		 if(e.getSource()==button[0])
    	        {
    	               this.displayNumber(0);
    	        }
    	        if(e.getSource()==button[1])
    	        {
    	               this.displayNumber(1);
    	        }
    	        if(e.getSource()==button[2])
    	        {
    	               this.displayNumber(2);
    	        }
    	        if(e.getSource()==button[3])
    	        {
    	               this.displayNumber(3);
    	        }
    	        if(e.getSource()==button[4])
    	        {
    	               this.displayNumber(4);
    	        }
    	        if(e.getSource()==button[5])
    	        {
    	               this.displayNumber(5);
    	        }
    	        if(e.getSource()==button[6])
    	        {
    	               this.displayNumber(6);
    	        }
    	        if(e.getSource()==button[7])
    	        {
    	               this.displayNumber(7);
    	        }
    	        if(e.getSource()==button[8])
    	        {
    	               this.displayNumber(8);
    	        }
    	        if(e.getSource()==button[9])
    	        {
    	         this.displayNumber(9);
    	        }
    	        
    	        //other variables
    	        if(e.getSource()==button[10])             
    	        {
    	            this.pressOperator(10);
    	        }
    	     if(e.getSource()==button[11])             
    	        {
    	            this.pressOperator(11);
    	        }
    	        if(e.getSource()==button[12])             
    	     {
    	               this.pressOperator(12);
    	        }
    	        if(e.getSource()==button[13])             
    	        {
    	        this.pressOperator(13);
    	        }   
    	        if(e.getSource()==button[14])             
    	        {
    	               if(str.charAt(str.length()-1)!='+'&&str.charAt(str.length()-1)!='-'&&str.charAt(str.length()-1)!='*'&&str.charAt(str.length()-1)!='/'&&press!=1)
    	               {
    	                      str.append(button[14].getText());
    	                      str2.append(button[14].getText());
    	                      text.setText(str.toString());
    	                      press=1;
    	               }
    	        }
    	        if(e.getSource()==button[18])             
    	        {
    	               if(str.charAt(str.length()-1)!='+'&&str.charAt(str.length()-1)!='-'&&str.charAt(str.length()-1)!='*'&&str.charAt(str.length()-1)!='/')
    	               {
    	                      if(press2==0)
    	                      {
    	                             str.insert(strlen, "-");
    	                             str2.insert(strlen, "@");               
    	                             text.setText(str.toString());
    	                             press2=1;
    	                      }
    	                      else
    	                      {
    	                             str.deleteCharAt(strlen);
    	                             str2.deleteCharAt(strlen);
    	                             text.setText(str.toString());
    	                             press2=0;
    	                      }
    	               }
    	        }
    	        if(e.getSource()==button[15])             
    	        {
    	               if(strlen!=0)
    	               {
    	                 str.delete(strlen, str.length());
    	                 str2.delete(strlen, str2.length());
    	                 text.setText(str.toString());
    	               }
    	               else
    	               {
    	                      str.delete(0,str.length());
    	                      str2.delete(0,str2.length());
    	                      str.append("0");
    	                      str2.append("0");
    	                      text.setText(str.toString());
    	                      press=0;
    	                      press2=0;
    	               }
    	        }
    	        if(e.getSource()==button[16])            
    	     {
    	             str.delete(0, str.length());
    	             str2.delete(0, str2.length());
    	              str.append("0");
    	              str2.append("0");
    	              text.setText(str.toString());
    	              press=0;
    	              press1=0;
    	              press2=0;
    	              strlen=0;
    	        }
    	        if(e.getSource()==button[17])             
    	        {
    	        	 
    	        }
    	      
    	 }
    	 
    	  public void pressOperator(int i)          
          {
                  if(str.charAt(str.length()-1)!='+'&&str.charAt(str.length()-1)!='-'&&str.charAt(str.length()-1)!='*'&&str.charAt(str.length()-1)!='/'&&str.charAt(str.length()-1)!='.')    
                  {
                         str.append(button[i].getText());
                         str2.append(button[i].getText());
                         text.setText(str.toString());
                         press=0;
                         press1=1;
                         press2=0;
                         strlen=str.length();
                  }
          }
    	    public void displayNumber(int i)         ///number display to box   
    	    {
    	          if(press1!=1&&press!=1&&str.charAt(0)=='0')
    	               {
    	                      str.replace(0, 1,button[i].getText());
    	                      str2.replace(0, 1,button[i].getText());
    	                   text.setText(str.toString());
    	               }
    	               else
    	               {
    	                      str.append(button[i].getText());
    	                      str2.append(button[i].getText());
    	                      text.setText(str.toString());
    	               }   
    	    }
    	 private static double add(Stack<Double> stk)
         {
            double op2 = stk.pop();
            double op1 = stk.pop();
            double result = op1 + op2;
            stk.push(result);
            return result;
         }// end add
         
          private static double subtract(Stack<Double> stk)
         {
            double op2 = stk.pop();
            double op1 = stk.pop();
            double result = op1 - op2;
            stk.push(result);
            return result;
         }// end subtract
         
          private static double divide(Stack<Double> stk)
         {
            double op2 = stk.pop();
            double op1 = stk.pop();
            double result = op1 / op2;
            stk.push(result);
            return result;
         }//end divide
      	
          private static double multiply(Stack<Double> stk)
         {
            double op2 = stk.pop();
            double op1 = stk.pop();
            double result = op1 * op2;
            stk.push(result);
            return result;
         }//end multiply
          
      	/**perform calculations based on operators, loop until operator stack is empty*/
          private static double calculate(Stack<Double> stkd, Stack<String> stks)
         {
            double result = 0;
            String str = "";
         	
            while(!stks.empty())  
            {
               str = stks.pop();
               if(str.equals("+"))
                  result = add(stkd);
               else if(str.equals("-"))
                  result = subtract(stkd);
               else if(str.equals("/"))
                  result = divide(stkd);
               else if(str.equals("*"))
                  result = multiply(stkd);
            }//end while
            return result;
         }
    	
    	
    }
    
    
    


    the equal operation is at line 201.

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Click here to e-mail me

Friends

mbilal1 hasn't added any friends yet.

Comments

mbilal1 has no profile comments yet. Why not say hello?