Professional Documents
Culture Documents
Java Programming (Bcse 2333) Lab File: Submitted by
Java Programming (Bcse 2333) Lab File: Submitted by
Submitted by-
YASH
MALHOTRA
20SCSE1010205
B.Tech
C.S.E
3rd sem
28/11/2021
INDEX
6 Control Statement I WAP in java to find out the first N even numbers.
7 Control Statement II WAP in java to find out factorial of a number
8 Control Statement III WAP in java to find out the Fibonacci series
9 Arrays I WAP in java to sort elements in 1D array in ascending order.
13 Class,methods and objects Create a class Book with the following information. Member
variables : name (String), author (of the class Author you have
just created), price (double), and qtyInStock (int)
[Assumption: Each book will be written by exactly one Author]
Parameterized Constructor: To initialize the variables Getters and
Setters for all the member variables
In the main method, create a book object and print all details of the
book (including the author details)
14
15 Methods and OOPS I WAP in java that implements method overloading. 17. WAP that
shows passing object as parameter. 1
16 Methods and OOPS II WAP in java to show that the value of non static
variable is not visible to all the instances, and therefore cannot
be used to count the number of instances.
17 Constructor overloading Create a class Shape and override area() method
to calucate area of rectangle, square and circle
18 Static Keyword Create a class Student with non-static variable name and static
variable Course. Create 3 objects of class
,store and display information.
19 Access modifiers WAP in java to demonstrate the properties of public private and
protected variables and methods(with package).
20
21 Wrapper Class I WAP in javato show autoboxing and unboxing
22
23 Inheritence I a)Create a class named ‘Animal’ which includes methods like
eat() and sleep().
Create an instance of Animal class and invoke the eat and sleep
methods using this object.
Create an instance of Bird class and invoke the eat, sleep and fly
methods using this object.
Inheritance
33 Collections II WAP in java WAP in java and run applications that use "List"
Collection objects
34 Collections III Create a HashMap<Rollno, Name>. Display Map, take
rollno from user and display name.
35 JDBC I WAP to do CRUD operation in java
EXPERIMENT NO.1 (a)
CODE:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output
Hello, World!
1. boolean type
The boolean data type has two possible values, either true or
false.
Default value: false.
class Main {
public static void main(String[] args) {
The byt data type can have values from -128 to 127 (8-bit signed two's complement
e integer).
If it's certain that the value of a variable will be within -128 to 127, then it is used instead of int to save
memory.
Default value: 0
class Main {
public static void main(String[] args) {
byte range;
range = 124;
System.out.println(range); // prints 124
}
}
3. short type
The short data type in Java can have values from -32768 to 32767 (16-bit signed two's complement
integer).
If it's certain that the value of a variable will be within -32768 and 32767, then it is used instead of other
class Main {
public static void main(String[] args) {
short temperature;
temperature = -200; System.out.println(temperature); // prints -200
}
}
4. int type
The int data type can have values from -231 to 231-1 (32-bit signed two's complement integer).
If you are using Java 8 or later, you can use an unsigned 32-bit integer. This will have a minimum value of 0 and
class Main {
public static void main(String[] args) {
5. long type
The long data type can have values from -263 to 263-1 (64-bit signed two's complement integer).
If you are using Java 8 or later, you can use an unsigned 64-bit integer with a minimum value of 0 and a
Default value: 0
class LongExample {
public static void main(String[] args) {
Notice, the use of L at the end of -42332200000. This represents that it's an integer of the long type.
6. double type
class Main {
public static void main(String[] args) {
The float data type is a single-precision 32-bit floating-point. Learn more about single-precision and
class Main {
public static void main(String[] args) {
Notice that we have -42.3f instead of -42.3in the above program. It's -42.3 is a doubl literal.
used because e
To tell the compiler to -42.3 as float rather than double, you need to use f or F.
treat
If you want to know about single-precision and double-precision, visit
8. char type
The minimum value of the char data type is '\u0000' (0) and the maximum value of the is '\uffff'.
Default value: '\u0000'
class Main {
public static void main(String[] args) {
class Precedence {
public static void main(String[] args) {
System.out.println(result);
}
}
Output:
2
import java.util.Scanner;
public class IntegerSumExample2
{
public static void main(String[] args)
{
System.out.println("Enter the Two numbers for addtion: ");
Scanner readInput = new Scanner(System.in);
int a = readInput.nextInt(); int
b = readInput.nextInt();
readInput.close();
System.out.println("The sum of a and b is = " + Integer.sum(a, b));
}
}
Output:
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}
}
Output: sonoo
jaiswal
1
3
abc
EXPERIMENT NO.5
Output:
Output:
Output:
Output:
0 1 1 2 3 5 8 13 21 34
public class SortAsc
{
public static void main(String[] args)
{
int [] arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;
System.out.println("Elements of original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
for (int i = 0; i < arr.length; i++) { for
(int j = i+1; j < arr.length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println();
System.out.println("Elements of array sorted in ascending order: "); for
(int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
}
}
Output:
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();//new line
}
}
}
Output:
2 6 8
4 8 6
4 6 9
public class MatrixAdditionExample
{
public static void main(String args[])
{
int a[][]={{1,3,4},{2,4,3},{3,4,5}};
int b[][]={{1,3,4},{2,4,3},{1,2,4}};
int c[][]=new int[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
Output:
2 6 8
4 8 6
4 6 9
EXPERIMENT NO.11
Output:
els
e
{
}
return true;
if (res == true)
System.out.println( "Same" );
else
System.out.println( "Not Same" );
}
str1 = "Geeks";
str2 = "geeks";
equalIgnoreCaseUtil(str1, str2);
str1 = "Geek";
str2 = "geeksforgeeks";
equalIgnoreCaseUtil(str1, str2);
}
}
Output:
EXPERIMENT NO.13
package database;
this.title=title;
this.author=author;
this.year=year;
this.publisher=publisher;
this.cost=cost;
return title;
return author;
return year;
return publisher;
}
return cost;
this.title=title;
this.author=author;
this.year=year;
this.publisher=publisher;
this.cost=cost;
return "The details of the book are: " + title + ", " + author + ", " + year + ", " + publisher + ", " + cost;
}
EXPERIMENT NO.15
Box(int l, int b)
{
length=l; breadth=b; height=2;
System.out.println("Initialized with parameterized constructor having 2 params");
Output:
class OverloadDemo
void area(float x)
void area(double x)
double z = 3.14 * x * x;
class Overload
ob.area(5);
ob.area(11,12);
ob.area(2.5);
}
Output-
EXPERIMENT NO.18
class Student{
int rollno;//instance variable
String name;
static String college ="ITS";//static variable
Student(int r, String n){
rollno = r;
name = n;
}
Output:
1. Private Data members and methods are only accessible within the class
2. Class and Interface cannot be declared as private
3. If a class has private constructor then you cannot create the object of that class from
outside of the class.
This example throws compilation error because we are trying to access the private data member and
method of class ABC in the class Example. The private data member and method are only accessible
within the class.
class ABC{
private double num = 100;
private int square(int a){
return a*a;
}
}
public class Example{
public static void main(String args[]){
ABC obj = new ABC();
System.out.println(obj.num);
System.out.println(obj.square(10));
}
}
Output:
Protected data member and method are only accessible by the classes of the same package and the
subclasses present in any package. You can also say that the protected access modifier is similar to default
access modifier with one exception that it has visibility in sub classes.
Classes cannot be declared protected. This access modifier is generally used in a parent child relationship.
package abcpackage;
public class Addition {
Test.java
package xyzpackage;
import abcpackage.*;
class Test extends Addition{
public static void main(String args[]){
Test obj = new Test();
System.out.println(obj.addTwoNumbers(11, 22));
}
}
Output:
33
EXPERIMENT NO.21
class BoxingExample1
{
public static void main(String args[])
{
int a=50;
Integer a2=new Integer(a);
Integer a3=5
System.out.println(a2+" "+a3);
}
}
Output-
Output:50 5
EXPERIMENT NO.23
class Animal
System.out.println("eat method");
System.out.println("sleep method");
super.eat();
System.out.println("overide eat");
super.sleep();
System.out.println("override sleep");
}
public void fly()
class Animals
a.eat();
a.sleep();
b.eat();
b.sleep();
b.fly();
}
EXPERIMENT NO.24
class Vehicle
{
void run(){System.out.println("Vehicle is running");}
}
Output:
import java.util.*;
abstract class Diagram
{
private int d1,d2;
abstract public void areaCalculation();
public void readData()
{
Scanner sin=new Scanner(System.in);
System.out.println("Enter two dimensions:");
d1=sin.nextInt();
d2=sin.nextInt();
}
public void displayData()
{
System.out.println("D1:"+d1+"\nD2:"+d2);
}
public int getD1()
{
return d1;
}
public int getD2()
{
return d2;
}
}
class Rectangle extends Diagram
{
private int area;
public void areaCalculation()
{
int x=super.getD1();
int y=super.getD2();
area=x*y;
}
public void displayData()
{
super.displayData();
System.out.println("Area:"+area);
}
}
class Triangle extends Diagram
{
private int area;
public void areaCalculation()
{
int x=super.getD1();
int y=super.getD2();
area=(1/2)*x*y;
}
public void displayData()
{
super.displayData();
System.out.println("Area:"+area);
}
}
class Ellipse extends Diagram
{
private int area;
public void areaCalculation()
{
int x=super.getD1();
int y=super.getD2();
area=(1/2)*x*y;
}
public void displayData()
{
super.displayData();
System.out.println("Area:"+area);
}
}
public class Draw
{
public static void main(String args[])
{
Scanner sin=new Scanner(System.in);
Diagram d1;
int ch;
do
{
System.out.println("1.RECTANGLE(default)");
System.out.println("2.TRIANGLE");
System.out.println("3.ELLIPSE");
System.out.print("Enter ur choice:");
ch=sin.nextInt();
switch(ch)
{
case 1:
d1=new Rectangle();
break;
case 2:
d1=new Triangle();
break;
case 3:
d1=new Ellipse();
break;
default :
d1=new Rectangle();
System.out.println("Enter correct Choice");
break;
}
d1.readData();
d1.areaCalcultion();
d2.displayData();
System.out.print("Do u want to continue(y-1)(n-0):");
ch=sin.nextInt();
}while(ch==1);
}
}
EXPERIMENT NO.25(b)
package live;
import music.Playable;
import music.string.Veena;
import music.wind.Saxophone;
v.play();
pv.play();
s.play();
ps.play();
package music;
public interface Playable {
void play();
package music.string;
import music.Playable;
System.out.println("Veena Plays");
package music.wind;
import music.Playable;
System.out.println("SaxophonePlays");
}
EXPERIMENT NO.26(a)
class Example1
{
public static void main(String args[])
{
int num1, num2;
try
{
num1 = 0;
num2 = 62 / num1;
System.out.println(num2);
System.out.println("Hey I'm at the end of try block");
}
catch (ArithmeticException e)
{
System.out.println("Exception occurred");
}
System.out.println("I'm out of try-catch block in Java.");
}
}
Output:
Output:
EXPERIMENT NO.27
class Main
divideByZero();
Output-
EXPERIMENT NO.28
char c = message.charAt(i);
continue;
if(numCharMap.containsKey(c)){
Else
numCharMap.put(c, 1);
m.getValue());
cc.countChars("I am an Indian");
Output-
EXPERIMENT NO.29
import java.io.*;
import java.util.*;
throws Exception
Try
int n;
out.write(n);
Finally
if (in != null)
in.close();
if (out != null)
{ out.close();
}
System.out.println("File Copied");
System.out.println( "Enter the source filename from where you have to read/copy
System.out.println( "Enter the destination filename where you have to write/paste :");
String b = sc.nextLine();
copyContent(x, y);
Output-
EXPERIMENT NO.30
String name;
Thread t;
name = threadname;
t.start();
try {
i); Thread.sleep(1000);
catch (InterruptedException e)
{
System.out.println(name + "Interrupted");
class MultiThread
new MyThread("One");
new MyThread("Two");
new NewThread("Three");
try {
Thread.sleep(10000);
catch (InterruptedException e) {
}
Output-
EXPERIMENT NO.31
try
Thread.sleep(100);
ie.printStackTrace();
System.out.println("The state of thread t1 while it invoked the method join() on thread t2 -"+
ThreadState.t1.getState());
try
Thread.sleep(200);
ie.printStackTrace();
}
public class ThreadState implements Runnable
t1 = new Thread(obj);
t1.start();
System.out.println("The state of thread t1 after invoking the method start() on it - " + t1.getState());
t2.getState()); t2.start();
System.out.println("the state of thread t2 after calling the method start() on it - " + t2.getState());
try
Thread.sleep(200);
ie.printStackTrace();
System.out.println("The state of thread t2 after invoking the method sleep() on it - "+ t2.getState() );
try
t2.join();
ie.printStackTrace();
System.out.println("The state of thread t2 when it has completed it's execution - " + t2.getState());
} }
Output-
EXPERIMENT NO.32
package my.app;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
}
private int deduplicate(Set<Item> set)
set.add(i);
return set.size();
assert(count == x.deduplicateWithLinkedHashSet());
assert(count == x.deduplicateWithTreeSet());
.include(DeduplicationWithSetsBenchmark.class.getSimpleName())
.forks(1)
.build();
new Runner(opt).run();
}
private static Item[] makeInputData() {
// We are looking to include a few collisions, so restrict the space of the values
item.id = rnd.nextInt(100);
acc[i] = item;
return acc;
return name;
.thenComparing(Item::getId, Comparator.naturalOrder());
}
EXPERIMENT NO.33
import java.util.*;
list.add("Mango");
list.add("Apple");
list.add("Banana");
list.add("Grapes");
System.out.println(list);
Output-
import java.io.*;
this.rollno = rollno;
this.name = name;
return this.name;
return this.rollno;
}
public void setname(String name)
this.name = name;
this.rollno = rollno;
int ans = 1;
return ans;
if (this == o) {
return true;
if (o == null) {
return false;
}
if (this.getClass() != o.getClass())
{ return false;
if (this.rollno != other.rollno)
{ return false;
return true;
map.put(one, one.getname());
map.put(two, two.getname());
one.setname("Not Geeks1");
two.setname("Not Geeks2");
System.out.println(map.get(one));
System.out.println(map.get(two));
"Geeks3");
System.out.println(map.get(three)) }}
Output-
EXPERIMENT NO.35
import java.util.*;
import java.sql.*;
Connection con=null;
Try
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
catch(Exception e){System.out.println(e);}
return con;
int status=0;
try
Connection con=EmpDao.getConnection();
(?,?,?,?)");
ps.setString(1,e.getName());
ps.setString(2,e.getPassword());
ps.setString(3,e.getEmail());
ps.setString(4,e.getCountry());
status=ps.executeUpdate();
con.close();
catch(Exception ex){ex.printStackTrace();
return status;
int status=0;
try
Connection con=EmpDao.getConnection();
where id=?");
ps.setString(1,e.getName());
ps.setString(2,e.getPassword());
ps.setString(3,e.getEmail());
ps.setString(4,e.getCountry());
ps.setInt(5,e.getId());
status=ps.executeUpdate();
con.close();
catch(Exception ex){ex.printStackTrace();}
return status;
int status=0;
try
Connection con=EmpDao.getConnection();
ps.setInt(1,id);
status=ps.executeUpdate();
con.close();
catch(Exception e){e.printStackTrace();
return status;
{
Emp e=new Emp();
try{
Connection con=EmpDao.getConnection();
ps.setInt(1,id);
ResultSet rs=ps.executeQuery();
if(rs.next()){
e.setId(rs.getInt(1));
e.setName(rs.getString(2));
e.setPassword(rs.getString(3));
e.setEmail(rs.getString(4));
e.setCountry(rs.getString(5));
con.close();
}catch(Exception ex){ex.printStackTrace();}
return e;
try{
Connection con=EmpDao.getConnection();
while(rs.next()){
e.setId(rs.getInt(1));
e.setName(rs.getString(2));
e.setPassword(rs.getString(3));
e.setEmail(rs.getString(4));
e.setCountry(rs.getString(5));
list.add(e);
con.close();
catch(Exception e){e.printStackTrace();
return list;