0% found this document useful (0 votes)
3 views36 pages

JAVA Lab Manual

The document is a lab manual for the Object Oriented Programming with Java Lab course at ABES Institute of Technology, outlining the vision and mission of the institute and department. It details course objectives, outcomes, and program-specific outcomes, along with a list of experiments and lab plans for practical learning. The manual includes coding examples and exercises aimed at developing students' skills in Java programming, data structures, and software development concepts.
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)
3 views36 pages

JAVA Lab Manual

The document is a lab manual for the Object Oriented Programming with Java Lab course at ABES Institute of Technology, outlining the vision and mission of the institute and department. It details course objectives, outcomes, and program-specific outcomes, along with a list of experiments and lab plans for practical learning. The manual includes coding examples and exercises aimed at developing students' skills in Java programming, data structures, and software development concepts.
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/ 36

ABES Institute of Technology

Ghaziabad
Affiliated to Dr. A.P.J. AKTU, Lucknow

LAB MANUAL
Department of Computer Science & Engineering
Subject Name: Object Oriented Programming with Java Lab
Subject Code: BCS452
Session: 2023-24

Semester: 4th
To be leading institution in technical education providing education and
Institute Vision training enabling human resource to serve nation and world at par with
global standards in education

1)Developing state of art infrastructure which also includes establishment


of centre of excellence in pursuit of academic and technical excellence

Institute Mission 2)Valuing work force inculcating belongingness and professional integrity

3)To develop human resource to solve local, regional and global problems
to make technology relevant to those who mean it most

To provide excellence by imparting knowledge to the learners


Department Vision
enabling them to become skilled professionals to be recognized as
a responsible citizen.

1) Provide quality education in the field of computer science and


engineering through experienced and qualified faculty members.
2) Motivate learners for higher studies and research oriented activities
Department
by utilizing resources of Centers of Excellence.
Mission
3) Inculcate societal values, professional ethics, team work, and
leadership qualities by having exposure at National and International
level activities.
Solve complex problems using data structures and other advanced suitable algorithms.
PSO 1

Interpret fundamental concepts of computer systems and understand its hardware and
PSO 2
software aspect.
Analyze the constraints of the existing data base management systems and get experience on
PSO 3
large-scale analytical methods in the evolving technologies.
Develop intelligent systems and implement solutions to cater the business specific
PSO 4
requirements.

Program Specific Outcomes (POs)

PO1 Engineering knowledge

PO2 Problem analysis

PO3 Design/development of solutions


PO4 Conduct investigations of complex problems
PO5 Modern tool usage
PO6 The engineer and society
PO7 Environment and sustainability
PO8 Ethics
PO9 Individual and team work
PO10 Communication
PO11 Project management and finance
PO12 Life-long learning

Graduates of the program are expected to be employed in IT industry or


PEO1
indulge in higher studies and research.
Graduates of the program are expected to exhibit curiosity to learn new technologies and
PEO2
work with ethical values and team work.
Graduates of the program are expected to design and develop innovative solutions related
PEO3
to real world problems of the society.
Course – B.Tech

Course Outcomes (COs)

Academic Year Class/Semester Course Name Course Code


2024-25 4th Object Oriented BCS-452
Programming with
JavaLab

After the successful completion of the course students will be able:

Understand the basics of object-oriented programming using JAVA..


CO1

Apply the concept of classes, Java, JDK Components and develop Simple Java Programs.
CO2

Develop Simple Java Programs using inheritance and Exception handling ,Multithreading.
CO3
Demonstrate the concept of Collections, Comparators and Interfaces.
CO4
Interpret the need for advanced Java concepts like spring, Auto Boxing and annotations.
CO5

_____________

( Name &Signature)
BCS452- Object Oriented Programming with Java

List of Experiments (Indicative & not limited to)

