cambeme
Giới tính : Cung : Con giáp : Bài gửi : 174 Điểm : 5271 Thanks : 0 Sinh nhật : 05/12/1991 Ngày gia nhập : 25/03/2011 Tuổi : 32 Đến từ : Thành phố Hồ Chí minh Nghề nghiệp/ước mơ : IT Tâm trạng : Hưng phắn Châm ngôn sống : Không có gì quý hơn hột vịt thịt kho!
Cao nhân tắt thở vô phương trị!
| Tiêu đề: Assignment 02: Simple Calculating 18/10/2011, 23:50 | |
| | 18/10/2011, 23:50 Assignment 02: Simple Calculating | | | Bài 1: - Code:
-
import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField;
/* * author Anh Trinh (T094054) */ public class cComputingSquareRoot_T094054 extends JFrame { // declare labels JLabel lblSource = new JLabel("Source:"); JLabel lblErrors = new JLabel("Choose Errors:"); // declare text field JTextField txtInput = new JTextField(); // declare text area JTextArea txtResult = new JTextArea(); // declare combo box JComboBox cmbChoose = new JComboBox(); // declare buttons JButton btnCompute = new JButton("Compute"); JButton btnClear = new JButton("Clear"); JButton btnClearall = new JButton("Clear all"); // declare scroll pane JScrollPane panResult = new JScrollPane(txtResult); // declare variable int i, j = 0; double input; String output; double d = 1.0;
/* * constructor */ public cComputingSquareRoot_T094054() { // set title setTitle("T094054 - Square Root"); // set size setSize(400, 265); // set location setLocationRelativeTo(null); // reset null layout setLayout(null); // set resizable setResizable(false); // add labels to window add(lblSource); add(lblErrors); // add text to window add(txtInput); // add scroll bar to window add(panResult); // add buttons to window add(btnCompute); add(btnClear); add(btnClearall); // add combo box to window add(cmbChoose); // add data to combo box using for function for (int i = 1; i <= 8; i++) { cmbChoose.addItem(i); }// end for // default chosen item cmbChoose.setSelectedItem(2); // set location lblSource.setBounds(10, 10, 60, 20); txtInput.setBounds(80, 10, 150, 20); lblErrors.setBounds(235, 10, 90, 20); cmbChoose.setBounds(330, 10, 50, 20); btnCompute.setBounds(70, 40, 80, 25); btnCompute.setMargin(new Insets(1, 1, 1, 1)); btnClear.setBounds(160, 40, 80, 25); btnClear.setMargin(new Insets(1, 1, 1, 1)); btnClearall.setBounds(250, 40, 80, 25); btnClearall.setMargin(new Insets(1, 1, 1, 1)); panResult.setBounds(10, 80, 375, 150); txtResult.setEditable(false); // add Action listener ActionListener method = new ActionListener() {
@Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub // declare variable String sTmp = txtInput.getText().trim().toString(); // calculate if (arg0.getSource() == btnCompute) { // try function try { input = Double.parseDouble(sTmp); // use if function to check the input data if (input > 0) { // check selected error double c = cmbChoose.getSelectedIndex(); // use while function to calculate error which is // selected while (j <= c) { d = d / 10; j++; }// end while // declare variable double x0 = 1; double x = (1 + input) / 2; // using while function to calculate square root while (x - x0 > d || x - x0 < (-d)) { x0 = x; x = (x0 + input / x0) / 2; }// end while output = "Sqrt(" + sTmp + ")= " + x + "\n"; d = 1.0; j = 0; }// end if else if (input < 0) { output = "Invalid Input" + "\n"; }// end if else if (input == 0) { output = "Sqrt(" + sTmp + ")= " + 0 + "\n"; }// End if }// end try catch (Exception e) { // TODO: handle exception output = "Invalid Input" + "\n"; }// end catch txtResult.append(output); }// end if else if (arg0.getSource() == btnClear) { txtInput.setText(""); }// end if else if (arg0.getSource() == btnClearall) { txtInput.setText(""); txtResult.setText(""); // default chosen item cmbChoose.setSelectedItem(2); }// end if }// end actionPerformed };// end actionListener // add actionListener to buttons btnCompute.addActionListener(method); btnClear.addActionListener(method); btnClearall.addActionListener(method); }// end constructor
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // new window cComputingSquareRoot_T094054 window = new cComputingSquareRoot_T094054(); // end program as closing window. window.setDefaultCloseOperation(EXIT_ON_CLOSE); // show the window window.setVisible(true);
}// end main function
}// end class Bài 2: - Code:
-
import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField;
/* * author Anh Trinh (T094054) */ public class cComputingSinFunction_T094054 extends JFrame { // declare labels JLabel lblSource = new JLabel("Source:"); JLabel lblErrors = new JLabel("Choose Errors:"); // declare text field JTextField txtInput = new JTextField(); // declare text area JTextArea txtResult = new JTextArea(); // declare combo box JComboBox cmbChoose = new JComboBox(); // declare buttons JButton btnCompute = new JButton("Compute"); JButton btnClear = new JButton("Clear"); JButton btnClearall = new JButton("Clear all"); // declare scroll pane JScrollPane panResult = new JScrollPane(txtResult); // declare variable int i, j = 0; double input; String output; double d = 1.0;
/* * constructor */ public cComputingSinFunction_T094054() { // set title setTitle("T094054 - Sin Function"); // set size setSize(400, 265); // set location setLocationRelativeTo(null); // reset null layout setLayout(null); // set resizable setResizable(false); // add labels to window add(lblSource); add(lblErrors); // add text to window add(txtInput); // add scroll bar to window add(panResult); // add buttons to window add(btnCompute); add(btnClear); add(btnClearall); // add combo box to window add(cmbChoose); // add data to combo box using for function for (int i = 1; i <= 8; i++) { cmbChoose.addItem(i); }// end for // default chosen item cmbChoose.setSelectedItem(2); // set location lblSource.setBounds(10, 10, 60, 20); txtInput.setBounds(80, 10, 150, 20); lblErrors.setBounds(235, 10, 90, 20); cmbChoose.setBounds(330, 10, 50, 20); btnCompute.setBounds(70, 40, 80, 25); btnCompute.setMargin(new Insets(1, 1, 1, 1)); btnClear.setBounds(160, 40, 80, 25); btnClear.setMargin(new Insets(1, 1, 1, 1)); btnClearall.setBounds(250, 40, 80, 25); btnClearall.setMargin(new Insets(1, 1, 1, 1)); panResult.setBounds(10, 80, 375, 150); txtResult.setEditable(false); // add Action listener ActionListener method = new ActionListener() {
@Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub // declare variable String sTmp = txtInput.getText().trim().toString(); // calculate if (arg0.getSource() == btnCompute) { // try function try { input = Double.parseDouble(sTmp); // check selected error double c = cmbChoose.getSelectedIndex(); // use while function to calculate error which is // selected while (j <= c) { d = d / 10; j++; }// end while // declare variable double x0 = 1; double x = 1; double y = input; int k = 2; // using if to check if -1< sinx <1 if (x >= (-1) && x <= 1) { x = input; // using while function to calculate sin while (x - x0 > d || x - x0 < (-d)) { x0 = x; y = (-1) * y * (input * input) / ((k + 1) * k); x = x + y; k = k + 2; }// end while output = "Sin(" + sTmp + ")= " + x + "\n"; d = 1.0; j = 0; k = 2; }// end if }// end try catch (Exception e) { // TODO: handle exception output = "Invalid Input" + "\n"; }// end catch txtResult.append(output); }// end if else if (arg0.getSource() == btnClear) { txtInput.setText(""); }// end if else if (arg0.getSource() == btnClearall) { txtInput.setText(""); txtResult.setText(""); // default chosen item cmbChoose.setSelectedItem(2); }// end if }// end actionPerformed };// end actionListener // add actionListener to buttons btnCompute.addActionListener(method); btnClear.addActionListener(method); btnClearall.addActionListener(method); }// end constructor
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // new window cComputingSinFunction_T094054 window = new cComputingSinFunction_T094054(); // end program as closing window. window.setDefaultCloseOperation(EXIT_ON_CLOSE); // show the window window.setVisible(true);
}// end main function
}// end class Bài 3: - Code:
-
import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField;
/* * author Anh Trinh (T094054) */ public class cComputingCosinFunction_T094054 extends JFrame { // declare labels JLabel lblSource = new JLabel("Source:"); JLabel lblErrors = new JLabel("Choose Errors:"); // declare text field JTextField txtInput = new JTextField(); // declare text area JTextArea txtResult = new JTextArea(); // declare combo box JComboBox cmbChoose = new JComboBox(); // declare buttons JButton btnCompute = new JButton("Compute"); JButton btnClear = new JButton("Clear"); JButton btnClearall = new JButton("Clear all"); // declare scroll pane JScrollPane panResult = new JScrollPane(txtResult); // declare variable int i, j = 0; double input; String output; double d = 1.0;
/* * constructor */ public cComputingCosinFunction_T094054() { // set title setTitle("T094054 - Cosin Function"); // set size setSize(400, 265); // set location setLocationRelativeTo(null); // reset null layout setLayout(null); // set resizable setResizable(false); // add labels to window add(lblSource); add(lblErrors); // add text to window add(txtInput); // add scroll bar to window add(panResult); // add buttons to window add(btnCompute); add(btnClear); add(btnClearall); // add combo box to window add(cmbChoose); // add data to combo box using for function for (int i = 1; i <= 8; i++) { cmbChoose.addItem(i); }// end for // default chosen item cmbChoose.setSelectedItem(2); // set location lblSource.setBounds(10, 10, 60, 20); txtInput.setBounds(80, 10, 150, 20); lblErrors.setBounds(235, 10, 90, 20); cmbChoose.setBounds(330, 10, 50, 20); btnCompute.setBounds(70, 40, 80, 25); btnCompute.setMargin(new Insets(1, 1, 1, 1)); btnClear.setBounds(160, 40, 80, 25); btnClear.setMargin(new Insets(1, 1, 1, 1)); btnClearall.setBounds(250, 40, 80, 25); btnClearall.setMargin(new Insets(1, 1, 1, 1)); panResult.setBounds(10, 80, 375, 150); txtResult.setEditable(false); // add Action listener ActionListener method = new ActionListener() {
@Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub // declare variable String sTmp = txtInput.getText().trim().toString(); // calculate if (arg0.getSource() == btnCompute) { // try function try { input = Double.parseDouble(sTmp); // check selected error double c = cmbChoose.getSelectedIndex(); // use while function to calculate error which is // selected while (j <= c) { d = d / 10; j++; }// end while // declare variable double x0 = 0; double x = 1; double y = 1; int k = 1; // using if to check if -1< cosinx <1 if (x >= (-1) && x <= 1) { // using while function to calculate cosin while (x - x0 > d || x - x0 < (-d)) { x0 = x; y = (-1) * y * (input * input) / ((k + 1) * k); x = x + y; k = k + 2; }// End while output = "Cos(" + sTmp + ")= " + x + "\n"; d = 1.0; j = 0; k = 2; }// end if }// end try catch (Exception e) { // TODO: handle exception output = "Invalid Input" + "\n"; }// end catch txtResult.append(output); }// end if else if (arg0.getSource() == btnClear) { txtInput.setText(""); }// end if else if (arg0.getSource() == btnClearall) { txtInput.setText(""); txtResult.setText(""); // default chosen item cmbChoose.setSelectedItem(2); }// end if }// end actionPerformed };// end actionListener // add actionListener to buttons btnCompute.addActionListener(method); btnClear.addActionListener(method); btnClearall.addActionListener(method); }// end constructor
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // new window cComputingCosinFunction_T094054 window = new cComputingCosinFunction_T094054(); // end program as closing window. window.setDefaultCloseOperation(EXIT_ON_CLOSE); // show the window window.setVisible(true);
}// end main function
}// end class
| | | | | |
|