Building an Application in Java.

We will start by building an application that adds two numbers. Whenever you build any java application, there is asset template for design that you automatically follow.

Import javax.swing.joptionpane;

The compiler imports the classes you will be using in your application.

Import javax.swing.joptionpane;

Public class Addition

{

}   // end of class addition

Define your class name, and right away place the opening and closing brackets—with the comment.

Import javax.swing.joptionpane;

Public class addition

{

Public static void main( string args[] )

{

System.exit( 0 );

} // end of main()

} // end of class addition

Add main method, and system.exit( 0 ) .

Next, we actually start with program logic for the intended application.

Import javax.swing.joptionpane;

Public class addition

{

Public static void main( string args[] )

{

String firstNumber, SecondNumber;

These are two “String” references. That means they have the potential to point to object to type string. However, at this point they are empty references.

Import javax.swing.joptionpane;

Public class addition

{

Public static void main( string args[] )

{

String firstNumber, SecondNumber;

Int number1, number2, sum;

System.exit( 0 );

} // end of main()

} // end of class addition

Now, we add the three integer variables. They are not objects, and hold three integers (without any methods or classes). number1, number2 and number3 are called primitive variables.

Import javax.swing.joptionpane;

Public class addition

{

Public static void main( string args[] )

{

String firstNumber, SecondNumber;

Int number1, number2, sum;

firstNumber= Joptionpane.show InputDialog(“First Num”);

secondNumber= Joptionpane.show InputDialog(“Second Num”);

Method show InputDialog() receives a string argument, and returns astring result. Joptionpane creates InputDialog Boxes.

But, since the numbers are still strings, we can’t add them yet. We need some object that has a same method capable of taking a string argument and returning an integer.

Import javax.swing.joptionpane;

Public class addition

{

Public static void main( string args[] )

{

String firstNumber, SecondNumber;

Int number1, number2, sum;

firstNumber= Joptionpane.show InputDialog(“First Num”);

secondNumber= Joptionpane.show InputDialog(“Second Num”);

number1 = Integer.parseInt ( firstNumber ) ;

nnumber2 = Integer.parseInt ( secondNumber ) ;

sum = number1 + number2;

Integer is class. Its method parseInt () takes a string argument and returns an int.

And finally, the code looks like:

Import javax.swing.joptionpane;

Public class addition

{

Public static void main( string args[] ) }

{

String firstNumber,

SecondNumber;

 

Int number1,

number2,

sum;

firstNumber= Joptionpane.show InputDialog(“First Num”);

secondNumber= Joptionpane.show InputDialog(“Second Num”);

number1 = Integer.parseInt ( firstNumber ) ;

nnumber2 = Integer.parseInt ( secondNumber ) ;

sum = number1 + number2;

Joptionpane.showMessage Dialog ( null, “The sum is : “ +sum,

“Result”, Joptionpane.PLAIN_MESSAGE );

System.exit( 0 );

} //end of main()

} // end of class addition

Written by Ranjeet Jha

Ranjeet Jha

Hey there! My name is Ranjeet. I’m a Web Designer & Front-end Developer. I design, code, build & promote all things HTML5, CSS3, JavaScript & jQuery. In this blog you will find solutions of day to day problems faced by you.. Enjoy Coding..

Leave a Reply

Your email address will not be published. Required fields are marked *