1. Use Java compiler and eclipse platform to write and execute java program.
2. Creating simple java programs using command line arguments
3. Understand OOP concepts and basics of Java programming.
4. Create Java programs using inheritance and polymorphism.
5. Implement error-handling techniques using exception handling and multithreading.
6. Create java program with the use of java packages.
7. Construct java program using Java I/O package.
8. Create industry oriented application using Spring Framework.
9. Test RESTful web services using Spring Boot.
10. Test Frontend web application with Spring Boot
LAB PLAN
Object oriented with Java Programming Lab(BCS 452)
S.No. Contents Experiments Lab Turn

1 Simple  print the text “Welcome”


Programs  print the area of triangle
without classes  check the number is Prime or not Turn-01
 program to display Fibonacci series
and objects,
 Program to generate a Ladder of numbers
methods  Program to find the binary of decimal
number
2 Program based on  Object instantiation
the concepts of  Constructors
classes and  Methods (defining &Calling)
 Types of constructor Turn-02
objects,
 Parameter passing tomethods
constructor,
parameterized
constructor
3 Method  Method overloading
overloading,  Constructor overloading Turn-03
constructor  Abstract classes
overloading  Interfaces
4  Single level Inheritance
Single level &  Multiple inheritance
Multi level  Super
inheritance Turn-04
 Order of Constructorcalling
 Method overriding
 Final keyword

5 Array and String  Simple programs using


Array and String Turn-05
6 Exception handling  Exception handling through-
& Packages  Try ,Catch ,Throw
Turn-06
 Finally
 Making own package
7 Multithreading  Simple programs of multithreading
 Thread Synchronization Turn-07
8 I/O& File  Input from user
Handling  Creation of file Turn-8
Reading data from file
Writing data in to file
9 Collections  Array List
 Stack Turn-9
 Queue
 Hash Set
 Tree Set
10 Spring  Create industry oriented application
Framework  Test RESTful & Frontend web application Turn-10
with Spring Boot
Content Beyond Syllabus
11 AWT  Design a Student Registration Form using
Component different AWT Component Turn -11

12 JDBC  Show the Data Base Connectivity of AWT


Connectivity components and sql Server Turn -12

COURSE OBJECTIVES:

 To write programs using abstract classes.


 To write programs for solving real world problems using java collection frame work.
 To write multithreaded programs.
 To write GUI programs using swing controls in Java.
 To introduce java compiler and eclipse platform.
 To impart hands on experience with java programming.

COURSE OUTCOMES:

 Able to write programs for solving real world problems using java collection frame work.
 Able to write programs using abstract classes.
 Able to write multithreaded programs.
 Able to write GUI programs using swing controls in Java.
Turn1
Java Basic.

1. Write a Program to print the text “Welcome to World of Java”.


Save it with name Welcome.java in your folder.

Class Welcome
{
public static void main (String args[])
{
System.out.println (“welcome to world of Java”);
}
}

2. Write a Program to print the area of triangle. Save it with name


Area.java in your folder.

class Area
{
public static void main(String args[])
{
int height =10, base=6;
float area=0.5F*base* height;
System.out.println(“area of triangle = ”+area);
}
}

3. Write a java Program to check the number is Prime or not.


Import java. util. Scanner;
class Prime
{
public static void main(String arr[])
{
int c;
Scanner in=new Scanner(System.in);
System.out.println("Enter the number to be tested for prime ");
int n=in.nextInt();
for ( c = 2 ; c <= n - 1 ; c++ )
{
if ( n%c == 0 )
{
System.out.println(n+">>>>> not prime");
break;
} }
if ( c == n )
System.out.println(n+ ">>>>Number is prime.");
}

4. Write a java Program to generate a Fibonacci Series

class FibonacciExample1
{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);//printing 0 and 1
for(i=2;i<count;++i)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
} } }

5. Write a java Program to generate a Ladder of number.

Import java.util.Scanner;

