Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

URGENT Help With Scientific Calculator!

Thread view: 
Maria Laura Re - 13 Dec 2003 19:59 GMT
Hi everybody,

I designed a calculator, and I need help with the rest of
the actions.  I know I need to use the different Math
methods, but I tried that and it didn't work.  Also,
it needs to work as an applet and application, and in the
applet, the buttons don't appear in order, how can I fix
that?

I will really appreciate your help with this program, I
can't get it to work and I'm frustrated, I need to finish
this for next Tuesday 16th.  Please e-mail me at
laurare@rocketmail.com.

Below is the code for the calcualtor.

Thanks a lot!

-Maria

[code]
// calculator
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class calculator extends JApplet implements
ActionListener
{
 private JButton one, two, three, four, five, six, seven,
 eight, nine, zero, dec, eq, plus, minus, mult, div,
clear,
 mem, mrc, sin, cos, tan, asin, acos, atan, x2, sqrt, exp,
pi, percent;
 private JLabel output, blank;
 private Container container;
 private String operation;
 private double number1, number2, result;
 private boolean clear = false;
 
 //GUI
 public void init()
 {
   
   container = getContentPane();
   //Title
   //super("Calculator");
   JPanel container = new JPanel();   
   container.setLayout( new FlowLayout( FlowLayout.CENTER
) );
   
   output = new JLabel("");   
   output.setBorder(new MatteBorder(2,2,2,2,Color.gray));
   output.setPreferredSize(new Dimension(1,26));   
   getContentPane().setBackground(Color.white);   
   getContentPane().add( "North",output );   
   getContentPane().add( "Center",container );
   
   //blank
   blank = new JLabel( "                    " );
   container.add( blank );
   
   //clear
   clear = new JButton( "CE" );
   clear.addActionListener(this);
   container.add( clear );
   
   //seven
   seven = new JButton( "7" );
   seven.addActionListener(this);
   container.add( seven );

   
   //eight
   eight = new JButton( "8" );
   eight.addActionListener(this);
   container.add( eight );
   
   //nine
   nine = new JButton( "9" );
   nine.addActionListener(this);
   container.add( nine );
   
   //div
   div = new JButton( "/" );
   div.addActionListener(this);
   container.add( div );
   
   //four
   four = new JButton( "4" );
   four.addActionListener(this);
   container.add( four );
   
   //five
   five = new JButton( "5" );
   five.addActionListener(this);
   container.add( five );
   
   //six
   six = new JButton( "6" );
   six.addActionListener(this);
   container.add( six );
   
   //mult
   mult = new JButton( "*" );
   mult.addActionListener(this);
   container.add( mult );
   
   //one
   one = new JButton( "1" );
   one.addActionListener(this);
   container.add( one );
   
   //two
   two = new JButton( "2" );
   two.addActionListener(this);
   container.add( two );
   
   //three
   three = new JButton( "3" );
   three.addActionListener(this);
   container.add( three );
   
   //minus
   minus = new JButton( "-" );
   minus.addActionListener(this);
   container.add( minus );
   
   //zero
   zero = new JButton( "0" );
   zero.addActionListener(this);
   container.add( zero );
   
   //dec
   dec = new JButton( "." );
   dec.addActionListener(this);
   container.add( dec );
   
   //plus
   plus = new JButton( "+" );
   plus.addActionListener(this);
   container.add( plus );

   //mem
   mem = new JButton( "MEM" );
   mem.addActionListener(this);
   container.add( mem );    

   //mrc
   mrc = new JButton( "MRC" );
   mrc.addActionListener(this);
   container.add( mrc );

   //sin
   sin = new JButton( "SIN" );
   sin.addActionListener(this);
   container.add( sin );

   //cos
   cos = new JButton( "COS" );
   cos.addActionListener(this);
   container.add( cos );
 
   //tan
   tan = new JButton( "TAN" );
   tan.addActionListener(this);
   container.add( tan );
   
   //asin
   asin = new JButton( "ASIN" );
   asin.addActionListener(this);
   container.add( asin );

   //acos
   acos = new JButton( "ACOS" );
   cos.addActionListener(this);
   container.add( cos );
 
   //atan
   atan = new JButton( "ATAN" );
   atan.addActionListener(this);
   container.add( atan );

   //x2
   x2 = new JButton( "X2" );
   x2.addActionListener(this);
   container.add( x2 );

   //sqrt
   sqrt = new JButton( "SQRT" );
   sqrt.addActionListener(this);
   container.add( sqrt );

   //exp
   exp = new JButton( "EXP" );
   exp.addActionListener(this);
   container.add( exp );

   //pi
   pi = new JButton( "PI" );
   pi.addActionListener(this);
   container.add( pi );

   //percent
   percent = new JButton( "%" );
   percent.addActionListener(this);
   container.add( percent );

   //eq
   eq = new JButton( "=" );
   eq.addActionListener(this);
   container.add( eq );

   //Set size and visible
   setSize( 190, 285 );
   setVisible( true );
 }
 

public static void main(String args[]){
   //execute applet as application

    //applet's window
    JFrame applicationWindow = new JFrame("calculator");
   
applicationWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //applet instance
    calculator appletObject = new calculator();
   
    //init and start methods
    appletObject.init();
    appletObject.start();

 } // end main

 public void actionPerformed(ActionEvent ae)
 {   
   JButton but = ( JButton )ae.getSource();   

   //dec action
   if( but.getText() == "." )
   {
     //if dec is pressed, first check to make shure there
is not already a decimal
     String temp = output.getText();
     if( temp.indexOf( '.' ) == -1 )
       output.setText( output.getText() + but.getText() );
   }
   
   //clear action
   else if( but.getText() == "CE" )
   {
     output.setText( "" );
     operation = "";
     number1 = 0.0;
     number2 = 0.0;
   }
   
   //plus action
   else if( but.getText() == "+" )
   {
     operation = "+";
     number1 = Double.parseDouble( output.getText() );
     clear = true;
     //output.setText( "" );
   }
   
   //minus action
   else if( but.getText() == "-" )
   {
     operation = "-";
     number1 = Double.parseDouble( output.getText() );
     clear = true;
     //output.setText( "" );
   }
   
   //mult action
   else if( but.getText() == "*" )
   {
     operation = "*";
     number1 = Double.parseDouble( output.getText() );
     clear = true;
     //output.setText( "" );
   }
   
   //div action
   else if( but.getText() == "/" )
   {
     operation = "/";
     number1 = Double.parseDouble( output.getText() );
     clear = true;
     //output.setText( "" );
   }
   
   //eq action
   else if( but.getText() == "=" )
   {
     number2 = Double.parseDouble( output.getText() );
     if( operation == "+" )
       result = number1 + number2;
     else if( operation == "-" )
       result = number1 - number2;
     else if( operation == "*" )
       result = number1 * number2;
     else if( operation == "/" )
       result = number1 / number2;        

     //output result
     output.setText( String.valueOf( result ) );
     
     clear = true;
     operation = "";
   }
   

   //default action
   else
   {
     if( clear == true )
     {
       output.setText( "" );
       clear = false;
     }
     output.setText( output.getText() + but.getText() );
   }
 }
 
}
[/code]
hiwa - 14 Dec 2003 01:50 GMT
> Hi everybody,
>
[quoted text clipped - 4 lines]
> applet, the buttons don't appear in order, how can I fix
> that?
(1)Don't use FlowLayout for a complex GUI. Use BoxLayout instead.
(2)Don't give a same name for different variables ... 'container' for
ContentPane and JPanel.
(3)Don't give a same name for different objects ... 'clear' for a
button and a boolean.
(4)You don't need to give names to buttons because they are not used.
(5)Also, you don't need operator variable because you can use the
symbol on the button.
(6)Catch exception for each parseXxxx() method.
(7)Supprt continuous calculation.

