Anda di halaman 1dari 10

Swarnim Infosoft Pvt. Ltd.

Page 1
Workshop-1
Objectives:
Installing J ava Development Kit.
Setting up Path Environment Variable.
Compiling & executing a simple java application.
Installing Java Development Kit

To compile and execute J ava applications, a set of tools called J ava Software
Development Kit (J SDK often called J DK) are required. J ava SDK can be obtained from
J ava official site free of cost and can be installed using following steps:

Step 1: Double click on the J DK setup file; the setup wizard will appear as in figure 1.0
Figure 1.0: step 1 of J DK setup wizard
Swarnim Infosoft Pvt. Ltd. Page 2

Step 2: Select I accept the terms in the license agreement and click on Next. Custom
setup dialog will appear as in figure 1.1.
Figure 1.1: Setup options
Step 3: Change the installation path if desired and click on Next to start the installation
and wait until it is done. When completed, click on Finish to terminate the installation
wizard.

Setting Up Path environment variable

Define or update Path environment variable to use the tools of J DK. Path environment
variable is used by the O/S to find out the location of executable files. If path of J DK
tools is known to the O/S then they can be executed from any location.

Step 1: To define path environment variable, right-click on My Computer icon on the
desktop or start menu, then select Properties from the pop-up menu. The System
Properties dialog appears, from the top of the dialog box select Advanced tab as shown in
figure 1.2.




Swarnim Infosoft Pvt. Ltd. Page 3




Figure 1.2: Windows advanced settings

Step 2: From Advanced tab, click on Environment Variables button, Environment
Variables dialog appears as shown in figure 1.3.

Step 3: To add new environment variable, click on New button in User Variables field,
note that you may not modify system variables unless you have administrator privileges.
This basic difference between system and user variables is that, system variables are
available to all the users where as user variables are available only to the user for which
they are defined.

Step 4: Once you click on New button, variable addition dialog appears as in figure 1.4.
In Variable name field enter the name of your variable Path in this case, in Variable value
field enter the path of bin folder of J DK and click on OK.

Your computer is ready now to be used for J ava development.


Swarnim Infosoft Pvt. Ltd. Page 4





Figure 1.3: Environment Variables



Figure 1.4: Adding new environment variable dialog box
Swarnim Infosoft Pvt. Ltd. Page 5


Writing, Compiling and Running Your First Java Application

For writing java applications any text processing tool can be used. For the first few
applications, we will use Notepad.
Step 1: Type the following code (always keep in mind that J ava is case-sensitive
language, for example if you typed Class instead of class the compiler will report a
syntax error).

