Java Lab
Java Lab
Java Lab
AIM:
Write a Program in Java to implement the Classes and Objects.
ALGORITHM:
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="Sonoo";
s2.id=102;
s2.name="Amit";
//Printing data
System.out.println(s1.id+" "+s1.name);
System.out.println(s2.id+" "+s2.name);
}
}
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:
101 Sonoo
102 Amit
111 Karan
222 Aryan
55
45
Thus the Java program to implement classes and objects was written, executed and the output
was verified successfully.
EX.No.2 CONSTRUCTORS & DESTRUCTORS
AIM:
To write a program in java with Constructors and destructors
ALGORITHM:
STEP 1: Start the Program
STEP 2: Declare and Initialize the input variables
STEP 3: Create the Constructors
STEP 4: Create various methods for Subclass
STEP 5: In derived class extend the previous class
STEP 6: In main class specify the values and create the object
STEP 7: Stop
SOURCE CODE:
// import java.io.*;
class Geek
int num;
String name;
Geek()
System.out.println("Constructor
called");
}
class GFG
System.out.println(geek1.name);
System.out.println(geek1.num);
}
OUTPUT:
Constructor called
null
Param
RESULT:
Thus the program in java with Constructors and destructors is executed successfully and the
output is verified
EX.No.3 METHOD OVERLOADING
AIM:
STEP 7: In main function create the object and call the methods
SOURCE CODE:
OUTPUT:
30
60
31.0
#Adding Numbers
return x + y;
return x + y;
}
OUTPUT:
int: 13
double: 10.559999999999999
RESULT:
Thus the Java program to implement method overloading and the output was verified
successfully.
EX. No.4 INHERITANCE
AIM:
ALGORITHM:
SOURCE CODE:
class Bicycle {
// the Bicycle class has two fields
public int gear;
public int speed;
// derived class
class MountainBike extends Bicycle {
// driver class
public class Test {
public static void main(String args[])
{
No of gears are 3
Speed of bicycle is 100
Seat height is 25
Result:
Thus the program in java to implement Inheritance is executed successfully and the output is verified.
EX.No.5 INTERFACE
AIM:
ALGORITHM:
STEP 1: Start the Program
SOURCE CODE
import java.io.*;
// A simple interface
interface In1
{
// public, static and final
final int a = 10;
// 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
EX. No. 6 MULTI THREADING
AIM:
To write a Program in Java to implement Multi Thread.
ALGORITHM:
STEP 8: Stop
SOURCE CODE:
try
{
// Displaying the thread that is running
System.out.println(
+ " is running");
catch (Exception e) {
// Throwing an exception
System.out.println("Exception is caught");
// Main Class
MultithreadingDemo object
= new MultithreadingDemo();
object.start();
OUTPUT:
Thread 15 is running
Thread 14 is running
Thread 16 is running
Thread 12 is running
Thread 11 is running
Thread 13 is running
Thread 18 is running
Thread 17 is running
// Java code for thread creation by implementing
// the Runnable Interface
class MultithreadingDemo implements Runnable {
public void run()
{
try {
// Displaying the thread that is running
System.out.println(
"Thread " + Thread.currentThread().getId()
+ " is running");
}
catch (Exception e) {
// Throwing an exception
System.out.println("Exception is caught");
}
}
}
// Main Class
class Multithread {
public static void main(String[] args)
{
int n = 8; // Number of threads
for (int i = 0; i < n; i++) {
Thread object
= new Thread (new Multithreading Demo());
object.start ();
}
}}
OUTPUT:
Thread 13 is running
Thread 11 is running
Thread 12 is running
Thread 15 is running
Thread 14 is running
Thread 18 is running
Thread 17 is running
Thread 16 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.
EX. No. 7 PACKAGES
AIM:
To write a program in Java to implement Packages
ALGORITHM:
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
Result:
Thus the program in java to implement Packages is executed successfully and the output is verified
EX. No. 8 CREATING JAVA APPLETS
AIM:
To write a program in java to create Java Applets
ALGORITHM:
STEP 5: STOP
SOURCE CODE:
import java.applet.Applet;
import java.awt.Graphics;
/*
<applet code="HelloWorld" width=200 height=60>
</applet>
*/
// HelloWorld class extends Applet
public class HelloWorld extends Applet
{
// Overriding paint() method
@Override
public void paint(Graphics g)
{
g.drawString("Hello World", 20, 20);
}
}
Output
appletviewer
HelloWorld
RESULT:
Thus the program in Java to create Java Applets is executed successfully and the output is verified