Results 1 to 7 of 7
  1. #1
    tonespy is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Unhappy jAVA Assignment Help

    Java Code:
     import java.awt.*;
    //import javax.swing.JFrame;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Calculator extends JFrame implements ActionListener{
    
    	JPanel panel1 = new JPanel();
    	JPanel panel2 = new JPanel();
    	JPanel panel3 = new JPanel();
    	JPanel panel4 = new JPanel();
    	JPanel panel5 = new JPanel();
    	
    	// this array initializes the numeric buttons 0-9
    	static final JButton[] numbers ={new JButton("0"),new JButton("1"),new JButton("2"),new JButton("3"),
    		                  new JButton("4"),new JButton("5"),new JButton("6"),new JButton("7"),
    		                  new JButton("8"),new JButton("9")};
    	
    	static final JButton dot=new JButton(".");
    	static final JButton unary_plus_minus=new JButton("+-");
    	static final JButton plus=new JButton("+");
    	static final JButton minus=new JButton("-");
    	static final JButton star=new JButton("*");
    	static final JButton divide=new JButton("/");
    	static final JButton square_root=new JButton("sqr"); // square root of a number
    	static final JButton modulo=new JButton("%");  // remainder of a no divided by another  
    	static final JButton inverse=new JButton("1/x"); // reciprocal of a number
    	static final JButton equals=new JButton("=");
    	
    	// decimal, binary, octal, clear and exit buttons
    	static final JButton dec=new JButton("Dec");
    	static final JButton bin=new JButton("Bin");//disable performing operations in dec and oct mode, switch to octal mode
    												// perform add and subtraction in binary
    	
    	static final JButton oct=new JButton("Oct"); //disable performing operations in dec and bin mode, switch to octal mode
    												 // perform add and subtraction in octal only	
    	static final JButton clear=new JButton("C"); // clear the calculator screen
    	static final JButton close=new JButton("Exit"); // close the application
    	
    	
    	static final JButton mem_read=new JButton("MR"); // recall what is stored in memory
    	static final JButton mem_clear=new JButton("MC"); // cancel value in memory
    	static final JButton mem_store=new JButton("MS"); // store value in memory
    	static final JButton mem_add=new JButton("M+"); // add to what is stored in memory
    		
    	static JTextField display= new JTextField(20);// calculator screen
    	
    	public Calculator() {
    		panel1.setLayout(new FlowLayout(FlowLayout.LEFT,6,4)); // panel to store the numeric and 
    		int i;                                                       // arithmetic operators 
    		for (i=1;i<4;i++) panel1.add(numbers[i]); //add 1-3 number buttons to panel1
    		panel1.add(plus);
    		
    		for (i=4;i<7;i++) panel1.add(numbers[i]); //add 4-6 number buttons to panel1
    		panel1.add(minus);
    				
    		for (i=7;i<10;i++) panel1.add(numbers[i]); //add 7-9 number buttons to panel1
    		panel1.add(star);
    		
    		panel1.add(numbers[0]); //add 0 button to panel1
    		panel1.add(unary_plus_minus);
    		panel1.add(dot);
    		panel1.add(divide);
    		
          		
    		// south
    		panel2.setLayout(new FlowLayout(FlowLayout.LEFT));
    
    		panel2.add(dec);
    		panel2.add(bin);
    		panel2.add(oct);
    		panel2.add(clear);
    		panel2.add(close);
    		
    		//west
    		panel3.setLayout(new GridLayout(4,1));
    		panel3.add(mem_clear);
    		panel3.add(mem_read);
    		panel3.add(mem_store);
    		panel3.add(mem_add);
    		
    		//east
    		panel4.setLayout(new GridLayout(4,1));
    		panel4.add(square_root);
    		panel4.add(modulo);
    		panel4.add(inverse);
    		panel4.add(equals);
    		
    		
    		panel5.setLayout(new BorderLayout());
    		panel5.add(display,BorderLayout.NORTH);
    		panel5.add(panel3,BorderLayout.WEST);
    		panel5.add(panel4,BorderLayout.EAST);
    		panel5.add(panel1,BorderLayout.CENTER);
    		panel5.add(panel2,BorderLayout.SOUTH);
    		
    		
    		getContentPane().add(panel5);
    		
    		// register the buttons
    		for (i=0;i<10;i++) numbers[i].addActionListener(this); // this registers listener for buttons 0-9
    		close.addActionListener(this);
    		clear.addActionListener(this);
                    dec.addActionListener(this);
                    bin.addActionListener(this);
    		oct.addActionListener(this);
                    //
                    mem_clear.addActionListener(this);
                    mem_read.addActionListener(this);
                    mem_store.addActionListener(this);
                    mem_add.addActionListener(this);
                    //
                    square_root.addActionListener(this);
                    modulo.addActionListener(this);
                    equals.addActionListener(this);
                    inverse.addActionListener(this);
                    //
                    unary_plus_minus.addActionListener(this);
                    plus.addActionListener(this);
                    minus.addActionListener(this);
                    star.addActionListener(this);
    		divide.addActionListener(this);
                    square_root.addActionListener(this);
    		// ...
    		// complete the rest
    	}
    	
    
    	
    	
    	
    	public void actionPerformed(ActionEvent e){
    	// write the event handlers for the remaining controls here
    		if(e.getSource()==close) System.exit(0);
    		else if(e.getSource()==clear) display.setText(" ");
    		//else if(e.getActionCommand()=="0") display.setText("0");
    		
    		else if(e.getSource()==inverse){ // this button computes inverse
    			float x= Float.parseFloat(display.getText().trim()); //convert string to float
    			if (x!=0) x=1/x;
    			display.setText(Float.toString(x));
    		}
    		else  display.setText(display.getText()+e.getActionCommand());  // event handler for buttons 0-9
    		
    	// .. complete the rest
    		
        }
    	
    	
    	
    	
    		public static void main(String[] args) {
    		Calculator c1 = new Calculator();
    		c1.setSize(320, 215);
    		c1.setTitle("Calculator");
    		c1.setVisible(true);
    		c1.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
    	}
    
    
    }
    Last edited by tonespy; 12-16-2012 at 09:25 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,301
    Rep Power
    20

    Default Re: jAVA Assignment Help

    Please go through the Forum Rules, particularly the third paragraph. Also go through Guide For New Members and BB Code List - Java Programming Forum - Learn Java Programming and edit your post accordingly.

    Did you have a question?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    tonespy is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: jAVA Assignment Help

    Quote Originally Posted by DarrylBurke View Post
    Please go through the Forum Rules, particularly the third paragraph. Also go through Guide For New Members and BB Code List - Java Programming Forum - Learn Java Programming and edit your post accordingly.

    Did you have a question?

    db
    Yes, making the buttons perform there functions

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: jAVA Assignment Help

    Well that is easy, just implement the functions within the action listener.
    I like likes!

  5. #5
    tonespy is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: jAVA Assignment Help

    Quote Originally Posted by tonespy View Post
    Yes, making the buttons perform there functions
    I'm having problem implementing the memory section

  6. #6
    tonespy is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: jAVA Assignment Help

    okaay
    Last edited by tonespy; 12-17-2012 at 06:35 AM.

  7. #7
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: jAVA Assignment Help

    Um... what memory function? ^^
    I like likes!

Similar Threads

  1. Java Assignment help?
    By smith999 in forum New To Java
    Replies: 1
    Last Post: 11-24-2012, 12:25 AM
  2. java assignment
    By Tuz in forum New To Java
    Replies: 28
    Last Post: 02-16-2012, 08:39 PM
  3. java assignment
    By drewjordan in forum New To Java
    Replies: 9
    Last Post: 01-15-2012, 11:44 PM
  4. java assignment
    By omgonoes in forum New To Java
    Replies: 13
    Last Post: 04-23-2011, 02:29 AM
  5. My java assignment -- please help me !
    By java_beginner1 in forum New To Java
    Replies: 11
    Last Post: 05-20-2010, 04:00 PM

Posting Permissions

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