0% found this document useful (0 votes)
6 views

Java Lab

lab

Uploaded by

lahap88162
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Java Lab

lab

Uploaded by

lahap88162
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

PROGRAM -1

OBJECTIVE : Write a program to print “Hello World!” in java.

SOURCE CODE : Ex_hello.java


public class Ex_hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
OUTPUT :
PROGRAM 2
OBJECTIVE : Write a program to create a class, instantiate an object, and access its elements.

SOURCE CODE : Ex_ob.java

class A
{
int x = 10;
public void show()
{
System.out.println(“X = “ + x);
}
}
x = 10
public class Ex_ob{
public static void main(String s[])
x = 1234
{
A ob = new A( );
ob.show( );
ob.x = 1234;
ob
ob.show( );
}
}
OUTPUT:
PROGRAM 3
OBJECTIVE : Write a program to understand the concept of method overloading in java

SOURCE CODE : Ex_methodover.java

class A
{
int x = 10;
public void show()
{
System.out.println(“X = “ + x);
}

public void show(int p1) x = 10


{
x = p1;
x = 1234
System.out.println(“Now X = “ + x);
}
}
ob
public class Ex_methodover{
public static void main(String s[])
{
A ob = new A( );
ob.show( );
ob.show(1234);
ob.show( );
}
}
OUTPUT :
PROGRAM - 4
OBJECTIVE : Write a program to understand the concept of constructor in java

SOURCE CODE : Ex_constr.java


class A
{
public A( )
{
System.out.println("constructor of class A is executed");
}
}
public class Ex_constr
{
public static void main(String s[])
{
A ob = new A();
}
}

OUTPUT :
PROGRAM - 5
OBJECTIVE : Write a program to explain constructor overloading in java

SOURCE CODE : Ex_constrOverload.java


class A
{
int x;
public A( )
{
x = 10;
}
public void disp()
{
System.out.println("X = "+ x); x = 10
}
public A(int p1)
{
x = p1;
ob
}
}

public class Ex_constrOverload


{
public static void main(String s[]) x=
{
A ob = new A();
ob.disp();
A ob1 = new A(1234);
ob1.disp();
ob.disp();
}
}
OUTPUT :
PROGRAM - 6
OBJECTIVE : Write a program for single inheritance in java

SOURCE CODE : Ex_singleInher.java

class A
{
public A()
{
System.out.println("in class A");
}
A
}
class B extends A
{
public B()
{
System.out.println("in class B");
}
}

public class Ex_singleInher B


{
public static void main(String s[])
{
B ob = new B();
}
}
OUTPUT:
PROGRAM - 7
OBJECTIVE : Write a program to execute multi-level inheritance in java

SOURCE CODE: Ex_multilevelInher.java

class A
{
public A()
{
System.out.println("in class A"); A
}
}
class B extends A
{
public B()
{
System.out.println("in class B");

}
} B
class C extends B
{
public C()
{
System.out.println("in class C");
}
}
public class Ex_multilevelInher
{
C
public static void main(String s[])
{
C ob = new C();
}
}

OUTPUT :
PROGRAM - 8
OBJECTIVE : Write a program to execute hierarchical inheritance in java

SOURCE CODE : Ex_hieInher.java

class A
{ A
public A()
{
System.out.println("in class A");
}
}
class B extends A
B C
{
public B()
{
System.out.println("in class B");
}
}
class C extends A
{
public C()
{
System.out.println("in class C");
}
}
public class Ex_hieInher
{
public static void main(String s[])
{
B ob = new B();
C ob1 = new C();
}
}
OUTPUT :
PROGRAM - 9
OBJECTIVE : Write a program to explain the concept of method overriding in java

SOURCE CODE : Ex_methodoverriding.java

class A
{
int x;
public A()
{
x = 10;
}
public void show()
{
System.out.println("X = " + x);
}
}
class B extends A
{
int y;
public B()
{
y = 20;
}
public void show()
{
System.out.println("Y = " + y);
}
}

public class Ex_methodoverriding


{
public static void main(String s[])
{
B ob = new B();
ob.show();
}
}
OUTPUT :
PROGRAM - 10
OBJECTIVE : Write a program to explain the importance and use of super keyword in java

SOURCE CODE : Ex_super.java

