Labsheet#03

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

Jaypee Institute of Information Technology, Noida-128 Software Engineering Lab Lab Exercise 3

Objective- Main aim of this lab is to get familiar with Java coding, Java program compilation and running of Java program. Program-1: Write a simple java program for Hello World!.

Hello world (First java program)


Java is a high level programming language and it is used to develop the robust application. Java application program is platform independent and can be run on any operating System . Writing Hello World program is very simple. To write the Hello world program you need simple text editor like note pad and jdk must be install in your machine for compiling and running. Hello world program is the first step of java programming language. Be careful when you write the java code in your text pad because java is a case sensitive programming language. For Example hello world !=(not equal to) Hello World Write the following code into your note pad to run the Hello World program . class HelloWorld { public static void main(String[] args){ System.out.println("Hello World!"); } } Save the file and please remember the location where you save your file. In this example we have saved the file in the "C:\javatutorial\example" directory. The file name should be matching the class name and to save the file give the .java extension. e.g. HelloWorld.java Now open the command prompt to compile the HelloWorld.java program . Go to the directory where you have saved the file ( in our case C:\javatutorial\example>) and issue the following command to compile the program: C:\javatutorial\example>javac HelloWorld.java javac is a compiler in the java Language. Java compiler change the programming Language into machinery language. So that the java virtual can understand it.

Once the compilation is successful a new file will be created with the name HelloWorld.class. To run the program issue the following command on command prompt: C:\javatutorial\example>java HelloWorld You will see the following result on the command prompt. Hello World! Here is the screen shot of the above steps:

In this lesson you have learned how to write and then test the Hello World! java program.

Understanding Hello World Java Program


Now you are familiar with the Java program . In the previous section you learned how to compile and run the Java program. Before start hard programming in Java, its necessary to understand each and every part of the program. Lets understand the meaning of public, class, main, String[] args, System.out, and so on.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } }

Class Declaration: Class is the building block in Java, each and every methods & variable exists within the class or object. (instance of program is called object ). The public word specifies the accessibility of the

class. The visibility of the class or function can be public, private, etc. The following code declares a new class "HelloWorld" with the public accessibility: public class HelloWorld {

The main Method:


The main method is the entry point in the Java program and java program can't run without main method. JVM calls the main method of the class. This method is always first thing that is executed in a java program. Here is the main method: public static void main(String[] args) { ...... ..... } { and is used to start the beginning of main block and } ends the main block. Every thing in the main block is executed by the JVM. The code: System.out.println("Hello, World"); prints the "Hello World" on the console. The above line calls the println method of System.out class. The keyword static: The keyword static indicates that the method is a class method, which can be called without the requirement to instantiate an object of the class. This is used by the Java interpreter to launch the program by invoking the main method of the class identified in the command to start the program.

Program 2 Write a java program for comparing of two numbers.


Comparing Two Numbers
This is a very simple example of Java that teaches you the method of comparing two numbers and finding out the greater one. First of all, name a class "Comparing" and take two numbers in this class. Here we have taken a=24 and b=25, now we have to find out whether a=b, a>b or b>a. To find out this apply if and else condition one by one. Now apply the condition "if (a=b)", if this satisfies then type that both are equal in the system class. If this doesn't satisfy, then check whether a>b by applying the "else if" condition and type the message "a is greater than b" in the system class. Again this doesn't satisfy then 'else' condition as shown in the example will show that b is greater than a. Now compile and run the program and you will find the desired output. If you are getting any error then check the whole program thoroughly and surely you will get correct result. By compiling and running this exact program, you will find that b is greater than a.
class Comparing{ public static void main(String[] args) { int a=24, b=25; if (a == b){ System .out.println("Both are equal"); } else if(a>b){ System.out.println("a is greater than b"); } else{ System.out.println("b is greater than a"); } } }

Home Assignment Problem1. Write a java program for listing out all the even numbers between
two numbers.

Problem2. Write a java program for preparing the table of a given number by
using loop condition.

Problem3. Write a java program to construct a triangle with the *.

You might also like