Showing posts with label Java Program to find lasgest number among four numbers. Show all posts
Showing posts with label Java Program to find lasgest number among four numbers. Show all posts

Monday, September 3, 2018

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