class A
{
int x;
public A()
{
x = 10;
}
public void show()
{
System.out.println("X = " + x);
}
}
class B extends A
{
int y;
public B()
{
y = 20;
}
public void show()
{
super.show();//calls the show method of parent class
System.out.println("Y = " + y);
}
}
public class Ex_super
{
public static void main(String s[])
{
B ob = new B();
ob.show();
}
}
OUTPUT :
PROGRAM - 11
OBJECTIVE : Write a program to explain the working of super constructor in java
SOURCE CODE : Ex_superconst.java

class A {
public A() {
System.out.println("A's constructor called");
}
}

class B extends A {
public B() {
super(); // Calls the constructor of class A explicitly
System.out.println("B's constructor called");
}
}

public class Ex_superconstr {


public static void main(String s[]) {
B ob = new B();
}
}
NOTE : IN THE ABOVE CODE IT IS NOT NECESSARY FOR SUPER
CONSTRUCTOR BUT IT IS USED WHEN PARAMETERISED CONSTRUCTOR OF
PARENT CLASS IS INTRODUCED.

OUTPUT :
PROGRAM - 12
OBJECTIVE : Write a program to demonstrate the use of this keyword

SOURCE CODE : Ex_this.java

class A {
int x = 10;
public void show()
{
System.out.println("X = "+x);
}

public void show(int x)


{
this.x = x+1;//calling the variable by which object the method is called
System.out.println("Now X = " + x);
System.out.println("Instance X = " + this.x);
}
}
public class Ex_this {
public static void main(String s[]) {
A ob = new A();
ob.show();
ob.show(1);
ob.show();
}
}
OUTPUT:
PROGRAM - 13
OBJECTIVE : Write a program to demonstrate the use of final keyword with variables in java

SOURCE CODE : Ex_finalvar.java

class A {
final int x = 10;
public void show()
{
System.out.println("X = "+x);
}
}
public class Ex_finalvar {
public static void main(String s[]) {
A ob = new A();
ob.show();
ob.x = 11;//gives error as x is final and value assigned to 10
}
}

OUTPUT:
PROGRAM - 14
OBJECTIVE : Write a program to demonstrate the use of final keyword with methods in java

SOURCE CODE : Ex_finalmethod.java

class A {
final int x = 10;
public final void show()
{
System.out.println("X = "+x);
}

class B extends A{
int y = 20;
public void show();//gives error as show method is final in class A
{
System.out.println("Y = "+ y);
}
}
}
public class Ex_finalmethod{
public static void main(String s[]) {
B ob = new B();
ob.show();
}
}

OUTPUT:
PROGRAM - 15
OBJECTIVE : Write a program to demonstrate the use of final keyword with classes in java

SOURCE CODE : Ex_finalclass.java

final class A {
final int x = 10;
public final void show()
{
System.out.println("X = "+x);
}

class B extends A{//gives error as class A is final -> cant be inherited


int y = 20;
public void show();
{
System.out.println("Y = "+ y);
}
}
}
public class Ex_finalmethod{
public static void main(String s[]) {
B ob = new B();
ob.show();
}
}
OUTPUT :
PROGRAM - 16
OBJECTIVE : Write a program that explains the static keyword in java

Static keyword : Non-static data cannot be called in static context while in the same class

SOURCE CODE: Ex_static.java

class A{
public void show()
{
System.out.println("SHOW");
}

public static void disp()


{
A ob = new A();
ob.show();//Non-static data called in static block through object
System.out.println("DISP");
}
}

