JAVA Lab
JAVA Lab
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
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.
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
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
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
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.