class Ladder
{
public static void main(String arr[])
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of rows");
int a=in.nextInt();
for(int i=1;i<=a;i++)
{
for(int j=1;j<=i;j++)
System.out.print(j);
for(int k=i-1;k>=1;k--)
System.out.print(k);
System.out.print("\n");
}
}
}

6. Write a java Program to Convert any decimal number into its


binary equivalent

public class Convert


{
public static void main(String[] args)
{
int decimal = 10;
String binary = decimalToBinary(decimal);
System.out.println("Decimal: " + decimal);
System.out.println("Binary: " + binary);
}
public static String decimalToBinary(int n)
{
int remainder, quotient = n;
String binaryNum = "";
while (quotient > 0) {
remainder = quotient % 2;
binaryNum
= Integer.toString(remainder) + binaryNum;
quotient = quotient / 2;
}
return binaryNum;
}
}
Lab Turn 2
Program based on the concepts of classes and objects, constructor,
parameterized constructor

7. Write a program to create a class Student with data ‘name, city


and age’ along with method printData to display the data. Create
the two objects s1 ,s2 to declare and access the values.
class Student
{
String name, city; int age;
Static int m;
Void printData()
{
System.out.println("Student name = "+name);
System.out.println("Student city = "+city);
System.out.println("Student age = "+age);
}
}
Class Stest
{
public static void main(String args[])
{
Student s1=new Student();
Student s2=new Student();
s1.name="Amit";
s1.city="Dehradun";
s1.age=22;
s2.name="Kapil";
s2.city="Delhi";
s2.age=23;
s2.printData();
s1.printData();
s1.m=20;
s2.m=22;
Student.m=27;
System.out.println("s1.m = "+s1.m);
System.out.println("s2.m = "+s2.m);
System.out.println("Student.m ="+Student.m);
}
}

8. Write a program to create a class Student2 along with two


method getData(),printData() to get the value through argument
and display the data in printData. Create the two objects s1 ,s2 to
declare and access the values from class STtest.

class Student2
{
private String name, city;
private int age;
public void getData(String x, Stringy, int t)
{
name=x;
city=y;
age=t;
}
public void printData()
{
System.out.println("Student name ="+name);
System.out.println("Student city ="+city);
System.out.println("Student age ="+age);
}
}
Class STtest
{
public static void main(String args[])
{
Student2 s1=new Student2();
Student2 s2=new Student2();
s2.getData("Kapil","Delhi",23);
s2.printData();
s1.getData("Amit","Dehradun",22);
s1.printData();
}
}

9. WAP using parameterized constructor with two parameters id


and name. While creating the objects obj1 and obj2 passed two
arguments so that this constructor gets invoked after creation of
obj1 and obj2.

class Employee
{
int empId;
String empName; //parameterized constructor with two parameters
Employee(int id, String name)
{
this.empId = id;
this.empName = name;
}
void info()
{
System.out.println("Id: "+empId+" Name: "+empName);
}
public static void main(String args[])
{
Employee obj1 = new Employee(10245,"Chaitanya");
Employee obj2 = new Employee(92232,"Negan");
obj1.info();
obj2.info();
}
}

Lab Turn 3
Method overloading, constructor overloading, Abstract Class and
Interface

10. Write a program in JAVA to demonstrate the method and


constructor overloading.

class Cs
{
Int p,q;
public Cs()
{}
public Cs(int x, int y)
{
p=x; q=y;
}
Public int add(int i, int j)
{
return (i+j);
}
Public int add(int i, int j, int k)
{
return (i+j+k);
}
public float add(float f1, float f2)
{
return (f1+f2);
}
public void printData()
{
System.out.print("p = "+p);
System.out.println(" q = "+q);
}
}
Class ConstructorOverlaoding
{
public static void main(String args[])
{
int x=2, y=3, z=4;
Cs c=new Cs();
Cs c1=new Cs(x, z );
c1.printData();
float m=7.2F, n=5.2F;
int k=c.add(x,y);
int t=c.add(x,y,z);
float ft=c.add(m, n);
System.out.println("k = "+k);
System.out.println("t = "+t);
System.out.println("ft = "+ft);
}
}