//First.java
/* This is my first java program.
*/
class First{
public static void main(String[] args){
System.out.println("Welcome to the fascinating world of
Java!");
}//end of main method
}//end of class

Step 2: Save the file by the name First.java in any folder of your choice. Standard
Naming convention of J ava is to give the source file same name as the class containing
main() method.

Notice that some lines in the program begin with //, these are called comments, compiler
does not read comments, so whatever is written in them will not affect the functionality
of the program. The notations /* and */ declare the beginning and ending of a block or
multi-line comment.

Step 3: Open the command prompt by selecting run from start menu and entering the
command cmd in the dialog box, the command prompt console appears, as shown in
figure 1.5.

Step 4: Change to path to your source folder (as I have done to d:\Neeraj).


Figure 1.5: Command Prompt
Swarnim Infosoft Pvt. Ltd. Page 6

Step 5: To compile J ava source file, use the command javac filename (First.java in this
example) as shown in figure 1.6.

If there is no typing error, prompt will return indicating that file is successfully compiled.
Look in your source folder, there will be a class file by the name First.class.


Figure 1.6: Compiling J ava Source File

Step 6: To run you application, use the command java ClassName (First in this example).
If everything is alright, output of the class will be displayed on the console as shown in
figure 1.7.


Figure 1.7: Executing J ava Class

Swarnim Infosoft Pvt. Ltd. Page 7


Exercises

1. Define following class, save it and compile it.

/ / Ar gDemo. j ava
/ / t hi s cl ass i s def i ned t o t est command l i ne ar gument .
publ i c cl ass Ar gDemo{
publ i c st at i c voi d mai n( St r i ng ar r [ ] )
{
Syst em. out . pr i nt ( Wel come, \ t ) ;
f or ( i nt i =0; i <ar r . l engt h; i ++)
Syst em. out . pr i nt ( ar r [ i ] +\ t ) ;
}}

Use the following command to execute it.

java ArgDemo Your Name

Output must be Welcome, Your Name

2. Define following class, save it, compile it and execute it.

/ / Adder . j ava
/ / t hi s cl ass r ecei ves t wo number s as i nput f r omkeyboar d
/ / and di spl ays t hei r sum.

i mpor t j ava. ut i l . Scanner ;
/ / Scanner cl ass pr ovi des met hods t o r ead i nput f r oma
sour ce.

publ i c cl ass Adder {
publ i c st at i c voi d mai n( St r i ng ar r [ ] )
{
/ / Syst em. i n r epr esent s St andar d I nput Devi ce ( Keyboar d)
Scanner i n=new Scanner ( Syst em. i n) ;
Syst em. out . pr i nt l n( Ent er Fi r st No. ) ;
i nt a=i n. next I nt ( ) ;
Syst em. out . pr i nt l n( Ent er Second No. ) ;
i nt b=i n. next I nt ( ) ;
i nt c=a+b;
Syst em. out . pr i nt l n( Sumi s: +c) ;
}}

Swarnim Infosoft Pvt. Ltd. Page 8




3. Define a class named Inverse which contains main() method. In main() method a
number is received as input from the keyboard, it is reversed and displayed.
e.g. if 456 is provided as input, 654 must be displayed as output.

4. Define a class named Convertor which contains main() method. In main() method a
number is received as input from the keyboard and is displayed in words. Assume that
maximum three digits numbers can be given as input.
e.g. if 456 is provided as input, Four Hundred Fifty Six must be the output.

5. Modify class Convertor to convert up to nine digit numbers in words.

6. Define following class, save it, and compile it.

/ / St ar s. j ava
/ / t hi s cl ass r ecei ves an no. of l i nes as command l i ne
ar gument
/ / and di spl ays l i nes of i ncr easi ng st ar s.

publ i c cl ass St ar s{
publ i c st at i c voi d mai n( St r i ng ar r [ ] )
{
/ / Command l i ne ar gument i s conver t ed i nt o i nt eger .
i nt a=I nt eger . par seI nt ( ar r [ 0] ) ;
f or ( i nt i =1; i <=a; i ++)
{
Syst em. out . pr i nt l n( ) ;
f or ( i nt j =0; j <i ; j ++)
Syst em. out . pr i nt ( *) ;
}
}}

Execute the class by providing different line number as command line argument and see
the output.
e.g. java Stars 4







Swarnim Infosoft Pvt. Ltd. Page 9




7. Define a class named NumberTriangle to display a triangle of increasing numbers. No.
of rows of the triangle is provided as command line argument.
e.g. if it is executed as java NumberTriangle 5, then following output must be generated.

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

8. Define a class named NumberLadder to display a ladder of increasing & decreasing
numbers. No. of rows of the triangle is provided as command line argument.
e.g. if it is executed as java NumberLadder 5, then following output must be generated.

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

9. Define a class named FibonacciGenerator to display first n terms of the Fibonacci
Series. The value of n is provided as command line argument.
Fibonacci series is: 0 1 1 2 3 5 8 13 21
e.g. If it is executed as java FibonacciGenerator 5, following output must be displayed
0 1 1 2 3

10. Define a class named PrimeTester which receives a number as command line
argument and displays whether it is prime or not.
e.g. if it is executed as java PrimeTester 25, output must be 25 isnt a prime number.

11. Define a class named PalindromeTester which receives a number as command line
argument and displays whether it is palindrome or not. A number is palindrome, if its
reverse is same as the number. e.g. 252 is palindrome.

12. Define a class named DecToBinary which receives a number as command line
argument and displays it in binary format. No library method must be used.
e.g. if it is executed as java DecToBinary 12, then output must be 1100.

13. Define a class named DecToHex which receives a number as command line argument
and displays it in HexaDecimal format. No library method must be used.
e.g. if it is executed as java DecToHex 25, then output must be f9.

Swarnim Infosoft Pvt. Ltd. Page 10






14. Define following class, save it, compile it and execute it.

/ / Shapes. j ava
/ / i t di spl ays f our shapes on t he consol e.

publ i c cl ass Shapes
{
publ i c st at i c voi d mai n( St r i ng ar gs[ ] )
{
Syst em. out . pr i nt l n( "********* *** * *
") ;
Syst em. out . pr i nt l n( "* * * * *** * *
") ;
Syst em. out . pr i nt l n( "* * * * ***** * *
") ;
Syst em. out . pr i nt l n( "* * * * * * *
") ;
Syst em. out . pr i nt l n( "* * * * * * *
") ;
Syst em. out . pr i nt l n( "* * * * * * *
") ;
Syst em. out . pr i nt l n( "* * * * * * *
") ;
Syst em. out . pr i nt l n( "* * * * * * *
") ;
Syst em. out . pr i nt l n( "********* *** * *
") ;
} / / end mai n
} / / end cl ass


15. Modify Shapes class to display your name using stars.

Anda mungkin juga menyukai