Java Lab
Java Lab
Saheli Bakshi
KINGSTON SCHOOL OF MANAGEMENT & SCIENCE
Kingston School of Management & Science (College Code: 173)
Object Oriented Programming (BCAC 301)
Index:
SL Title Date
No.
1. Even or Odd 06/09/2021
2. Positive or negative, Leap year or not 13/09/2021
3. Loop (while, for), Reverse Number, 15/09/2021
Palindrome Number
4. Do-while loop, short circuit operator 16/09/2021
5. Class object, Odd even checking with 20/09/2021
Method
6. Checking Prime number using Method 21/09/2021
7. Method Overloading 22/09/2021
8. Typecasting 23/09/2021
9. Constructor, checking Prime No with 27/09/2021
Class & Constructor
10. Constructor Overloading 28/09/2021
11. Array 29/09/2021
12. Binary search, Linear search, Matrix 30/09/2021
(addition, multiplication, transpose)
13. Irregular array, sparce matrix, 04/10/2021
Inheritance
14. Abstract Method Overriding 05/10/2021
15. Access Specification 07/10/2021
16. String 01/11/2021
17. String as Immutable class 02/11/2021
18. Reverse an immutable String, Exception 08/11/2021
Handling
19. Compile time exception 09/11/2021
20. Throws Keyword 10/11/2021
21. Thread 11/11/2021
22. Multithreading 22/11/2021
23. Applet 25/11/2021
24. Package 07/12/2021
P a g e 1 | 58
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
P a g e 2 | 58
1. Checking Even Odd
Code:
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number: ");
int x=sc.nextInt();
if(x%2==0)
System.out.println(x + " is even number");
else
System.out.println(x + " is odd number");
}
}
Output:
P a g e 3 | 58
2. Positive or Negative Number
Code:
import java.util.*;
class PositiveNegative
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter two numbers: ");
int x=sc.nextInt();
int y=sc.nextInt();
if(x>=0 && y>=0)
System.out.println("Both the numbers are positive");
else if(x<0 && y<0)
System.out.println("Both the numbers are negative");
else
System.out.println("One number is positive another is negative");
}
Output
P a g e 4 | 58
Leap Year
Code:
import java.util.*;
class LeapYear
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a year: ");
int y=sc.nextInt();
if((y%4==0 && y%100!=0) || (y%400==0))
System.out.println("It is a Leap Year");
else
System.out.println("It is NOT a Leap Year");
}
}
Output:
P a g e 5 | 58
Using while loop
Code:
import java.util.*;
class WhileLoop
{
public static void main(String args[])
{
//Scanner sc=new Scanner(System.in);
//System.out.println("Enter a number: ");
//int i=sc.nextInt();
int i=1;
while(i<=10)
{
System.out.println("i="+i);
i++;
}
}
}
Output:
P a g e 6 | 58
Using for loop
Code:
import java.util.*;
class ForLoop
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number: ");
int i=sc.nextInt();
for(;i<=10;i++)
{
System.out.println("For i="+i);
}
}
Output:
P a g e 7 | 58
Reverse Number
Code:
import java.util.*;
class ReverseNumber
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number: ");
int n=sc.nextInt();
System.out.print("Reverse number: ");
while(n>0)
{
System.out.print(n%10);
n=n/10;
}
}
Output:
P a g e 8 | 58
Palindrome Number
Code:
import java.util.*;
class PalindromeNumber
{
public static void main(String args[])
{
int r,rev=0,n1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number: ");
int n=sc.nextInt();
n1=n;
while(n>0)
{
r=n%10;
rev=rev*10 +r;
n=n/10;
}
System.out.print("Reverse number: "+rev);
if(rev==n1)
System.out.print("\n\nNumber is palindrome");
else
System.out.print("\n\nNumber is NOT palindrome");
}
}
Output:
P a g e 9 | 58
Do While Loop
Code
import java.util.*;
class DoWhileLoop
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number: ");
int n=sc.nextInt();
do //Exit controlled loop
{
System.out.println("Number "+n);
n--;
}while(n>0);
}
}
Output:
P a g e 10 | 58
Short Circuit operator
Code:
class Test
int x=5,y=10;
System.out.println(y);
Output:
P a g e 11 | 58
Code:
class Test1
int x=5,y=10;
System.out.println(x++>0 || y++>100);
System.out.println(y);
Output:
P a g e 12 | 58
Class & Object
Code:
class TestObject
{
int x;
void meth()
{
System.out.println("1st Object program...");
System.out.println("Value of x="+x);
}
}
class TestMyOblect
{
public static void main(String ghhgtd[])
{
TestObject to=new TestObject();
to.x=5;
to.meth();
}
}
Output:
P a g e 13 | 58
Check even odd with class & object
Code:
import java.util.Scanner;
class Test {
int n, i, p = 1;
void input() {
System.out.print("Enter a number:");
n = sc.nextInt();
}
void check() {
if (n % 2 == 0) {
System.out.println("Number is even:" + n);
} else {
System.out.println("Number is odd:" + n);
}
}
}
class Main {
}
}
P a g e 14 | 58
Output:
P a g e 15 | 58
Check prime number
Code:
import java.util.*;
class TestPrime1
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number:");
int n=sc.nextInt();
CheckPrime1 cp=new CheckPrime1();
int val=cp.isPrime(n);
if(val==0)
{
System.out.println("Number is not prime");
}
else
{
System.out.println("Number is prime");
}
}
}
class CheckPrime1
{
int isPrime(int x)
{
for(int i=2;i<=x/2;i++)
{
if(x%i==0)
return 0;
}
return 1;
}
Output
P a g e 16 | 58
P a g e 17 | 58
Static polymorphism \ Method Overloading
Code:
class MethodOverloading
{
public void disp(char c, int num)
{
System.out.println("Hey, This is Saheli Bakshi.");
}
public void disp(int num, char c)
{
System.out.println(" I am a student of BCA." );
}
}
class SaheliBakshi
{
public static void main(String args[])
{
MethodOverloading obj = new MethodOverloading();
obj.disp('x', 33 );
obj.disp(21, 'y');
}
}
Output:
P a g e 18 | 58
Implicit Typecasting
Code:
class Typecasting{
public static void main(String[] args) {
// create int type variable
int num = 10;
System.out.println("The integer value: " + num);
Output:
P a g e 19 | 58
Explicit Typecasting
Code:
class MainType {
public static void main(String[] args) {
// create double type variable
double num = 10.99;
System.out.println("The double value: " + num);
P a g e 20 | 58
Constructor
Code
class TestConstr
TestConstr()
} }
class MyCons
Output:
P a g e 21 | 58
Check prime number with Constructor
Code
class TestConstr {
TestConstr(int x) {
int count=0;
for(int i=2;i<=x/2;i++) {
if(x%i==0) count++;
if(count==0)
} } }
class MyConstructor {
} }
Output:
P a g e 22 | 58
Constructor Overloading
Code
Student(){
System.out.println("this a default constructor");
}
P a g e 23 | 58
Array
Code
class TestArray
{
public static void main(String args[])
{
int arr[]=new int[5]; //static
System.out.println("Enter array elements:");
for(int i=0;i<5;i++)
{
arr[i]=i;
}
System.out.println("Array elements:");
for(int i=0;i<5;i++)
{
System.out.println(arr[i]);
}
}
}
Output:
P a g e 24 | 58
Linear Search
Code
import java.util.*;
class TestArray3
{
public static void main(String args[])
{
System.out.println("Enter how many array elements you want?");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
}
}
Output:
Binary Search
class BinarySearchExample
{
public static void binarySearch(int arr[], int first, int last, int key){
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}else if ( arr[mid] == key ){
System.out.println("Element is found at index: " + mid);
break;
}else{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last ){
System.out.println("Element is not found!");
}
P a g e 26 | 58
}
public static void main(String args[]){
int arr[] = {10,20,30,40,50};
int key = 30;
int last=arr.length-1;
binarySearch(arr,0,last,key);
}
}
Output:
P a g e 27 | 58
Matrix
Code
import java.util.*;
class TestTwoArray
{
public static void main(String args[])
{
System.out.println("Enter how many row and column you want?");
Scanner sc=new Scanner(System.in);
int row=sc.nextInt();
int col=sc.nextInt();
int arr[][]=new int[row][col]; //Dynamic two-D Array
System.out.println("Enter array elements:");
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
arr[i][j]=sc.nextInt();
}
}
System.out.println("\nArray elements:");
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
P a g e 28 | 58
}
}
}
Output:
Matrix addition
Code
import java.util.*;
class TestTwoArray1
{
public static void main(String args[])
{
System.out.println("Enter how many row and column you want?");
Scanner sc=new Scanner(System.in);
int row=sc.nextInt();
int col=sc.nextInt();
int arr1[][]=new int[row][col]; //Dynamic two-D Array
int arr2[][]=new int[row][col];
int arr3[][]=new int[row][col];
System.out.println("Enter " +(row*col)+" array elements in the 1st
Matrix:");
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
arr1[i][j]=sc.nextInt();
}
}
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
arr3[i][j]=arr1[i][j]+arr2[i][j];
}
}
Output:
P a g e 30 | 58
matrix multiplication
Code
public class MatrixMultiplicationExample{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
//creating another matrix to store the multiplication of two matrices
int c[][]=new int[3][3]; //3 rows and 3 columns
//multiplying and printing multiplication of 2 matrices
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}//end of k loop
System.out.print(c[i][j]+" "); //printing matrix element
P a g e 31 | 58
}//end of j loop
System.out.println();//new line
}
}}
P a g e 32 | 58
matrix transpose
Code
public class MatrixTransposeExample{
public static void main(String args[]){
//creating a matrix
int original[][]={{1,3,4},{2,4,3},{3,4,5}};
//creating another matrix to store transpose of a matrix
int transpose[][]=new int[3][3]; //3 rows and 3 columns
//Code to transpose a matrix
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
transpose[i][j]=original[j][i];
}
}
System.out.println("Printing Matrix without transpose:");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(original[i][j]+" ");
}
System.out.println();//new line
}
P a g e 33 | 58
System.out.println("Printing Matrix After Transpose:");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(transpose[i][j]+" ");
}
System.out.println();//new line
}
}}
P a g e 34 | 58
Irregular Array
Code
import java.util.*;
class TestIrrArr
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int arr[][]=new int[3][]; //irregular array
arr[0]=new int[2];
arr[1]=new int[3];
System.out.println("Enter 2 elements:");
for(int i=0;i<2;i++)
arr[0][i]=sc.nextInt();
System.out.println("Enter 3 elements:");
for(int i=0;i<3;i++)
arr[1][i]=sc.nextInt();
System.out.println("2 elements:");
for(int i=0;i<2;i++)
System.out.print(arr[0][i] + " ");
System.out.println("\n 3 elements");
for(int i=0;i<3;i++)
System.out.print(arr[1][i]+" ");
}
}
Output:
P a g e 35 | 58
Inheritance
Code
class Aa
{
int x;
void meth()
{
System.out.print("\nFrom Parent class...");
}
}
class Bb extends Aa
{
void meth1()
{
System.out.print("\nFrom Child class");
}
}
class TestInherit
{
public static void main(String khg[])
{
Bb b=new Bb();
b.x=5;
b.meth();
b.meth1();
System.out.print("\nValue of x="+b.x);
P a g e 36 | 58
}
}
Output:
P a g e 37 | 58
Abstract Method Overriding
Code
P a g e 38 | 58
}
class TestMyInherit
{
public static void main(String khg[])
{
ChildClass b=new ChildClass();
b.x=5;
b.meth();
b.meth1();
b.methAbs();
System.out.print("\nValue of x="+b.x);
}
}
P a g e 39 | 58
P a g e 40 | 58
Access Specification
Code
class TestPrivate
{
private int x=5;
public int y=7;
int returnPrivate()
{
return x;
}
}
class MyTestPrivate
{
public static void main(String kjg[])
{
TestPrivate tp=new TestPrivate();
System.out.println("Public data Y="+ tp.y);
Output:
P a g e 41 | 58
String
Code
class TestString
{
public static void main(String khg[])
{
String s1=new String("ABCDE");
System.out.println("1st String:"+s1);
String s2="FGH";
System.out.println("2nd String:"+s2);
s1=s1.concat(s2);
System.out.println("1st String:"+s1);
System.out.println("Sub-String:"+ s1.substring(3,5));
String s3=s1.toLowerCase();
System.out.println(s3);
P a g e 42 | 58
P a g e 43 | 58
String as immutable class
Code
P a g e 44 | 58
P a g e 45 | 58
Reverse string
Code
class StringReverse
{
public static void main(String bakshi[])
{
String s1="Te Amo";
String s2="";
for(int i=s1.length()-1 ;i>=0; i--)
{
s2=s2 + s1.charAt(i);
}
System.out.println("Reverse of String:" + s2);
}
}
Output:
P a g e 46 | 58
Exception Handling
Code
int c=a/b;
System.out.println("C="+c);
}
catch(Exception e)
{
System.out.println("Exception solved by BCA students : "+ e);
}
finally
{
System.out.println("In Finally.... \nEnd of Code....");
}
}
}
Output:
P a g e 47 | 58
Throws Keyword
Code
import java.io.*;
class TestException2
throws Exception
int a=Integer.parseInt(bfr.readLine());
int b=Integer.parseInt(bfr.readLine());
} }
P a g e 48 | 58
Multithreading
Code
P a g e 50 | 58
Thread with in-built method
Code
//thread.yield method
class MyMainThread1
P a g e 51 | 58
public static void main(String jhf[])
throws Exception
th1.start();
th1.join();
th2.start();
th2.join();
th3.start();
th3.join();
} }
P a g e 52 | 58
Code
//thread.sleep method
class ThreadBCA1 implements Runnable
{
public void run()
{
try{
for(int i=1;i<=20;i++)
{
System.out.println("1st Thread Running...");
Thread.sleep(1000);
}
}
catch(Exception e){}
}
}
P a g e 53 | 58
Applet
Developing an Applet Program
code
import java.awt.*;
import java.applet.*;
public class AppletDemo
{
public void paint(Graphics g)
{
g.drawString("Welcome to TutorialRide", 50, 50);
}
}
//AppletDemo.html
<html>
<head>
<title> AppletDemo </title>
</head>
P a g e 54 | 58
<body>
<applet code="AppletDemo.class" width="200"
height="250">
</applet>
</body>
</html>
P a g e 55 | 58
Package
Step 1: Create a folder with name "ArithmeticOperation"
Step 2: within that folder create classes as:
Code
package ArithmeticOperation;
public class Sum
{
private int x,y;
public Sum(int a,int b)
{
X=a;
y=b;
}
public long add()
{
return(x+y);
}
}
package ArithmeticOperation;
public class Substract
{
private int a,b;
public Substract(int x,int y)
{
a=x; b=y;
}
public long substraction()
{
return(a-b);
}
}
Step 3: Create another class just outside the folder "ArithmeticOperation" that contains the main
method as:
import java.io.*;
import ArithmeticOperation.*;
class Operation
{
public static void main(String sr[])
{
try
{
P a g e 56 | 58
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br= new BufferedReader(isr);
System.out.println("Enter two Integer: ");
int templ=Integer.parselnt(br.readLine();
int temp2=Integer.parselnt(br.readLine();
Sum s=new Sum(templ,temp2);
Substract st= new Substract(temp1,temp2);
System.out.println("Addition=" +s.add());
System.out.println("Substraction= "+st.substraction));
}
Catch(exception e)
{
System.out.println(e);
}
}
}
P a g e 57 | 58