11. Write a program in JAVA to create a class Bird also declares


the different parameterized constructor to display the name of Birds.

class Bird
{
int age;
String name ;
Bird()
{
System.out.println("this is the perrot");
}
Bird(String x)
{
name=x;
System.out.println("this is the "+name);
}
Bird(int y,String z)
{
age=y;
name=z;
System.out.println("this is the "+age+"years\t"+name);
}

public static void main(String arr[])


{
Bird a=new Bird();
a.Bird();
Bird b=new Bird("maina");
Bird c=new Bird(20,"sparrow");
}
}

12. Write a program in java to generate an abstract class A also


class B inherits the class A. generate the object for class B
and display the text “call me from B”.

abstract class A
{
abstract void call();
}
class B extends A
{
public void call()
{
System.out.println("call me from B");
}
public static void main(String arr[])
{
B b=new B();
b.call();
}
}

13. Write a java program in which you will declare two interface
sum and Add inherits these interface through class A1 and
display their content.

interface sum
{
Int sm=90;
Void suma();
}

Interface add
{
int ad=89;
voidadda();
}

class A1 implements add ,sum


{
public void suma()
{
System.out.println(+sm);
}
public void adda()
{
System.out.println(+ad);
}
public static void main(String arr[])
{
A1 n= new A1();
n.adda();
n.suma();
}
}

Lab Turn 4
Single level & Multi level inheritance , Method Overriding

14. Java program to illustrate the concept of single inheritance

importjava.util.*;
importjava.lang.*;
importjava.io.*;
class one
{
Public void print_geek()
{
System.out.println("Geeks");
}
}
Class two extends one
{
Public void print_for()
{
System.out.println("for");
}
}
// Driver class
Public class Main {
Public static void main(String[] args)
{
two g = new two();
g.print_geek();
g.print_for();
g.print_geek();
}
}

15.// Java program to illustrate the concept of Multilevel inheritance

Import java.util.*;
Import java.lang.*;
Import java.io.*;
Class one
{
Public void print_geek()
{
System.out.println("Geeks");
}
}
Class two extends one
{
Public void print_for()
{
System.out.println("for");
}
}
Class three extends two
{
Public void print_geek()
{
System.out.println("Geeks");
}
}
// Drived class
Public classMain
{
Public static void main(String[] args)
{
three g = new three();
g.print_geek();
g.print_for();
g.print_geek();
}
}

16// A Simple Java program to demonstrate method overriding in java

Class Parent
{
Void show()
{
System.out.println("Parent's show()");
}

Class Child extends Parent


{ // This method overrides show() of Parent @Override
Void show()
{
System.out.println("Child's show()");
}
}

// Driver class
Class Main
{
Public static void main(String[] args)
{
// If a Parent type reference refers // to a Parent object, then Parent's // show is called

Parent obj1 = new Parent();


obj1.show();

// If a Parent type reference refers // to a Child object Child's show()


// is called. This is called RUN TIME // POLYMORPHISM.

Parent obj2 = new Child();


obj2.show();
}
}

17 Super Keyword in Java Use of super with variables

Class Vehicle
{
Int maxSpeed = 120;
}
/* sub class Car extending vehicle */ 20
Class Car extends Vehicle
{ int maxSpeed = 180;
Void display()
{ /* print maxSpeed of base class (vehicle) */
System.out.println("Maximum Speed: "+ super.maxSpeed);
}
}
/* Driver program to test */

classTest
{
Public static void main(String[] args)
{
Car small = newCar();
small.display();
}
}
Lab Turn 5
Array and String

18.Write a Java Program to to find maximum in arr[]

// Driver Class
class Test
{
// array declared
static int arr[] = {10, 324, 45, 90, 9808};

// Method to find maximum in arr[]


static int largest()
{
int i;

// Initialize maximum element


int max = arr[0];

// Traverse array elements from second and


// compare every element with current max
for (i = 1; i < arr.length; i++)
if (arr[i] > max)
max = arr[i];

return max;
}

// Driver method
public static void main(String[] args)
{
System.out.println("Largest in given array is " + largest());
}
}

19. implementation of the sort() function across different scenarios of the Arrays class

import java.util.Arrays;
class GFG {
public static void main(String args[])
{
int[] arr = { 5, -2, 23, 7, 87, -42, 509 };
System.out.println("The original array is: ");
for (int num : arr) {
System.out.print(num + " ");
}
Arrays.sort(arr);
System.out.println("\nThe sorted array is: ");
for (int num : arr) {
System.out.print(num + " ");
}
}
}
20.// Java program for addition of two matrices

import java.io.*;

class GFG {

// Function to print Matrix


static void printMatrix(int M[][],
int rowSize,
int colSize)
{
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++)
System.out.print(M[i][j] + " ");

System.out.println();
}
}