Below is a small amount of brush-up, not a radical refactoring.
<code>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class calculator extends JApplet implements ActionListener{
 private JButton one, two, three, four, five, six, seven, eight,
                 nine, zero, dec, eq, plus, minus, mult, div, clear,
                 mem, mrc, sin, cos, tan, asin, acos, atan, x2, sqrt,
exp,
                 pi, percent;
 private JLabel output, blank;
 private Container contentP;
 private String operation;
 private double number1, number2, result;
 private boolean cleared = false;
 
 public void init(){
   contentP = getContentPane();
   JPanel container = new JPanel();
   container.setLayout(new FlowLayout(FlowLayout.CENTER));
   
   output = new JLabel("");
   output.setBorder(new MatteBorder(2,2,2,2,Color.gray));
   output.setPreferredSize(new Dimension(1,26));
   contentP.setBackground(Color.white);
   contentP.add("North",output);
   contentP.add("Center",container);
   
   blank = new JLabel("                    ");
   container.add(blank);
   
   clear = new JButton("CE");
   clear.addActionListener(this);
   container.add(clear);
   
   seven = new JButton("7");
   seven.addActionListener(this);
   container.add(seven);

   eight = new JButton("8");
   eight.addActionListener(this);
   container.add(eight);
   
   nine = new JButton("9");
   nine.addActionListener(this);
   container.add(nine);
   
   div = new JButton("/");
   div.addActionListener(this);
   container.add(div);
   
   four = new JButton("4");
   four.addActionListener(this);
   container.add(four);
   
   five = new JButton("5");
   five.addActionListener(this);
   container.add(five);
   
   six = new JButton("6");
   six.addActionListener(this);
   container.add(six);
   
   mult = new JButton("*");
   mult.addActionListener(this);
   container.add(mult);
   
   one = new JButton("1");
   one.addActionListener(this);
   container.add(one);
   
   two = new JButton("2");
   two.addActionListener(this);
   container.add(two);
   
   three = new JButton("3");
   three.addActionListener(this);
   container.add(three);
   
   minus = new JButton("-");
   minus.addActionListener(this);
   container.add(minus);
   
   zero = new JButton("0");
   zero.addActionListener(this);
   container.add(zero);
   
   dec = new JButton(".");
   dec.addActionListener(this);
   container.add(dec);
   
   plus = new JButton("+");
   plus.addActionListener(this);
   container.add(plus);

   mem = new JButton("MEM");
   mem.addActionListener(this);
   container.add(mem);    

   mrc = new JButton("MRC");
   mrc.addActionListener(this);
   container.add(mrc);

   sin = new JButton("SIN");
   sin.addActionListener(this);
   container.add(sin);

   cos = new JButton("COS");
   cos.addActionListener(this);
   container.add(cos);
 
   tan = new JButton("TAN");
   tan.addActionListener(this);
   container.add(tan);
   
   asin = new JButton("ASIN");
   asin.addActionListener(this);
   container.add(asin);

   acos = new JButton("ACOS");
   cos.addActionListener(this);
   container.add(cos);
 
   atan = new JButton("ATAN");
   atan.addActionListener(this);
   container.add(atan);

   x2 = new JButton("X2");
   x2.addActionListener(this);
   container.add(x2);

   sqrt = new JButton("SQRT");
   sqrt.addActionListener(this);
   container.add(sqrt);

   exp = new JButton("EXP");
   exp.addActionListener(this);
   container.add(exp);

   pi = new JButton("PI");
   pi.addActionListener(this);
   container.add(pi);

   percent = new JButton("%");
   percent.addActionListener(this);
   container.add(percent);

   eq = new JButton("=");
   eq.addActionListener(this);
   container.add(eq);

   setSize(190, 285);
   setVisible(true);
 } // init()

 public static void main(String args[]){
   JFrame applicationWindow = new JFrame("calculator");
   applicationWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   calculator appletObject = new calculator();
   applicationWindow.getContentPane().add(appletObject,
BorderLayout.CENTER);
   applicationWindow.setBounds(0,0, 500, 200);
   applicationWindow.setVisible(true);
   appletObject.init();
   appletObject.start();
 } // main()

 public void actionPerformed(ActionEvent ae){
   JButton but = (JButton)ae.getSource();
   if(but.getText() == "."){
     String temp = output.getText();
     if(temp.indexOf('.') == -1){
       output.setText(output.getText() + but.getText());
     }
   }
   else if(but.getText() == "CE"){
     output.setText("");
     operation = "";
     number1 = 0.0;
     number2 = 0.0;
   }
   else if(but.getText() == "+"){
     operation = "+";
     number1 = Double.parseDouble(output.getText());
     cleared = true;
   }
   
   else if(but.getText() == "-"){
     operation = "-";
     number1 = Double.parseDouble(output.getText());
     cleared = true;
   }
   
   else if(but.getText() == "*"){
     operation = "*";
     number1 = Double.parseDouble(output.getText());
     cleared = true;
   }

   
   else if(but.getText() == "/"){
     operation = "/";
     number1 = Double.parseDouble(output.getText());
     cleared = true;
   }
   
   else if(but.getText() == "="){
     number2 = Double.parseDouble(output.getText());
     if(operation == "+"){
       result = number1 + number2;
     }
     else if(operation == "-"){
       result = number1 - number2;
     }
     else if(operation == "*"){
       result = number1 * number2;
     }
     else if(operation == "/"){
       result = number1 / number2;        
     }
     output.setText(String.valueOf(result));
     cleared = true;
     operation = "";
   }
   
   //for number buttons default action
   else{
     if(cleared == true){
       output.setText("");
       cleared = false;
     }
     output.setText(output.getText() + but.getText());
   }
 }
}
</code
hiwa - 14 Dec 2003 03:09 GMT
(X)Don't use == operator for String comparison. Use equals() method instead.

