Java Lab Manuals
.............................
..................................
1. Write simple programs based on basis syntactical constructs of Java like:-
a: - Operators and Expressions
Code: -
import java.util.Scanner;
class Add2Numbers
{
public static void main(String args[])
{
System.out.println("Hello World !");
int a,b,c;
Scanner sc = new Scanner(System.in);
System.out.println("Enter 2 no = ");
a = sc.nextInt();
b = sc.nextInt();
c = a+b;
System.out.println("sum = "+c);
sc.close();
}
}
Output: -
Hello World !
Enter 2 no =
10 20
sum = 30
b: - Looping Statements
Code: -
import java.util.Scanner;
class AddRange
{
public static void main(String args[])
{
int a,b,s=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter The Range = ");
b = sc.nextInt();
for(a=0; a<=b; a++)
s = s+a;
System.out.println("Sum = "+s);
}
}
Output: -
Enter The Range =
10
Sum = 55
c: - Decision Making Statements
Code: -
import java.util.Scanner;
class Compare
{
public static void main(String args[])
{
System.out.println("Hello World !");
int a,b;
Scanner sc = new Scanner(System.in);
System.out.println("Enter 2 no = ");
a = sc.nextInt();
b = sc.nextInt();
if(a>b)
System.out.println("Greater Number Is = " + a);
else
System.out.println("Greater Number Is = " + b);
}
}
Output: -
Hello World !
Enter 2 no =
10
20
Greater Number Is = 20
d: - Type Casting
Code: -
class Test
{
public static void main(String args[])
{
byte b = 50;
b = (byte)(b * 2);
System.out.println(b);
}
}
Output:
Int value 100
Long value 100
Float value 100.0
2. Write a simple Java Program to demonstrate use of Command Line Arguments in Java
Code: -
class A
{
public static void main(String args[])
{
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}
}
Compile By> javac A.java
Run By> java A sonoo jaiswal 1 3 abc
Output: -
sonoo
jaiswal
1
3
abc
3. Write a Java Program to define a class, define its constructor, overload the constructors and instantiate its object.
Code: -
class Volume
{
int width,height,depth;
Volume()
{
width = 10;
height = 20;
depth = 30;
}
Volume(int x, int y, int z)
{
width = x;
height = y;
depth = z;
}
void volumeCalculate()
{
System.out.println("Volume Is = "+(width*height*depth));
}
}
class VolumeFinder
{
public static void main(String args[])
{
Volume b1 = new Volume();
b1.volumeCalculate();
Volume b2 = new Volume(1,2,3);
b2.volumeCalculate();
}
}
Output: -
Volume Is = 6000
Volume Is = 6
4. Write a Java Program to define a class, define instance method for setting and retrieving values of instance variables and instantiate its object.
Code: -
class A
{
int i,j;
A(int x, int y)
{
i = x;
j = y;
}
void swap(A x)
{
int t;
t = x.i;
x.i = x.j;
x.j = t;
}
}
class SwapNumbers
{
public static void main(String args[])
{
A a1 = new A(1,2);
System.out.println("Before Swap = " + a1.i + " " + a1.j);
a1.swap(a1);
System.out.println("After Swap = " + a1.i + " " + a1.j);
}
}
Output: -
Before Swap = 1 2
After Swap = 2 1
5. Write a java program to define class, define instance methods and overload them and use them for dynamic method invocation.
Code: -
class a
{
void add(int a, int b)
{
System.out.println("Sum = "+(a+b));
}
void add(float x, float y)
{
System.out.println("Sum = "+(x+y));
}
}
class Ambiguous
{
public static void main(String args[])
{
a obj = new a();
obj.add(2,3);
obj.add(2.5f,3.6f);
}
}
Output: -
Sum = 5
Sum = 6.1
6.Write a Java Program to demonstrate use of sub class.
Code: -
class A
{
void show()
{
System.out.println("I Am In A");
}
}
class B extends A
{
void show()
{
System.out.println("I Am In B");
}
}
class BasicInheritance
{
public static void main(String args[])
{
B b1 = new B();
b1.show();
}
}
Output: -
I Am In B
7. Write a Java Program to demonstrate use of nested class.
Code: -
class OuterClass
{
static int outer_x = 10;
int outer_y = 20;
private int outer_private = 30;
class InnerClass
{
void display()
{
System.out.println("outer_x = " + outer_x);
System.out.println("outer_y = " + outer_y);
System.out.println("outer_private = " + outer_private);
}
}
}
public class InnerClassDemo
{
public static void main(String[] args)
{
OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
innerObject.display();
}
}
Output:
outer_x = 10
outer_y = 20
outer_private = 30
8. Write a Java Program to practice: -
a. Using of Single Dimensional Array.
Code: -
class oneDimensionalArray {
public static void main(String args[])
{
int[] a;
a = new int[3];
for (int i = 0; i < 3; i++)
{
a[i] = 100;
System.out.println(a[i]);
}
}
}
Output:
100
100
100
b. Use of Multidimensional Array.
Code: -
import java.util.Scanner;
class MatrixAdd
{
public static void main(String srgs[])
{
int i,j;
int a[][] = new int[3][3];
int b[][] = new int[3][3];
int sum[][] = new int[3][3];
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1st Matrix: -");
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Enter 2nd Matrix: -");
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
b[i][j]=sc.nextInt();
}
}
System.out.println("Sum Of The Matrices = ");
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
sum[i][j]=a[i][j]+b[i][j];
System.out.print(sum[i][j]+" ");
}
System.out.print("\n");
}
}
}
Output: -
Enter 1st Matrix: -
1 2 3
4 5 6
7 8 9
Enter 2nd Matrix: -
9 8 7
6 5 4
3 2 1
Sum Of The Matrices =
10 10 10
10 10 10
10 10 10
9. Write a Java Program to implement array of objects.
Code: -
class Student
{
public int roll_no;
public String name;
Student(int roll_no, String name)
{
this.roll_no = roll_no;
this.name = name;
}
}
public class GFG
{
public static void main (String[] args)
{
Student[] arr;
arr = new Student[5];
arr[0] = new Student(1,"aman");
arr[1] = new Student(2,"vaibhav");
arr[2] = new Student(3,"shikar");
arr[3] = new Student(4,"dharmesh");
arr[4] = new Student(5,"mohit");
for (int i = 0; i < arr.length; i++)
System.out.println("Element at " + i + " : " +
arr[i].roll_no +" "+ arr[i].name);
}
}
Output:
Element at 0 : 1 aman
Element at 1 : 2 vaibhav
Element at 2 : 3 shikar
Element at 3 : 4 dharmesh
Element at 4 : 5 mohit
10. Write a Java Program to practice: -
a. Using String class and its Methods.
Code: -
class print
{
int roll;
String name;
print(int roll, String name)
{
this.roll = roll;
this.name = name;
}
void show()
{
System.out.println("Roll: - " + roll + "\n" + "Name: - " + name);
}
}
class ThisString
{
public static void main(String args[])
{
print S1 = new print(31,"Subhajit Ghosh");
S1.show();
}
}
Output: -
Roll: - 31
Name: - Subhajit Ghosh
b. Using String Buffer class and its Methods.
Code: -
import java.io.*;
class GFG {
public static void main(String[] args)
{
StringBuffer s = new StringBuffer("GeeksforGeeks");
int p = s.length();
int q = s.capacity();
System.out.println("Length of string GeeksforGeeks=" + p);
System.out.println("Capacity of string GeeksforGeeks=" + q);
}
}
Output:
Length of string GeeksforGeeks=13
Capacity of string GeeksforGeeks=29
11. Write a Java Program to implement Vector class and its methods.
Code: -
import java.util.*;
class VectorDemo1
{
public static void main(String[] args)
{
Vector v = new Vector();
v.add(2.5);
v.add(5);
v.add("Geeks");
v.add("For Geeks");
v.add(10);
System.out.println("Vector Is : " + v);
}
}
12. Write a Java Program to implement Wrapper classes and their methods.
a. Code: - Boolean Wrapper
class BooleanWrapper
{
public static void main(String[] args)
{
boolean a = true;
Boolean i1 = new Boolean(a);
System.out.println(i1);
Boolean i2 = new Boolean(false);
boolean b=i2;
System.out.println(b);
}
}
b. Code: - ByteWrapper
class ByteWrapper
{
public static void main(String[] args)
{
byte a = 5;
Byte i1 = new Byte(a);
System.out.println(i1);
Byte i2 = new Byte("10");
byte b=i2;
System.out.println(b);
}
}
c. Code: - CharacterWrapper
class CharacterWrapper
{
public static void main(String[] args)
{
char a = 'A';
Character i1 = new Character(a);
System.out.println(i1);
Character i2 = new Character('B');
char b=i2;
System.out.println(b);
}
}
d. Code: - DoubleWrapper
class DoubleWrapper
{
public static void main(String[] args)
{
double a = (float)5.10;
Double i1 = new Double(a);
System.out.println(i1);
Double i2 = new Double(10.20);
double b=i2;
System.out.println(b);
}
}
e. Code: -FloatWrapper
class FloatWrapper
{
public static void main(String[] args)
{
float a = (float)5.10;
Float i1 = new Float(a);
System.out.println(i1);
Float i2 = new Float(10.20);
float b=i2;
System.out.println(b);
}
}
f. Code: - IntegerWrapper
class IntegerWrapper
{
public static void main(String[] args)
{
int a=5;
Integer i1 = new Integer(a);
System.out.println(i1);
Integer i2 = new Integer(10);
int b=i2;
System.out.println(b);
}
}
g. Code: -ShortWrapper
class ShortWrapper
{
public static void main(String[] args)
{
short a = 5;
Short i1 = new Short(a);
System.out.println(i1);
Short i2 = new Short("10");
short b=i2;
System.out.println(b);
}
}
h. Code: -
import java.util.ArrayList;
class WrapperDemo1
{
public static void main(String[] args)
{
char ch = 'A';
//Autoboxing - Primitive To Character Object Conversion
Character A = ch;
ArrayList <Integer> intake = new ArrayList <Integer>();
//Autoboxing Because Arraylist Stores Only Objects
intake.add(10);
//Printing The Values From Object
System.out.println(intake.get(0));
}
}
i. Code: -
import java.util.ArrayList;
class WrapperDemo2
{
public static void main(String[] args)
{
char ch = 'A';
//Unboxing - Character Object To Primitive Conversion
Character A = ch;
ArrayList <Integer> intake = new ArrayList <Integer>();
intake.add(10);
//Unboxing Because Get Method Returns An Integer Object
int num = intake.get(0);
//Printing The Values From Primitive Data Types
System.out.println(num);
}
}
13. Write a Java Program to implement single inheritance by applying various access controls to its data members and methods.
Code: -
class A
{
int i;
A (int x)
{
i = x;
}
}
class B extends A
{
int j;
B(int x, int y)
{
super(x);
j = y;
}
void show()
{
System.out.println("i = " + i + " ; j = "+j);
}
}
class SingleInheritance
{
public static void main(String args[])
{
B b1 = new B(1,2);
b1.show();
}
}
Output: -
i = 1 ; j = 2
14. Write a Java Program to implement multilevel inheritance by applying various access controls to its data members and methods.
Code: -
class A
{
int i;
A (int x)
{
i = x;
}
}
class B extends A
{
int j;
B(int x, int y)
{
super(x);
j = y;
}
}
class C extends B
{
int k;
C(int x, int y, int z)
{
super(x,y);
k = z;
}
void show()
{
System.out.println("i = " + i + " ; j = " + j + " ; k = " + k);
}
}
class MultipleInheritance
{
public static void main(String args[])
{
C c1 = new C(1,2,3);
c1.show();
}
}
Output: -
i = 1 ; j = 2 ; k = 3
15. Write a Java Program to implement inheritance and demonstrate use of method overriding.
Code: -
class A
{
void show()
{
System.out.println("I Am In A");
}
}
class B extends A
{
void show()
{
System.out.println("I Am In B");
}
}
class BasicInheritance
{
public static void main(String args[])
{
B b1 = new B();
b1.show();
}
}
Output: -
I Am In B
16. Write a program to demonstrate: -
a. Use of implementing interfaces.
Code: -
interface figure
{
void area();
}
class square implements figure
{
int a = 10, b = 10;
public void area()
{
System.out.println("Area Of Square Is = "+(a*b));
}
}
class rectangle implements figure
{
int a = 5, b = 10;
public void area()
{
System.out.println("Area Of Rectangle Is = "+(a*b));
}
}
class Implement1
{
public static void main(String args[])
{
square s = new square();
rectangle r = new rectangle();
s.area();
r.area();
}
}
Output: -
Area Of Square Is = 100
Area Of Rectangle Is = 50
b. Use of extending interfaces.
Code: -
interface A
{
void show1();
}
interface B extends A
{
void show2();
}
interface C
{
void show3();
}
class D implements B,C
{
public void show1()
{
System.out.println("Welcome");
}
public void show2()
{
System.out.println("To");
}
public void show3()
{
System.out.println("CST");
}
}
class ComplexImplement
{
public static void main(String args[])
{
D d1 = new D();
d1.show1();
d1.show2();
d1.show3();
}
}
Output: -
Welcome
To
CST
17. Write a Java Program to implement the concept of importing classes from user defined package and creating packages.
Code: -
package pack1;
public class PrintText
{
public void msg()
{
System.out.println("Hello");
}
}
import pack1.*;
class PrintTextExecute
{
public static void main(String args[])
{
package1 obj = new package1();
obj.msg();
}
}
Output: -
Hello
18. Write a program to implement the concept of threading
Code: -
class ThreadDemo1
{
public static void main(String[] args)
{
Thread t = Thread.currentThread();
System.out.println("Current Thread : " + t);
t.setName("My Thread");
System.out.println("Afterr Name Change : " + t);
try
{
for(int n=5; n>0 ; n--)
{
System.out.println(n);
Thread.sleep(1000);
}
} catch(InterruptedException e)
{
System.out.println("Main Thread Interrupted");
}
}
}
Output: -
Current Thread : Thread[main,5,main]
Afterr Name Change : Thread[My Thread,5,main]
5
4
3
2
1
19. Write a program to implement the concept of Exception Handling.
a. Use predefined exception
Code: -
class TryCatchDemo1
{
public static void main(String[] args)
{
try
{
int d=0;
int a=10/d;
System.out.println("This Will Not Be Printed");
} catch (ArithmeticException e)
{
System.out.println("Division By Zero : " + e);
}
System.out.println("End Of The Program");
}
}
Output: -
Division By Zero : java.lang.ArithmeticException: / by zero
End Of The Program
b. By creating user defined exceptions
Code: -
class ThrowDemo1
{
static void demoprog()
{
try
{
throw new NullPointerException("Demo");
} catch(NullPointerException e)
{
System.out.println("Caught Inside Demoprog");
throw e;
}
}
public static void main(String[] args)
{
demoprog();
}
}
Output: -
Caught Inside Demoprog
Exception in thread "main" java.lang.NullPointerException: Demo
at ThrowDemo1.demoprog(ThrowDemo1.java:7)
at ThrowDemo1.main(ThrowDemo1.java:16)
20. Write a program to implement the concept of Synchronization for: -
a. Object Synchronization.
b. Method Synchronization.
Code: - No Program Given
21. Write a program using Applet: -
a. To display a message in the Applet.
Code: -
import java.applet.*;
import java.awt.*;
public class AppletDemo1 extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello World", 50,50);
}
}
b. For configuring Applets by passing parameters.
Code: -
import java.applet.Applet;
import java.awt.*;
public class Ellipse extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawOval(100,100,150,100);
}
}
22. Write programs for using Graphics class: -
a. To display basic shapes and fill them.
b. Draw different items using basic shapes
Code: - (a+b)
import java.awt.*;
import java.applet.*;
public class Figures extends Applet
{
public void paint(Graphics g)
{
Color c1 = new Color(255,0,0);
Color c2 = new Color(0,255,0);
Color c3 = new Color(0,0,255);
g.setColor(c1);
g.drawLine(75, 100, 225, 100);
g.drawLine(255, 100, 405, 100);
g.setColor(c2);
g.drawOval(120, 150, 149, 149);
g.fillOval(210, 150, 150, 150);
g.setColor(c3);
g.drawRect(150, 350, 99, 149);
g.fillRect(250, 350, 100, 150);
g.setColor(Color.CYAN);
g.drawRoundRect(150, 550, 99, 149, 50, 50);
g.fillRoundRect(250, 550, 99, 149, 50, 50);
}
}
c. Set background and foreground colours.
Code: - No Program Given
23. Write program to demonstrate use of I/O streams
Input Stream: -
Code: -
import java.io.FileInputStream;
public class FileInputStreamDemo
{
public static void main(String[] args)
{
try
{
FileInputStream fin = new FileInputStream("File.txt");
int i = 0;
while((i = fin.read()) != -1)
{
System.out.print((char)i);
}
fin.close();
} catch(Exception e)
{
System.out.println(e);
}
}
}
Output: -
Welcome To CS&T
Output Stream: -
Code: -
import java.io.FileOutputStream;
public class FileOutputStreamDemo1
{
public static void main(String[] args)
{
try
{
FileOutputStream fout = new FileOutputStream("File.txt");
String s = "Welcome";
byte b[] = s.getBytes();
fout.write(b);
fout.close();
System.out.println("Success");
} catch(Exception e)
{
System.out.println(e);
}
}
}
Output: -
Success
24. Write an Application program / Applet to make connectivity with database using JDBC API.
Code: -
import java.sql.*;
class DatabaseDemo02
{
public static void main(String[] args)
{
try
{
String database = "Student.mdb";
String url = "jdbc:odbc:Student";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("url");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from Student");
while(rs.next())
System.out.println(rs.getString(1) + " " + rs.getString(2));
con.close();
} catch (Exception e)
{
System.out.println(e);
}
}
}
Output: -
A
B
C
D
25. Write an Application program / Applet to send queries through JDBC bridge & handle result.
Code: -
import java.sql.*;
class DatabaseDemo01
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:Student");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from Student");
while(rs.next())
System.out.println(rs.getString(1) + " " + rs.getString(2));
con.close();
} catch (Exception e)
{
System.out.println(e);
}
}
}
Output: -
A
B
C
D
Pages
- Model question papers1
- PHP - Placement Papers
- DOT(.) Net Placement Papers
- Software Engineering - Placement Papers
- Algorithms - Placement Papers
- JAVA - Placement Papers
- OS Placement Papers
- DBMS Placement Papers
- C Placement papers
- C++ Placement Papers
- Environment & Ecology
- Gate question with answer
- computer organization
- Model question-papers 2
- MICROPROCESSOR AND MICROCONTROLLER
- HOME
- 2018 model question papers for BCA,CST,MCA and & B.Tech students
- C Language 5 years solution of MAKAUT exam ( for BCA only)
- Python for Data Science - Course for Beginners (Learn Python, Pandas, NumPy, Matplotlib)
No comments:
Post a Comment