Computer programming in Healthcare - HIMT 2403 Lab-2 Page 1
Lab 2 (Introduction to Java)
Objectives
• Write, compile, and run simple Java programs using eclipse IDE for JAVA
Developers
• Debug some syntax errors
Introduction:
Parameters used in Java Program
Let's see what is the meaning of class, public, static, void, main, String[],
System.out.println().
o class keyword is used to declare a class in java.
o public keyword is an access modifier which represents visibility. It means it
is visible to all.
o static is a keyword. If we declare any method as static, it is known as the
static method. The core advantage of the static method is that there is no
need to create an object to invoke the static method. The main method is
executed by the JVM, so it doesn't require to create an object to invoke the
main method. So it saves memory.
o void is the return type of the method. It means it doesn't return any value.
o main represents the starting point of the program.
o String[] args is used for command line argument. We will learn it later.
o System.out.println() is used to print statement. Here, System is a class,
out is the object of PrintStream class, println() is the method of PrintStream
class. We will learn about the internal working of System.out.println
statement later.
First Example: Sum of two numbers
public class AddTwoNumbers {
public static void main(String[] args) {
int num1 = 5, num2 = 15, sum;
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}
Output:
Sum of these numbers: 20
Computer programming in Healthcare - HIMT 2403 Lab-2 Page 2
Second Example: Sum of two numbers using Scanner:
The scanner allows us to capture the user input so that we can get the
values of both the numbers from user. The program then calculates the
sum and displays it.
import java.util.Scanner; public
class AddTwoNumbers2 {
public static void main(String[] args) {
int num1, num2, sum;
Scanner sc = new Scanner(System.in);
System.out.println("Enter First Number: ");
num1 = sc.nextInt();
System.out.println("Enter Second Number: ");
num2 = sc.nextInt();
sc.close();
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}
Output:
Enter First Number:
121
Enter Second Number:
19
Sum of these numbers: 140
Computer programming in Healthcare - HIMT 2403 Lab-2 Page 3
Third Example: Write a java program to check prime number:
Prime number in Java: Prime number is a number that is greater than 1 and
divided by 1 or itself only. In other words, prime numbers can't be divided by other
numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime
numbers.
Note: 0 and 1 are not prime numbers. The 2 is the only even prime number because
all the other even numbers can be divided by 2.
Let's see the prime number program in java. In this java program, we will take a
number variable and check whether the number is prime or not.
1. public class PrimeExample{
2. public static void main(String args[]){
3. int i,m=0,flag=0;
4. int n=3;//it is the number to be checked
5. m=n/2;
6. if(n==0||n==1){
7. System.out.println(n+" is not prime number");
8. }else{
9. for(i=2;i<=m;i++){
10. if(n%i==0){
11. System.out.println(n+" is not prime number");
12. flag=1;
13. break;
14. }
15. }
16. if(flag==0) { System.out.println(n+" is prime number"); }
17. }//end of else
18. }
19. }
Input: 44
Output: not prime number
Input: 7
Output: prime number
Computer programming in Healthcare - HIMT 2403 Lab-2 Page 4
Fourth Example: Write a java program to calculate Area of Rectangle.:
User would provide the length and width values during execution of the
program and the area would be calculated based on the provided values.
/**
* @author: BeginnersBook.com
* @description: Program to Calculate Area of rectangle
*/
import java.util.Scanner;
class AreaOfRectangle {
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of Rectangle:" );
double length = scanner.nextDouble();
System.out.println("Enter the width of Rectangle:" );
double width = scanner.nextDouble();
//Area = length*width;
double area = length*width;
System.out.println("Area of Rectangle is:" +area);
}
}
Output:
Enter the length of Rectangle:
2
Enter the width of Rectangle:
8
Area of Rectangle is:16.0
Program 2:
In the above program, user would be asked to provide the length and
width values. If you do not need user interaction and simply want to
specify the values in program, refer the below program.
/**
* @author: BeginnersBook.com
* @description: Calculation with no user interaction
*/
class AreaOfRectangle2 {
public static void main (String[] args)
{
double length = 4.5;
double width = 8.0;
double area = length*width;
System.out.println("Area of Rectangle is:" +area);
}
}
Output:
Area of Rectangle is:36.0