<code>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class calculator extends JApplet implements ActionListener{
 private JButton[] buttons;
 private String[] ops = {"CE", "/", "*", "-", ".", "+", "MEM", "MRC",
   "SIN", "TAN", "ASIN", "COS","ATAN","X","SQRT","EXP","PI",
   "%", "="};
 private JLabel output, blank;
 private Container contentP;
 private String operation;
 private double number1, number2, result;
 private boolean cleared = false;
 
 public void init(){
   contentP = getContentPane();
   JPanel container = new JPanel();
   container.setLayout(new FlowLayout(FlowLayout.CENTER));
   output = new JLabel("");
   output.setBorder(new MatteBorder(2, 2, 2, 2, Color.gray));
   output.setPreferredSize(new Dimension(1, 26));
   contentP.setBackground(Color.white);
   contentP.add("North", output);
   contentP.add("Center", container);
   
   blank = new JLabel("                    ");
   container.add(blank);
   
   buttons = new JButton[29];
   for (int i = 0; i < 10; ++i){
     buttons[i] = new JButton((new Integer(i)).toString());
   }
   for (int i = 10; i < buttons.length; ++i){
     buttons[i] = new JButton(ops[i - 10]);
   }
   for (int i = 0; i < buttons.length; ++i){
     buttons[i].addActionListener(this);
   }
   JButton[] alignButtons = new JButton[29];
   alignButtons[0] = buttons[10]; // CE
   alignButtons[4] = buttons[11]; // /
   alignButtons[8] = buttons[12]; // *
   alignButtons[12] = buttons[13]; // -
   alignButtons[1] = buttons[7]; // 7
   alignButtons[2] = buttons[8]; // 8
   alignButtons[3] = buttons[9]; // 9
   alignButtons[5] = buttons[4]; // 4
   alignButtons[6] = buttons[5]; // 5
   alignButtons[7] = buttons[6]; // 6
   alignButtons[9] = buttons[1]; // 1
   alignButtons[10] = buttons[2]; // 2
   alignButtons[11] = buttons[3]; // 3
   alignButtons[13] = buttons[0]; // 0
   for (int i = 14; i < buttons.length; ++i){
     alignButtons[i] = buttons[i];
   }
   addButtons(alignButtons, container);
 } // init()

 void addButtons(JButton[] btns, Container cont){

   for (int i = 0; i < btns.length; ++i){
     cont.add(btns[i]);
   }
 }

 public static void main(String args[]){
   JFrame applicationWindow = new JFrame("calculator");
   applicationWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   calculator appletObject = new calculator();
   applicationWindow.getContentPane().add(appletObject, BorderLayout.CENTER);
   applicationWindow.setBounds(0,0, 500, 200);
   applicationWindow.setVisible(true);
   appletObject.init();
   appletObject.start();
 } // main()

 public void actionPerformed(ActionEvent ae){
   JButton but = (JButton)ae.getSource();
   String op = but.getText();
   
   if (op.equals(".")){
     String temp = output.getText();
     if(temp.indexOf('.') == -1){
       output.setText(output.getText() + op);
     }
   }
   else if(op.equals("CE")){
     output.setText("");
     operation = "";
     number1 = 0.0;
     number2 = 0.0;
   }
   else if("+-*/".indexOf(op) != -1){
     operation = op;
     try{
       number1 = Double.parseDouble(output.getText());
     }
     catch(Exception e){
       e.printStackTrace();
       number1 = 0.0;
     }
     finally{
       cleared = true;
     }
   }
   else if(op.equals("=")){
     try{
       number2 = Double.parseDouble(output.getText());
     }
     catch(Exception e){
       e.printStackTrace();
       number2 = 0.0;
     }
     if(operation.equals("+")){
       result = number1 + number2;
     }
     else if(operation.equals("-")){
       result = number1 - number2;
     }
     else if(operation.equals("*")){
       result = number1 * number2;
     }
     else if(operation.equals("/")){
       result = number1 / number2;        
     }
     output.setText(String.valueOf(result));
     cleared = true;
     operation = "";
   }
   else{ //for number buttons only, others are not implemented
     if(cleared == true){
       output.setText("");
       cleared = false;
     }
     output.setText(output.getText() + but.getText());
   }
 }
}
</code
Andrew Thompson - 14 Dec 2003 04:26 GMT
> Hi everybody,
>
> I designed a calculator, and I need help with the rest of
> the actions.