// Function to add the two matrices


// and store in matrix C
static int[][] add(int A[][], int B[][],
int size)
{
int i, j;
int C[][] = new int[size][size];

for (i = 0; i < size; i++)


for (j = 0; j < size; j++)
C[i][j] = A[i][j] + B[i][j];

return C;
}

// Driver code
public static void main(String[] args)
{
int size = 4;

int A[][] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
// Print the matrices A
System.out.println("\nMatrix A:");
printMatrix(A, size, size);

int B[][] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
// Print the matrices B
System.out.println("\nMatrix B:");
printMatrix(B, size, size);

// Add the two matrices


int C[][] = add(A, B, size);

// Print the result


System.out.println("\nResultant Matrix:");
printMatrix(C, size, size);
}
}

21./ java program to reverse a word

import java.io.*;
import java.util.Scanner;

class GFG {
public static void main (String[] args) {

String str= "Geeks", nstr="";


char ch;

System.out.print("Original word: ");


System.out.println("Geeks"); //Example word

for (int i=0; i<str.length(); i++)


{
ch= str.charAt(i); //extracts each character
nstr= ch+nstr; //adds each character in front of the existing string
}
System.out.println("Reversed word: "+ nstr);
}
}

Lab Turn 6
Exception handling & Packages

22.Write a program in java if number is less than 10 and greater than 50 it generate the exception out of
range. Else it displays the square of number

Class CustomTest
{
public static void main(String arr[])
{
try
{
int a=Integer.parseInt(arr[0]);
if(a50) throw(new outofRangeException("valid range is 10 to 50"));
{
int s=a*a;
System.out.println("Square is:"+s);
}
}catch(Exception ex)
{
System.out.println(ex);
} } }

23 Write a program in java to enter the number through command line argument if first and second
number is not entered it will generate the exception. Also divide the first number with second number
and generate the arithmetic exception.

class Divide2
{
public static void main(String arr[])
{
try
{
if(arr.length<2)

throw(new Exception("two argument must be provided"));


int a= Integer.parseInt(arr[0]);
int b=Integer.parseInt(arr[1]);
if(b==0)
throw(new Exception("second argument should be non zero"));
int c=a/b;
System.out.println("result:"+c);
}
catch(Exception e)
{
System.out.println(e);
}
}}

24. Example of package that import the packagename.*

//save by A.java

package pack;

public class A{

public void msg(){System.out.println("Hello");}

//save by B.java

package mypack;

import pack.*;

class B

public static void main(String args[])

A obj = new A();

obj.msg();

}
25. Example of package by import fully qualified name

//save by A.java
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

Lab Turn7
Multithreading

26 Write a java program in which thread sleep for 5 sec and change the name of thread.

