![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2004
Posts: 4
Rep Power: 0
![]() |
i have just had caLcuLator homework in java..the caLcuLator and the buttons appear on the screen..the probLem is that..it doesnt do any caLcuLations..what do i have to write it with inorder to do multipLication,addition,etc..if someone heLps me i wiLL be very happy..
thanx..^_^ |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
If we could see some code, it'd be helpful. We don't know what the problem is until we see it.
|
|
|
|
|
|
#3 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
I agree. We're going to need to see some code. Have you parsed everything before trying to make the calculations?
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#4 |
|
Programmer
|
The code will be beneficial. Here is a basic guideline that you may be able to work around though.
1. The buttons will be swing I am sure. In this case you give each button an action event that calls a method in the class. example: addJButtonActionEvent{ int aa = Integer.parseInt(num1JTextField.getText()); int ab = Integer.parseInt(num2JTextField.getText()); int answer = aa + ab; outputJTextArea.setText(String.valueOf(answer)); } If the GUI is done then the Action events are probably coded also. Your job will be to see what method they call and make it according to haw the GUI layout is made.
__________________
Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration. -S. Kelley |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Dec 2004
Posts: 4
Rep Power: 0
![]() |
this is the appLet code that i have written so far..it is not compLete yet..but the main part of it is compLeted i guess..how can i add some functions?(addition,subtraction,..)just simpLe 1..where do i have to write those functions?..how shouLd i write it..thanx ^_^
code is
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JApplet implements ActionListener{
JLabel no0Label,no1Label,no2Label,no3Label,no4Label,no5Label;
JLabel no6Label,no7Label,no8Label,no9Label;
JLabel sumLabel,subtractLabel,multiplyLabel,divideLabel;
JLabel equalLabel,pointLabel,clearLabel;
JTextField display;
JButtonno0Button,no1Button,no2Button,no3Button,no4Button;
JButton no5Button;no6Button,no7Button,no8Button,no9Button;
JButton sumButton,subtractButton,multiplyButton,divideButton;
JButton equalButton,pointButton,clearButton;
double initialNumber;
double currentNumber = 0;
int decimalPlaces = 0;
public void init()
{
Container container=getContentPane();
container.setLayout(new FlowLayout());
display = new JTextField(20);
container.add (display);
no0Button=new JButton("0");
no0Button.addActionListener(this);
container.add(no0Button);
no1Button=new JButton("1");
no1Button.addActionListener(this);
container.add(no1Button);
no2Button=new JButton("2");
no2Button.addActionListener(this);
container.add(no2Button);
no3Button=new JButton("3");
no3Button.addActionListener(this);
container.add(no3Button);
no4Button=new JButton("4");
no4Button.addActionListener(this);
container.add(no4Button);
no5Button=new JButton("5");
no5Button.addActionListener(this);
container.add(no5Button);
no6Button=new JButton("6");
no6Button.addActionListener(this);
container.add(no6Button);
no7Button=new JButton("7");
no7Button.addActionListener(this);
container.add(no7Button);
no8Button=new JButton("8");
no8Button.addActionListener(this);
container.add(no8Button);
no9Button=new JButton("9");
no9Button.addActionListener(this);
container.add(no9Button);
sumButton=new JButton("+");
sumButton.addActionListener(this);
container.add(sumButton);
subtractButton=new JButton("-");
subtractButton.addActionListener(this);
container.add(subtractButton);
multiplyButton=new JButton("*");
multiplyButton.addActionListener(this);
container.add(multiplyButton);
divideButton=new JButton("/");
divideButton.addActionListener(this);
container.add(divideButton);
equalButton=new JButton("=");
equalButton.addActionListener(this);
container.add(equalButton);
pointButton=new JButton(".");
pointButton.addActionListener(this);
container.add(pointButton);
clearButton=new JButton("DEL");
clearButton.addActionListener(this);
container.add(clearButton);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==no0Button){
display.setText ("0");
}
if(e.getSource()==no1Button){
display.setText("1");
}
if(e.getSource()==no2Button){
display.setText("2");
}
if(e.getSource()==no3Button){
display.setText("3");
}
if(e.getSource()==no4Button){
display.setText("4");
}
if(e.getSource()==no5Button){
display.setText("5");
}
if(e.getSource()==no6Button){
display.setText("6");
}
if(e.getSource()==no7Button){
display.setText("7");
}
if(e.getSource()==no8Button){
display.setText("8");
}
if(e.getSource()==no9Button){
display.setText("9");
}
if(e.getSource() == sumButton) {
display.setText("+");
}
if(e.getSource() == subtractButton) {
display.setText("-");
}
if(e.getSource() == multiplyButton) {
display.setText("*");
}
if(e.getSource() == divideButton) {
display.setText("/");
}
}
} |
|
|
|
|
|
#6 |
|
Programmer
|
If you could post what you attempted that would be great. I will only help on homework. To do it costs money.
But, seriously post your attempt and we will tweak it and lead you in the right direction. Where I would go first is I would create some variables outside your method (right after the declaration of the Swing components. This will allow them to be modified in other methods. Next stor the results inputted by the user into these. This could easily be done when they are being displayed on screen. Just to be safe and to make things easier I would put them all in the double type. It looked like your going to have to use decimals. See if that gets you going. Come back if you are stuck.
__________________
Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration. -S. Kelley |
|
|
|
|
|
#7 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Okay, so now what you need to do is get the text from display and parse the number on the left side of the arithmetic operator into an integer variable, and parse the number on the right side of the arithmetic operator into a seperate integer variable. You're going to need to split the string to do this. Check out the split() method in the Javadocs under the String class. Now, to get a String variable to an integer variable, you use the method parseInt() under the Integer class. Again, Javadocs could explain it better than I could. If you need any more help, let me know. I haven't run the code yet, I will in a second though.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|