Monday, September 3, 2018

Program to add, subtract, multiply and divide two numbers using inputdialogbox


Follow the steps to create GUI:

Addition using Input Dialog Box









Step-1: Drag and drop four radio buttons for Add, Subtract, Multiply and Divide.

Step-2: Change the variable name of Add Radio Button to AddRadioButton.

Step-3: Double Click on Add Radio Button.

Step-4: Write the following code:


 private void AddRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
        double fn=Double.parseDouble(JOptionPane.showInputDialog("Enter First Number"));
        double sn=Double.parseDouble(JOptionPane.showInputDialog("Enter Second Number"));
        double sum=fn+sn;
        JOptionPane.showMessageDialog(null, sum);
    }     

Similarly for other operations like Subtraction, Multiplication, and Division.





Find the largest number among four number



Follow the steps to create GUI:



Step-1: Drag and drop the label from the Palette window and rename it as "Enter First Numer".

Step-2: Drag and drop the TextField from the Palette window and give the variable name to   "tfFirstNumber" by right-clicking on text field object.

Step-3: Drag and drop the label from the Palette window and rename it as "Enter Second Number".

Step-4: Drag and drop the TextField from the Palette window and give the variable name to "tfSecondNumber".

Step-5: Drag and drop the label from the Palette window and rename it as "Enter Third Number".

Step-6: Drag and drop the TextField from the Palette window and give the variable name to "tfThirdNumber".

Step-1: Drag and drop the label from the Palette window and rename it as "Enter Fourth Numer".

Step-2: Drag and drop the TextField from the Palette window and give the variable name to   "tfFourthNumber" by right-clicking on text field object.

Step-7: Drag and drop the Button object from the Palette window and give the variable name to "btnFind" and change the text to "Find Largest".

Step-10: Drag and drop the TextField from the Palette window and give the variable name to   "tfResult" by right-clicking on text field object and make editable to false.


Finally, double-click on the button and follow the codes given below:

private void btnFindActionPerformed(java.awt.event.ActionEvent evt) {                                       
        int fn,sn,th,fr;
        fn=Integer.parseInt(tfFirstNumber.getText());
        sn=Integer.parseInt(tfSecondNumber.getText());
        th=Integer.parseInt(tfThirdNumber.getText());
        fr=Integer.parseInt(tfFourthNumber.getText());
        
        if(fn>sn && fn>th && fn>fr)
        {
          tfResult.setText("Largest Number is: "+Integer.toString(fn));
        }
        else if(sn>fn && sn>th && sn>fr)
        {
            tfResult.setText("Largest Number is: "+Integer.toString(sn));
        }
        else if(th>fn && th>sn && th>fr)
        {
            tfResult.setText("Largest Number is: "+Integer.toString(th));
        }
        else if(fr>fn && fr>sn && fr>th)
        {
            tfResult.setText("Largest Number is: "+Integer.toString(fr));
        } 
        
    }                 

Java program to find compound interest compunded annully



Follow the steps to create GUI:

Step-1: Drag and drop the label from the Palette window and rename it as "Enter Principal".

Step-2: Drag and drop the TextField from the Palette window and give the variable name to   "txtPrincipal" by right-clicking on text field object.

Step-3: Drag and drop the label from the Palette window and rename it as "Enter Rate".

Step-4: Drag and drop the TextField from the Palette window and give the variable name to "txtRate".

Step-5: Drag and drop the label from the Palette window and rename it as "Enter Time".

Step-6: Drag and drop the TextField from the Palette window and give the variable name to "txtTime".

Step-7: Drag and drop the Button object from the Palette window and give the variable name to "btnCalculate" and change the text to "Calculate".

Step-8: Drag and drop the label from the Palette window and rename it as "Total Amount".

Step-9: Drag and drop the TextField from the Palette window and give the variable name to   "txtAmount" by right-clicking on text field object and make editable to false.

Step-10: Drag and drop the TextField from the Palette window and give the variable name to   "txtCI" by right-clicking on text field object and make editable to false.

Finally, double-click on the button and follow the codes given below:

 private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) {                                             
       double principal,rate;
       int time;
       principal=Double.parseDouble(txtPrincipalAmount.getText());
       rate=Double.parseDouble(txtRate.getText());
       time=Integer.parseInt(txtTime.getText());
       
       double amount=principal*Math.pow(1+rate/100,time);
       double ci=amount-principal;
       
       txtAmount.setText(Double.toString(amount));
       txtCI.setText(Double.toString(ci));
    }