public class Ex_static{


public static void main(String s[])
{
A ob1 = new A();
ob1.disp();
}
OUTPUT:
PROGRAM - 17
OBJECTIVE : Write a program that explains the static variable in java

SOURCE CODE: Ex_staticvar.java

class A{
int x;
static int y;
public A(int p1, int p2)
{
x=p1;
y=p2;
}
public void show()
{
System.out.println("X = "+ x +" "+ "Y = "+ y);
}
}
public class Ex_staticvar{
public static void main(String s[])
{
A ob = new A(100,200); y=
ob.show();
A ob1 = new A(300,400);
ob1.show();
ob.show();
}
x=
} x=
100
ob ob1
OUTPUT:
PROGRAM - 18
OBJECTIVE : Write a program to create and use a custom Java package, import a class, and
modify its member variable.

SOURCE CODE :
project-folder

├── packageA
│ └── A.java

└── Ex_pack.java

// Define the package for class A


package packageA;

public class A {
public int x = 10;
public void show() {
System.out.println("X = " + x);
}}
// Import the packageA.A class
import packageA.A;

public class Ex_pack {


public static void main(String[] args) {
A obj = new A();
obj.show();
obj.x = 100;
obj.show();
}
}

OUTPUT:
X = 10
X = 100
PROGRAM 19
OBJECTIVE : Write a program to demonstrate the use of Interface in java

SOURCE CODE : Ex_interface.java

interface IA
{
public void show();
public void add(int x,int y);
}
class A implements IA
{
int x = 10,y = 20;
public void disp()
{
System.out.println("X = " + x + " " + "y = "+ y);
}
public void show(){
}
public void add(int p1,int p2){
System.out.println("Sum = " + (p1+p2));
}
}
public class Ex_interface{
public static void main(String s[])
{
A ob = new A();
ob.disp();
ob.add(10,20);
}
}
OUTPUT:
PROGRAM - 20
OBJECTIVE : Write a program to take command line input from user in java

SOURCE CODE : Ex_cmd.java

public class Ex_cmd {


public static void main(String[] args) {
String name = args[0];
int age = Integer.parseInt(args[1]);
System.out.println("your Name is : " + name);
System.out.println("your age is : " + age);
}
}

OUTPUT :
your Name is : Aneesh
your age is : 21

PROGRAM - 21
OBJECTIVE : Write a program that demonstrates the exceptional handling process using
try,catch,finally blocks in java

SOURCE CODE : Ex_excep.java

public class Ex_excep


{
public static void main(String s[])
{
try
{
int x, y, z;
x = Integer.parseInt(s[-3]);
y = Integer.parseInt(s[1]);
z = x / y;
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Enter at valid index");
}
catch (NumberFormatException e)
{
System.out.println("Not a number");
}
catch (ArithmeticException e)
{
System.out.println("divided by zero");
}
finally
{
System.out.println("finally block is executed!");
}
}
}

OUTPUT :
PROGRAM - 22
OBJECTIVE : Write a program that handles exceptions using instanceof keyword in java

SOURCE CODE : Ex_instanceof.java

public class Ex_instanceof


{
public static void main(String s[])
{
try
{
int x, y, z;
x = 100;
y = 0;
z = x / y;
System.out.println(z);
}
catch(Exception e)
{
if (e instanceof ArrayIndexOutOfBoundsException)
{
System.out.println("Enter a valid index");
}
else if (e instanceof NumberFormatException)
{
System.out.println("Not a number");
}
else if(e instanceof ArithmeticException)
{
System.out.println("divided by zero");
}
}
finally
{
System.out.println("finally block is executed!");
}
}
}
OUTPUT :
PROGRAM - 23
OBJECTIVE : Write a program that takes input using datainputstream class in java

SOURCE CODE : Ex_datainputstream.java

import java.io.*;

public class Ex_datainputstream {


public static void main(String[] args) throws IOException {
DataInputStream input = new DataInputStream(System.in);
System.out.print("Enter your name: ");
String name = input.readLine();
System.out.print("Enter your age: ");
try {
int age = Integer.parseInt(input.readLine());
System.out.println("Name: " + name);
System.out.println("Age: " + age);
} catch (NumberFormatException e) {
System.out.println("Invalid input for age. Please enter a number.");
}
}
}
PROGRAM - 24
OBJECTIVE : Write a program that sets the thread name,priority using thread class in java

SOURCE CODE : Ex_thread.java

public class Ex_thread extends Thread


{
public static void main(String s[])
{
Thread th = currentThread();
System.out.println("Status = "+th);
System.out.println("name = "+th.getName());
System.out.println("priority = "+th.getPriority());

th.setName("Thread1");
th.setPriority(8);
System.out.println("New status = "+ th);
}
}

OUTPUT :
PROGRAM - 25
OBJECTIVE : Write a program that explains the start,run and sleep methods in thread class in
java

SOURCE CODE : Ex_threads.java

class A extends Thread


{
public A(String s)
{
System.out.println("Status = "+ s);
setPriority(8);
start();
}
public void run()
{
try{
for(int i = 1;i<=5;i++)
{
System.out.println("I = "+i);
sleep(1000);
}
System.out.println("child exited!");
}
catch(Exception e){}
}
}
public class Ex_threads
{
public static void main(String s[])
{
try{
A ob1 = new A("thread1");
A ob2 = new A("thread2");
for(int j = 1;j<=5;j++)
{
System.out.println("J = "+j);
Thread.sleep(1000);
}
System.out.println("Main exited!");
}
catch(Exception e){}
}
}

OUTPUT :

You might also like