السلام عليكم ورحمة الله وبركاته
وتحية طيبة للجميع
أنا عندي هذا البرنامج المكتوب بالجافا لعمل آلة حاسبة ، وقد عملت كل شيء بإسثناء الارقام السالبة
هذا الكود
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import static java.lang.Math.abs;
public class Calculation extends JFrame implements ActionListener
{
JTextField resultField;
// to define the two numbers to be used in differn operations
double operandOne;
double operandTwo;
int operator;
/* to define the basic features of the Calculator wihich include equal and the four basic operatins:
addition,subtraction,muliplication and divistion*/
JButton equalsButton;
JButton subtractButton;
JButton plusButton;
JButton multiButton;
JButton dividButton;
// to define the full features which include negaitves and floating numbers and candelbutton to clear the display
JButton cancelButton;
JButton dutButton;
JButton signButton;
// define advanced features which include
JButton MRButton;
JButton rootButton;
JButton BackspaceButton;
JButton MButton;
JButton SQRButton;
JButton dvbyoneButton;
JButton moodButton;
// to define the numbers from 0 to 9
JButton zero;
JButton one;
JButton two;
JButton three;
JButton four;
JButton five;
JButton six;
JButton seven;
JButton eight;
JButton nine;
Object source;
// the main method that going to be executed
public static void main(String[] args)
{
// to instantiation the class, to make the you own application which is calculator application
Calculation app= new Calculation();
// to implement the listner in the frame
app.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
// theses are dimentions to set the size of the application
app.setSize(450, 300);
// you have to be true , otherwise can not see it
app.setVisible(true);
}// end of main method
public Calculation()
{
// run the super class of class calcultaorAppliatoin
super();
//the result panle -------( in documentation) is an abtracation , to define the structor of output // you add things to the panel
JPanel operandPanel;
JPanel buttonPanel1;//
JPanel buttonPanel2;//
JPanel buttonPanel3;//
JPanel buttonPanel4;
Container cp;
// get any input that we are going to give it
cp = getContentPane();
// we have 5 rows and 1 panel
cp.setLayout(new GridLayout(5,1));
operandPanel = new JPanel();
operandPanel.setLayout(new FlowLayout());
resultField = new JTextField("0", 23);
resultField.setHorizontalAlignment(SwingConstants.RIGHT);
operandPanel.add(resultField);
cp.add(operandPanel);
//PANEL 1 Buttons
buttonPanel1 = new JPanel();
buttonPanel1.setLayout(new FlowLayout());
//to add number seven
seven = new JButton(" 7 ");
seven.addActionListener(this);
buttonPanel1.add(seven);
//to add button number eith
eight = new JButton(" 8 ");
eight.addActionListener(this);
buttonPanel1.add(eight);
//to add button number nine
nine = new JButton(" 9 ");
nine.addActionListener(this);
buttonPanel1.add(nine);
//to add divistion calculation
dividButton = new JButton(" / ");
dividButton.addActionListener(this);
buttonPanel1.add(dividButton);
//to add cancel button the operation
cancelButton = new JButton(" C ");
cancelButton.addActionListener(this);
buttonPanel1.add(cancelButton);
//to add backspace button withic will go one space back on the screen
BackspaceButton = new JButton("Backspace");
BackspaceButton.addActionListener(this);
buttonPanel1.add(BackspaceButton);
cp.add(buttonPanel1);
// end of Panel 1 buttons
//PANEL 2 Buttons
buttonPanel2 = new JPanel();
buttonPanel2.setLayout(new FlowLayout());
//to add button number four
four = new JButton(" 4 ");
four.addActionListener(this);
buttonPanel2.add(four);
//to add button number five
five = new JButton(" 5 ");
five.addActionListener(this);
buttonPanel2.add(five);
//to add button noumber six
six = new JButton(" 6 ");
six.addActionListener(this);
buttonPanel2.add(six);
//to add Multiplication calculation
multiButton = new JButton(" * ");
multiButton.addActionListener(this);
buttonPanel2.add(multiButton);
//to add memory button wthich will save the number into the memory
MButton = new JButton(" M ");
MButton.addActionListener(this);
buttonPanel2.add(MButton);
//to add button square root which will multiple the number in itself
SQRButton = new JButton(" SQR ");
SQRButton.addActionListener(this);
buttonPanel2.add(SQRButton);
cp.add(buttonPanel2);
//end of Panel 2 buttons
//PANEL 3 Buttons
buttonPanel3 = new JPanel();
buttonPanel3.setLayout(new FlowLayout());
//to add button number one
one = new JButton(" 1 ");
one.addActionListener(this);
buttonPanel3.add(one);
//to add button number two
two = new JButton(" 2 ");
two.addActionListener(this);
buttonPanel3.add(two);
//to add button number three
three = new JButton(" 3 ");
three.addActionListener(this);
buttonPanel3.add(three);
//to add button subtractButton
subtractButton = new JButton(" - ");
subtractButton.addActionListener(this);
buttonPanel3.add(subtractButton);
//to add button MR which will call the number that saved on the memory
MRButton = new JButton(" MR ");
MRButton.addActionListener(this);
buttonPanel3.add(MRButton);
//to add sign button either plus or mius
signButton = new JButton(" +/- ");
signButton.addActionListener(this);
buttonPanel3.add(signButton);
cp.add(buttonPanel3);
//end of Panel 3 buttons
//PANEL 4 Buttons
buttonPanel4 = new JPanel();
buttonPanel4.setLayout(new FlowLayout());
//to add button number zero
zero = new JButton(" 0 ");
zero.addActionListener(this);
buttonPanel4.add(zero);
//to add button no dut button whcih means folating numbers
dutButton = new JButton(" . ");
dutButton.addActionListener(this);
buttonPanel4.add(dutButton);
//to add mood button
moodButton = new JButton(" % ");
moodButton.addActionListener(this);
buttonPanel4.add(moodButton);
//to add plus buuton
plusButton = new JButton(" + ");
plusButton.addActionListener(this);
buttonPanel4.add(plusButton);
// to add euqal button
equalsButton = new JButton(" = ");
equalsButton.addActionListener(this);
buttonPanel4.add(equalsButton);
// to add root button
dvbyoneButton = new JButton(" 1/X ");
dvbyoneButton.addActionListener(this);
buttonPanel4.add(dvbyoneButton);
cp.add(buttonPanel4);
//end of Panel 4 buttons
} //end of Calculation method
public void showResult()
{
boolean validOperands;
DecimalFormat df = new DecimalFormat("0.00");
try
{
// operandOne = Double.parseDouble(opersndOneField.getText());
// operandTwo = Double.parseDouble(operandTwoField.getText());
validOperands = true;
}
catch(NumberFormatException nfe)
{
validOperands = false;
}
if (validOperands)
{
// to do addition operation
if (source == plusButton)
{
operandOne = Double.parseDouble(resultField.getText());
resultField.setText("0");
operator = 1;
}
// to do subtraction operation
if (source == subtractButton) // to make sub operation
{
operandOne = Double.parseDouble(resultField.getText());
resultField.setText("0");
operator = 2;
}
// to do multiplication operation
if (source == multiButton)// to make
{
operandOne = Double.parseDouble(resultField.getText());
resultField.setText("0");
operator = 3;
}
// to do dvision operation
if (source == dividButton)
{
operandOne = Double.parseDouble(resultField.getText());
resultField.setText("0");
operator = 4;
}
if (source == moodButton)
{
operandOne = Double.parseDouble(resultField.getText());
resultField.setText("0");
operator = 5;
}
if (source == SQRButton)
{
operandOne = Double.parseDouble(resultField.getText());
resultField.setText(Double.toString(operandOne * operandOne));//resultField.setText("0");
//operator = 6;
}
if (source == rootButton)
{
operandOne = Double.parseDouble(resultField.getText());
resultField.setText("0");
operator = 7;
//result = Math.sqrt(getNumberInDisplay());
}
if (source == dvbyoneButton)
{
operandOne = Double.parseDouble(resultField.getText());
resultField.setText(Double.toString(1/operandOne));
}
if (source == MButton)
{
operandOne = Double.parseDouble(resultField.getText());
resultField.setText(Double.toString(operandOne));
resultField.setText("0");
}
if (source == MRButton)
{
resultField.setText(Double.toString(operandOne));
}
else if (source == dutButton) {
if (resultField.getText().equals("0"))
resultField.setText("0.");
else if (resultField.getText () .indexOf ('.') == -1){
resultField.setText(resultField.getText() + ".");
}
}
if (source == signButton)
{
resultField.setText(Double.toString(operandOne * -1));
//System.out.println(abs(-1));
//resultField.setText(abs(-1));
}
else if (source == equalsButton)
{
operandTwo = Double.parseDouble(resultField.getText());
switch(operator)
{
case 1 :
resultField.setText(Double.toString(operandOne + operandTwo));
operandOne = 0.0;
operandTwo = 0.0;
break;
case 2:
resultField.setText(Double.toString(operandOne - operandTwo));
operandOne = 0.0;
operandTwo = 0.0;
break;
case 3:
resultField.setText(Double.toString(operandOne * operandTwo));
operandOne = 0.0;
operandTwo = 0.0;
break;
case 4:
if (operandTwo !=0.0){
resultField.setText(Double.toString(operandOne/operandTwo));
operandOne = 0.0;
operandTwo = 0.0; }
else {
resultField.setText("0");}
break;
case 5:
resultField.setText(Double.toString(operandOne % operandTwo));
operandOne = 0.0;
operandTwo = 0.0;
/* case 10: // +/-
processSignChange();
break;*/
}
}
// to print out number zero on the screen after the user press on zero button
else if (source == zero)
{
if (resultField.getText().equals("0"))
resultField.setText("0");
else
resultField.setText(resultField.getText() + "0");
}
// to print out number one on the screen after the user press on one button
else if (source == one)
{
if (resultField.getText().equals("0"))
resultField.setText("1");
else
resultField.setText(resultField.getText() + "1");
}
// to print out number two on the screen after the user press on two button
else if (source == two)
{
if (resultField.getText().equals("0"))
resultField.setText("2");
else
resultField.setText(resultField.getText() + "2");
}
//to print out number three on the screen after the user press on three button
else if (source == three)
{
if (resultField.getText().equals("0"))
resultField.setText("3");
else
resultField.setText(resultField.getText() + "3");
}
//to print out number four on the screen after the user press on four button
else if (source == four)
{
if (resultField.getText().equals("0"))
resultField.setText("4");
else
resultField.setText(resultField.getText() + "4");
}
//to print out number five on the screen after the user press on zero five
else if (source == five)
{
if (resultField.getText().equals("0"))
resultField.setText("5");
else
resultField.setText(resultField.getText() + "5");
}
//to print out number six on the screen after the user press on six button
else if (source == six)
{
if (resultField.getText().equals("6"))
resultField.setText("6");
else
resultField.setText(resultField.getText() + "6");
}
//to print out number seven on the screen after the user press on seven button
else if (source == seven)
{
if (resultField.getText().equals("0"))
resultField.setText("7");
else
resultField.setText(resultField.getText() + "7");
}
//to print out number eight on the screen after the user press on eight button
else if (source == eight)
{
if (resultField.getText().equals("0"))
resultField.setText("8");
else
resultField.setText(resultField.getText() + "8");
}
//to print out number eight on the screen after the user press on eight button
else if (source == nine)
{
if (resultField.getText().equals("0"))
resultField.setText("9");
else
resultField.setText(resultField.getText() + "9");
}
else if (source == cancelButton)
{
resultField.setText("0");
}
}
else
resultField.setText("Invalid operand");
}// end of showResult method
public void actionPerformed(ActionEvent ae)
{
source = ae.getSource();
showResult();
} //end of actionPerformed method
}// end of classمن له خبرة ، أرجو مساعدتي
وشكراً