I don't know if this will help, but I have
a complete calculator in a panel that I
add to both applets and frames.
http://www.physci.org/codes/display.jsp?fl=%2FPhySci.jar%21%2Fphysci%2Ftoolb
ox%2Fcalculator%2FCalculatorPanel.java

You can see the application code here..
http://www.physci.org/codes/display.jsp?fl=%2FPhySci.jar%21%2Fphysci%2Ftoolb
ox%2Fcalculator%2FCalculator.java

and the applet here..
http://www.physci.org/codes/display.jsp?fl=%2FPhySci.jar%21%2FCalculet.java

Here is the calculator in action..
http://www.physci.org/applet/Calculet.htm

Please note various things
1)  You should not come in here people to email
things to you.  This is a public forum for the
benefit of all - not just you.

2)  Do not tell me about your deadlines -
they are your problem and you don't pay me
enough to care.

3) The code above is useful for demonstrative
puposes but is intened to help you only.  Sumbit
it and you will almost certainly fail and be dismissed
for academic misconduct - this code is well known.

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
Antti S. Brax - 15 Dec 2003 06:29 GMT
laurare@rocketmail.com wrote in comp.lang.java.gui:
> Hi everybody,
>
> I designed a calculator,

    Nope. You just copied the design from a handheld calculator.

    You have (at least) 800x600 pixels, 104 keys and a mouse
    available and you don't have to fit your device on a human
    palm. Do some GUI design and use the resources you have.

Signature

Antti S. Brax - asb(at)iki.fi   Rullalautailu pitää lapset poissa ladulta
  http://www.iki.fi/asb/          http://www.cs.helsinki.fi/u/abrax/hlb/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.