Import java.lang.*;
Class ThreadTest extends Thread
{
static
{
Thread t = Thread.currentThread();

//Thread t=new Thread.currentThread();


System.out.println("thread test is loaded by"+t.getName()+"thread");
t.setName("vishal");
System.out.println("changed the name of thread");
System.out.println("suspending thread for 5 sec"); try
{
Thread.sleep(5000);
}
catch(Exception ex){}
}
public static void main(String arr[])
{
Thread t=Thread.currentThread();
System.out.println("main() is invoked in"+t.getName()+"thread...");
}
}

27. Write a java program for multithread in which user thread and thread started from main method
invoked at a timeeach thread sleep for 1 sec.

Class UserThread extends Thread


{
public void run()
{
Thread t=Thread.currentThread();
System.out.println("run() is invoked in"+t.getName()+"thread...");
for(int i=1;i<=10;i++)
{
System.out.println("run:"+i);
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
}
}
System.out.println("run() is completed");
}
}
classMultiThread
{
public static void main(String arr[])
{
System.out.println("main() started creating an object of user Thread. ");
UserThread t=new UserThread();
System.out.println("directly invoking run() of user thread");
t.run();
System.out.println("control back in main() ");
System.out.println("launching new thread for run() of user thread. ");
t.start();
for(int i=10;i>0;i--)
{
System.out.println("main:"+i);
try
{
Thread.sleep(1000);
}
catch(Exception e){}
}System.out.println("main() completed");
}
}

28 Write a java program for to solve producer consumer problem in which a producer produce a value and
consumer consume the value before producer generate the next value.

class Buffer
{
int value;
boolean produced=false;
public synchronized void produce(int x)
{
if(produced)
{
System.out.println("producer enter monitor out of turn..suspend. ");
try
{
wait();
}catch(Exception e)
{}
}
value=x;
System.out.println(value+"is produced");
produced=true;
notify();
}
public synchronized void consume()
{
if(! produced)
{
System.out.println("consumer enterd the monitor out of turn,suspend. ................ ");
try
{
wait();
}
catch(Exception e)
{
}
}
System.out.println(value+"is consumed"); produced=false;
notify();
}
}
class Producer extends Thread
{
Buffer buffer;
public Producer(Buffer b)
{
buffer =b;
}
Public void run()
{
{
System.out.println("producer started ,producing value. .................... ");
for(int i=1;1<=10;i++)
buffer.produce(i);
}
}

class Consumer extends Thread


{
Buffer buffer;
public Consumer(Buffer b)
{
buffer =b;
}
public void run()
{
System.out.println("consumer started,consuming value. ................");
for(int i=1;i<=10;i++)
buffer.consme();
}
}

class PC1
{
public static void main(String arr[])
{
Buffer b=new Buffer();
Producer p=new Producer(b); Consumer
c=new Consumer(b); p.start();
c.start();
}
}
Lab Turn 8

I/O and File Handling

29 .Write a java program to create a file and write the text in it and save the file.

import java.io.*;
Class CreateFile
{
public static void main(String arr[])
{
if(arr.length<1)
{
ry
System.out.println("usage:javacreatefile file name");
System.exit(0);
t }
{
BufferedReader b=new BufferedReader(new
InputStreamReader(System.in));
PrintStreamfos=new PrintStream(new FileOutputStream(arr[0]));
System.out.println("Enter text end to save");
PrintStream temp=System.out;
System.setOut(fos);
do

{
String str=b.readLine();
if(str.equalsIgnoreCase("end"));
System.out.println(str);
break;

}while(true);

System.setOut(temp);
fos.close();
b.close();
System.out.println("successfully created");
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
30.Write a java program to read a file and display the content on screen.

import java.io.*;
class input
{
public static void main(String arr[])
{
try
{
FileInputStreamf is=new FileInputStream("J.text");
int a=fis.read();
System.out.println(a);
}
catch(IOException e)
{
}
}
}
36

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy