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

JAVA Lab

The document describes 5 experiments on Java programming concepts: 1. Classes and objects - A program is created to demonstrate classes, objects, methods and constructors. 2. Inheritance - Programs show single, multi-level and method overriding inheritance. 3. Packages and interfaces - Examples demonstrate using packages and implementing interfaces. 4. Threads - Two methods of creating threads using Thread class/Runnable are presented. 5. Exception handling - Programs showcase try/catch, throw, and finally blocks to handle exceptions.

Uploaded by

Disha Prajapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

JAVA Lab

The document describes 5 experiments on Java programming concepts: 1. Classes and objects - A program is created to demonstrate classes, objects, methods and constructors. 2. Inheritance - Programs show single, multi-level and method overriding inheritance. 3. Packages and interfaces - Examples demonstrate using packages and implementing interfaces. 4. Threads - Two methods of creating threads using Thread class/Runnable are presented. 5. Exception handling - Programs showcase try/catch, throw, and finally blocks to handle exceptions.

Uploaded by

Disha Prajapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

EXPERIMENT: 1

AIM: Write a Program in Java to show implementation of Classes and Objects.

ALGORITHM:
STEP 1: Start the Program
STEP 2: Create Class
STEP 3: Declare the Input and Output Variables
STEP 3: Create object and access the method
STEP 4: Implement it with return type and without parameter list
STEP 5: Implement it with return type and with parameter list
STEP 6: Implement the constructor by creating classes and objects

SOURCE CODE:
class Student
{
int id;
String name;
}
class TestStudent3
{
public static void main(String args[])
{
//Creating objects
Student s1=new Student();
Student s2=new Student();
//Initializing objects
s1.id=101;
s1.name="Sonu";
s2.id=102;
s2.name="Amit";
//Printing data
System.out.println(s1.id+" "+s1.name);
System.out.println(s2.id+" "+s2.name);
}
}

OUTPUT
101 Sonu
102 Amit
class Student
{
int rollno;
String name;
void insertRecord(int r, String n)
{
rollno=r;
name=n;
}
void displayInformation()
{
System.out.println(rollno+" "+name);
}
}
class TestStudent4
{
public static void main(String args[])
{
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(111,"Karan");
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation();
}
}

OUTPUT
111 Karan
222 Aryan

RESULT:
Thus the Java program to implement classes and objects was written, executed and the output was
verified successfully.
EXPERIMENT: 2

AIM: write a program in Java to show the implementation of Inheritance.

ALGORITHM:
STEP 1: Start the Program
STEP 2: Declare and Initialize the input variables
STEP 3: Create the class Bicycle
STEP 4: Create the constructor for Bicycle class.
STEP 5: Create various methods for Subclass
STEP 6: In derived class extend the previous class
STEP 7: In main class specify the values and create the object
STEP 8: Stop

SOURCE CODE:
class Animal //Single level Inheritance
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class TestInheritance
{
public static void main(String args[])
{
Dog d=new Dog();
d.bark();
d.eat();
}
}

OUTPUT
barking...
eating...
class Animal //Multi level Inheritance
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class BabyDog extends Dog
{
void weep()
{
System.out.println("weeping...");
}
}
class TestInheritance2
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}

OUTPUT
weeping...
barking...
eating...

Result:
Thus the program in java to implement Inheritance is executed successfully and the output is verified.
EXPERIMENT: 3

AIM: Write a program in Java to show the implementation of Packages and Interfaces.

ALGORITHM (PACKAGE IMPLEMENTATION):


STEP 1. START
STEP 2. Import package
STEP 3. Create Class A
STEP 4. Create Class B
STEP 5. Get output.

SOURCE CODE:
package pack;
public class A
{
public void msg()
{
System.out.println("Hello");
}
}
//save by B.java
package mypack;
class B
{
public static void main(String args[])
{
pack.A obj = new pack.A(); //using fully qualified name
obj.msg ();
}
}

Output
Hello
ALGORITHM (INTERFACE IMPLEMENTATION):
STEP 1: Start the Program
STEP 2: Import the GUI packages
STEP 3: Create new frame and set sizes
STEP 4: In showeventdemo add button and listeners
STEP 5: In buttonclicklistener check whether the button is clicked
STEP 6: STOP

SOURCE CODE
import java.io.*;
// A simple interface
interface In1
{
// public, static and final
final int a = 10;
// public and abstract
void display();
}
// A class that implements the interface.
class TestClass implements In1
{
// Implementing the capabilities of
// interface.
public void display()
{
System.out.println ("Geek");
}
// Driver Code
public static void main (String[] args)
{
TestClass t = new TestClass ();
t.display ();
System.out.println (a);
}
}

RESULT:
Thus the program in java to implement Interface is executed successfully and the output is verified.
EXPERIMENT: 4

AIM: Write a Program in Java to show the implementation of threads.

ALGORITHM:
STEP 1: Start the Program
STEP 2: Declare and Initialize the Variables
STEP 3: Create the class Thread
STEP 4: Declare the method run
STEP 5: In main method create the object and start
STEP 6: Stop

SOURCE CODE:
class Multi extends Thread // Implementation by extends thread class
{
public void run()
{
System.out.println ("thread is running...");
}
public static void main(String args[])
{
Multi t1=new Multi ();
t1.start ();
}
}

OUTPUT
thread is running..
class Multi3 implements Runnable // Implementation by Runnable Interface
{
public void run()
{
System.out.println ("thread is running...");
}
public static void main(String args[])
{
Multi3 m1=new Multi3 ();
Thread t1 =new Thread (m1); // Using the constructor Thread(Runnable r)
t1.start ();
}
}

OUTPUT
thread is running..

RESULT:
Thus the Java program using Thread class and runnable interface for implementing Thread was
written, executed and the output was verified successfully.
EXPERIMENT: 5

AIM: Write a Program in Java using Exception Handling mechanisms.

SOURCE CODE:
class Main // Exception handling using try…catch
{
public static void main(String[] args)
{
try
{
// code that generate exception
int divideByZero = 5 / 0;
System.out.println ("Rest of code in try block");
}
catch (ArithmeticException e)
{
System.out.println ("ArithmeticException => " + e.getMessage());
}
}
}

OUTPUT
ArithmeticException => / by zero

class Main // Exception handling using finally block


{
public static void main(String[] args)
{
try
{
// code that generates exception
int divideByZero = 5 / 0;
}
catch (ArithmeticException e)
{
System.out.println ("ArithmeticException => " + e.getMessage());
}
finally
{
System.out.println ("This is the finally block");
}
}
}
OUTPUT
ArithmeticException => / by zero
This is the finally block

class Main // Exception handling using throw


{
// declareing the type of exception
public static void findFile() throws IOException
{
// code that may generate IOException
File newFile = new File("test.txt");
FileInputStream stream = new FileInputStream(newFile);
}
public static void main(String[] args)
{
try
{
findFile();
}
catch (IOException e)
{
System.out.println (e);
}
}
}

OUTPUT
java.io.FileNotFoundException: test.txt (The system cannot find the file specified).

RESULT:
Thus the Java program to implement Exception handling was written, executed and the output was
verified successfully.

You might also like