URGENT Help With Scientific Calculator!

M

Maria Laura Re

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 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
(e-mail address removed).

Below is the code for the calcualtor.

Thanks a lot!

-Maria

// 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() );
}
}

}
 
R

Roedy Green

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
(e-mail address removed).

see http://mindprod.com/jgloss/homework.html

The job is not to get the assignment finished. The job is to get you
understanding Java. If the assignment gets done without learning Java
you will have just wasted your money you spent on your school.

You sound like you are trying to subvert your own intention to learn.
 
M

Maria Laura Re

You sound like you are trying to subvert your own intention to learn.

You're right, I've tried for months to do this program and I can't get
it to work, that's all I could get than. I feel like I've already
wasted time in this school. I don't know if it's me, the instructor
or what, but I can't do it. I'm not asking anybody to do this for me,
just a little help, though I wouldn't mind if somebody would finish
it, because as I said I'm really frustrated, and don't know what else
to do. I need to graduate somehow, and I know Java is not something
that I'm gonna be using in the future, I'm more an artist than a
programmer, I prefer to work with Flash, Dreamweaver, Illustrator,
Photoshop.

So if you can help me that would be great, if not, I appreciate your
concern anyway.

-Maria
 
A

Andrew Thompson

Maria Laura Re said:
You're right, I've tried for months to do this program and I can't get
it to work, that's all I could get than.

Why were you not here 'months ago'?
..I feel like I've already
wasted time in this school.

And a place that could be filled by someone else,
someone that might not feel the school is a waste
of time.
..I don't know if it's me, the instructor
or what, but I can't do it. I'm not asking anybody to do this for me,

It does not sound that way to most people
who have responded to you.
just a little help, though I wouldn't mind if somebody would finish
it, because as I said I'm really frustrated, and don't know what else
to do.

Ask smart questions, put the study in,
put the work in, don't leave things till
the last minute. First four things I could
think of to help you.
..I need to graduate somehow, and I know Java is not something
that I'm gonna be using in the future,

Then why are you wasting both our time
and that of the school?
..I'm more an artist than a
programmer, I prefer to work with Flash, Dreamweaver, Illustrator,
Photoshop.

So get in a course that deals with them..
So if you can help me that would be great, if not, I appreciate your
concern anyway.

You have already received help in the
question you multi-posted to c.l.j.gui.

Stop whingeing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top