UPES OOPS Lab File
UPES OOPS Lab File
Lab File
Name:-Karan Saini
Sap Id:-500083231
Roll No.:-R214220590
Batch:-B2
Page 1 of 26
Experiment-1
Installation Screenshot of JDK:-
Page 2 of 26
Page 3 of 26
Algorithms Screenshot:-
Page 4 of 26
Output Screenshot:-
Page 5 of 26
Experiment 2
1. Write and execute my first java program.
Algorithm:-
Step 2:- Use public static void main(String args[]) to call the main function.
Program Code:-
class First
{
public static void main(String args[])
{
System.out.println("Hello User");
System.out.println("Name:Karan Saini, Roll No.:R214220590");
}
}
Output:-
Page 6 of 26
2. Write a program to print Fibonacci series using java loop.(up to 10
numbers)
Algorithm:-
Step 1:- Start the program.
Step 4:- Enter the number of terms of Fibonacci series to be printed (here numbers of
terms of Fibonacci series to be printed is 10)
Step 5:- Print first two terms of series (i.e. n1 & n2)
Program Code:-
class Fibonacci
{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.println("Name:Karan Saini, Roll No.:R214220590");
System.out.print("Fibonacci series is:");
System.out.print(n1+" "+n2);
for(i=2;i<count;i++)
{
n3=n1+n2;
System.out.print(" "+n3);
Page 7 of 26
n1=n2;
n2=n3;
}
}
}
Output:-
Algorithm:-
Program Code:-
Page 8 of 26
class Largest
{
public static void main(String[] args)
{
int a = 20, b = 42, c = 55;
if(a > b && a > c)
System.out.println("largest number is:" +a);
else if(b > a && b > c)
System.out.println("largest number is:" +b);
else
System.out.println("The largest number is:" +c);
System.out.println("Name:Karan Saini, Roll No.:R214220590");
}
}
Output:-
4. Write a program to find the sum of all integers greater than 40 and less
than 250 that are divisible by 5.
Algorithm:-
Step 1:- Start the program.
Page 9 of 26
Step 2:- Declare a variable of type int i=41 and Sum=0
Program Code:-
class Sum
{
public static void main(String arg[])
{
int sum=0;
for(int i=41;i<250;i++)
{
if(i%5==0)
{
System.out.println(i);
sum=sum+i;
}
}
System.out.println("Sum of intergers is: \n"+sum);
System.out.println("Name:Karan Saini, Roll No.:R214220590");
}
}
Output:-
Page 10 of 26
Page 11 of 26
Experiment 3
1. Write a program to take input (a number) of a month (1 - 12) and print its
equivalent name of the month. (e.g 1 to Jan, 2 to Feb. 12 to Dec.) Use
Scanner class for user input ( Hint-use switch case)
Algorithm:-
Step 1:- Start the Program
Step 2:- import java.util.scanner
Step 3:- Scan the next token of the input as an int.
Step 4:- Use switch case and declare all months.
Step 5:- if case is not present then print invalid month.
Step 6:- Print the output.
Step 7:- End
Program Code:-
import java.util.Scanner;
public class Month
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter month in number: ");
int month = sc.nextInt();
String month_name;
switch (month)
{
Page 12 of 26
case 1: month_name = "January";
break;
case 2: month_name = "February";
break;
case 3: month_name = "March";
break;
case 4: month_name = "April";
break;
case 5: month_name = "May";
break;
case 6: month_name = "June";
break;
case 7: month_name = "July";
break;
case 8: month_name = "August";
break;
case 9: month_name = "September";
break;
case 10: month_name = "October";
break;
case 11: month_name = "November";
break;
case 12: month_name = "December";
break;
default: month_name = "Invalid month";
break;
}
Page 13 of 26
System.out.println(month_name);
System.out.println("name:Karan Saini, Roll NO:R214220590");
}
}
Output:-
Algorithm:-
Step 1:- Start the Program.
Step 2:- Declare variable a, b, c.
Step 3:- Parsing a&b arguments into an integer:-
a=Integer.parseInt(ar[0]);
b=Integer.parseInt(ar[1]);
&
Declare-
c=a+b;
Step 4:- Print the sum.
Page 14 of 26
Step 5:- End.
Program Code:-
class Sum
{
public static void main(String ar[])
{
int a,b,c;
a=Integer.parseInt(ar[0]);
b=Integer.parseInt(ar[1]);
c=a+b;
System.out.println("sum of " + a + " and " + b +" is: " +c);
System.out.println("name:Karan Saini, Roll NO:R214220590");
}
}
Output:-
Page 15 of 26
3. Write a program to implement a command line calculator.
Algorithm:-
Step 1:- Start the program
Step 2:- Parsing a&b arguments into an integer:-
int a = Integer.parseInt(args[0]);
String op = args[1];
int b = Integer.parseInt(args[2]);
int c;
Step 3:- Declare all the four operators (=, -, *, /) using if & else if cases.
Step 4:- if operator doesn’t find print “Operator not recognized”
Step 5:- Print the output
Step 6:- End
Program Code:-
class Calculator
{
public static void main(String[] args)
{
int a = Integer.parseInt(args[0]);
String op = args[1];
int b = Integer.parseInt(args[2]);
int c;
if (op.equals("+"))
{
Page 16 of 26
c = a+b;
}
else if (op.equals("-"))
{
c = a-b;
}
else if (op.equals("*"))
{
c = a*b;
}
else if (op.equals("/"))
{
c = a/b;
}
else
{
throw new java.lang.Error("Operator not recognized");
}
System.out.println(c);
System.out.println("name:Karan Saini, Roll NO:R214220590");
}
}
Output:-
Page 17 of 26
4. Write a program to accept three digits (i.e. 0 - 9) and print all its possible
combinations. (For example if the three digits are 1, 2, 3 than all possible
combinations are: 123, 132, 213, 231, 312, 321.)
Algorithm:-
Step 1:- Start the program.
Step 2:- add input as (1, 2, 3)
Step 3:- declare variable a, b, c
Step 4:- by using loops initialize the variables (as a<3, b<3, c<3)
Step 5:- Print the Program
Step 6:- End
Program Code:-
public class Combinations
{
public static void main(String[] args)
{
int[] input = { 1, 2, 3 };
Page 18 of 26
for (int a = 0; a < 3; a++)
{
for (int b = 0; b < 3; b++)
{
for (int c = 0; c < 3; c++)
{
if (a != b && b != c && c != a)
{
System.out.println(input[a] + "" + input[b] + "" + input[c]);
}
}
}
}
{
System.out.println("Name:Karan Saini, Roll NO:R214220590");
}
}
}
Output:-
Page 19 of 26
Experiment 4
Title: Class, Objects, Methods and Constructors
Algorithm:-
Step 1:- Start.
Step 2:- Declare a class student and inside that take variables roll, name and marks and create
a function getData() and inside it store them as new variables.
Step 3:- Now print roll, name and marks
Step 4:- Again declare a class Cmechanism
Step 5:- Using string functions print the details of two students
Step 6:- End.
Program Code:-
class Student
{
int roll;
String name;
double marks;
void setData(int r, String s, double m)
{
roll=r;
name=s;
marks=m;
}
void getData()
{
System.out.println("Name:"+name);
System.out.println("Roll no:"+roll);
System.out.println("Marks:"+marks);
}
Page 20 of 26
}
class Cmechanism{
public static void main (String[] args)
{
System.out.println();
Student S1=new Student();
S1.setData(5,"Karan",94);
S1.getData();
System.out.println();
Student S2=new Student();
S2.setData(2,"ved",95);
S2.getData();
}
}
Output:-
Algorithm:-
Step 1:- Start
Step 2:- Create a class SimpleCalculator
Step 3:- Make function add and take two variables a and b and return (a+b)
Page 21 of 26
Step 4:- Similarly, again make function add and take two variables a, b and c and return
(a+b+c)
Step 5:- Again create a class Demo then call the function in the main class and use print
command to print the output
Step 6:- End
Program Code:-
class SimpleCalculator
{
int add(int a, int b)
{
return a+b;
}
int add(int a, int b, int c)
{
return a+b+c;
}
}
public class Demo
{
public static void main(String args[])
{
SimpleCalculator obj = new SimpleCalculator();
System.out.println(obj.add(15, 45));
System.out.println(obj.add(15, 45, 78));
}
}
Output:-
Page 22 of 26
3. Write a JAVA program to implement type promotion in method
overloading.
Algorithm:-
Step 1:- Start
Step 2:- Create a class Calculation
Step 3:- Make function add and take two variables a and b and return void(a+b)
Step 4:- Similarly, again make function add and take two variables a, b and c and return void
(a+b+c)
Step 5:- Again create a class Demo then call the function in the main class and use print
command to print the output
Step 6:- End
Program Code:-
class Calculation{
void sum(int a,int b)
{
System.out.println(a+b);
}
void sum(int a,int b,int c){
System.out.println(a+b+c);
}
Page 23 of 26
public static void main(String args[]){
Calculation obj=new Calculation();
obj.sum(15,15,15);
obj.sum(40,40);
}
}
Output:-
Algorithm:-
Step 1:- Start
Step 2:- Create a class Rectangle then take two private variables Length and Breadth
Step 3:- Assign these two variables a new variable side inside a function
Step 4:- Then in other function return Length*Breadth
Step 5:- Declare a main class and assign values to the function and then use print command
to print the output
Step 6:- End
Program Code:-
class Rectangle {
Page 24 of 26
private int breadth;
class Test {
System.out.println(rect.getArea());
System.out.println(sq.getArea());
}
}
Output:-
Page 25 of 26
Experiment 5
1. Write a Java program to show that private member of a super class cannot
be accessed from derived classes.
Algorithm:-
Step 1:- Start the Program
Step 3:- Define variables for length and breadth (l=x & b=y)
Step 4:- Again declare a class Room extends room & declare a super class
Step 5:- Now using Super class print the details of room
Program Code:-
class room
{
private int l,b;
room(int x,int y)
{
l=x; b=y;
}
int area()
{
return(l*b);
}
}
class class_room extends room
{
int h;
class_room(int x,int y,int z)
{
super(x,y);
h=z;
}
Page 26 of 26
int volume()
{
return(area()*h);
}
}
class Super
{
public static void main(String args[])
{
class_room cr=new class_room(18,45,55);
int a1=cr.area();
int v1=cr.volume();
System.out.println("Area of Room : "+a1);
System.out.println("Volume of Room : "+v1);
System.out.println("Name:Karan Saini, Roll No.:R214220590");
}
}
Output:-
2. Write a program in Java to create a Player class. Inherit the classes Cricket
_Player, Football _Player and Hockey_ Player from Player class.
Algorithm:-
Step 1:- Start the program
Step 2:- Declare a class player and inside that take variables name & age
Page 27 of 26
Step 3:- Now using string print that name & age
Step 5:- Now using string and super Print the details of Cricket player
Step 6:- Now similarly declare a class football player extends player & using string and super
Print the details of football player
Step 7:- Now similarly again declare a class Hockey player extends player & using string and
super Print the details of Hockey player
Step 8:- Now declare a class HCPlayer and provide the details of Cricket, Football and Hockey
player
Program Code:-
class player
{
String name;
int age;
player(String n,int a)
{
name=n; age=a;
}
void show()
{
System.out.println("\n");
System.out.println("Player name : "+name);
System.out.println("Age : "+age);
}
}
class cricket_player extends player
{
String type;
cricket_player(String n,String t,int a)
{
super(n,a);
type=t;
Page 28 of 26
}
public void show()
{
super.show();
System.out.println("Player type : "+type);
}
}
class football_player extends player
{
String type;
football_player(String n,String t,int a)
{
super(n,a);
type=t;
}
public void show()
{
super.show();
System.out.println("Player type : "+type);
}
}
class hockey_player extends player
{
String type;
hockey_player(String n,String t,int a)
{
super(n,a);
type=t;
}
public void show()
{
super.show();
System.out.println("Player type : "+type);
Page 29 of 26
class HCPlayer
{
public static void main(String args[])
{
cricket_player c=new cricket_player("Ved","cricket",25);
football_player f=new football_player("Yogesh","foot ball",25);
hockey_player h=new hockey_player("Mahesh","hockey",25);
c.show();
f.show();
h.show();
}
}
Output:-
Page 30 of 26
Algorithm:-
Step 1:- Start the program
Step 2:- Declare a class worker and inside that take variable Employment no. and name
Step 3:- Now using string print that class worker’s employment no. and name
Step 4:- Now again declare a class dailyworker extends worker and declare variable rate as r
Step 6:- Now again declare a class salaried extends worker and declare variable rate as r
Step 8:- Now declare class pay and provides daily worker and salaried worker’s details
Step 9:- Now using show and super print the name, Employment no. and salary of Daily and
Salaried worker.
Program Code:-
class worker
{
String name;
int empno;
worker(int no,String n)
{ empno=no; name=n; }
void show()
{
System.out.println("\n");
System.out.println("Employee number : "+empno);
System.out.println("Employee name : "+name);
}
}
class dailyworker extends worker
{
int rate;
dailyworker(int no,String n,int r)
Page 31 of 26
{
super(no,n);
rate=r;
}
void compay(int h)
{
show();
System.out.println("Salary : "+rate*h);
}
}
class salariedworker extends worker
{
int rate;
salariedworker(int no,String n,int r)
{
super(no,n);
rate=r;
}
int hour=40;
void compay()
{
show();
System.out.println("Salary : "+rate*hour);
System.out.println("Name:Karan Saini, Roll No.:R214220590");
}
}
class Pay
{
public static void main(String args[])
{
dailyworker d=new dailyworker(300,"Yash",90);
salariedworker s=new salariedworker(800,"Aman",100);
d.compay(45);
s.compay();
}
}
Output:-
Page 32 of 26
4. Design a class employee of an organization. An employee has a name, empid,
and salary. Write the default constructor, a constructor with parameters
(name, empid, and salary) and methods to return name and salary. Also write
a method increaseSalary that raises the employee’s salary by a certain user
specified percentage. Derive a subclass Manager from employee. Supply a
test program that uses theses classes and methods.
Algorithm:-
Step 1:- Start the program
Step 3:- declare the variables like employment id, salary and name
Step 4:- Now using string declare the double salary, employment id and name
Step 6:- Now using super declare variables and show details like increased salary, name,
Employment id
Step 7:- Now declare class Empl and provide all details
Page 33 of 26
Step 8:- Print all details like Employment id, name, salary using string
Program Code:-
class employee
{
int empid;
String name;
double salary;
employee()
{
empid=500083231;
name="Karan";
salary=70000;
}
employee(String name,int empid,double salary)
{
this.empid=empid;
this.name=name;
this.salary=salary;
}
String getName()
{
return name;
}
double getSalary()
{
return salary;
}
double increaseSalary(double x)
{
salary=salary+(salary*x);
return salary;
}
}
Page 34 of 26
class manager extends employee
{
double r=0.5;
manager()
{
super();
}
manager(String name, int empid, double salary)
{
super(name,empid,salary);
salary= increaseSalary(r);
}
}
class Empl
{
public static void main(String[] args)
{
manager m=new manager("Karan" , 500083231 , 140000);
System.out.println("NAME:" +m.name);
System.out.println("ID:" +m.empid);
System.out.println("Salary:" +m.salary);
System.out.println("Name:Karan Saini, Roll NO:R214220590");
}
}
Output:-
Page 35 of 26
Experiment 6
Interface
1. Write a program to create interface A, in this interface we have two method
meth1 and meth2. Implements this interface in another class named
MyClass.
Algorithm:-
Step 1:- Start the program
Step 3:- Create another interface y with having interface x with function meth3
Step 6:- Now create a main class and call all the functions
Program Code:-
interface x
{
void meth1();
void meth2();
}
interface y extends x
{
void meth3();
}
class MyClass implements y
{
public void meth1 ( )
{
System.out.println("Implement meth1().");
}
Page 36 of 26
public void meth2()
{
System.out.println ("Implement meth2().");
}
public void meth3()
{
System.out.println ("Implement meth()." );
System.out.println ("Name:Karan Saini. Roll No.:R214220590" );
}
}
class IFExtend
{
public static void main(String arg[])
{
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
}
}
Output:-
Page 37 of 26
Step 2:- Create Interface One with function method1
Step 3:- Now, Again Create Interface Two with function method2
Step 4:- Now, Again Create Interface Three which extends One, Two with function method3
Step 5:- Now, Create Int implements Three class and print all three (method1, method2,
method3) functions details
Program Code:-
interface One
{
public void method1();
}
interface Two
{
public void method2();
}
interface Three extends One,Two
{
void method3();
}
Page 38 of 26
System.out.println("Third!");
}
Output:-
Algorithm:-
Step 1:- Start the program
Step 2:- Create interface Course with 2 functions division and modules
Page 39 of 26
Step 3:- Create stud implements course class and define functions using variables as division=
x and module= y
Step 4:- Now, using string print info we want like Name, division & module
Step 6:- Now, provide all details like Name, division & module
Program Code:-
interface course
{
void division(int x);
void modules(int y);
}
class stud implements course
{
String name;
int div,mod;
void name(String n)
{
name=n;
}
public void division(int x)
{
div=x;
}
public void modules(int y)
{
mod=y;
}
void disp()
{
System.out.println("Name :"+name);
System.out.println("Division :"+div);
System.out.println("Modules :"+mod);
System.out.println("Name:Karan Saini, Roll No.:R214220590");
Page 40 of 26
}
}
class DM
{
public static void main(String args[])
{
stud s=new stud();
s.name("Karan Saini");
s.division(10);
s.modules(20);
s.disp();
}
}
Output:-
Algorithm:-
Step 1:- Start the program
Page 41 of 26
Step 3:- Now, Create Int Square();
Step 8:- Now create Testt class and print all the details
Program Code:-
interface test
{
int square();
}
arithmetic(int x)
{
l = x;
}
}
class ToTestInt
{
public int return_ans(int x)
Page 42 of 26
{
arithmetic a=new arithmetic(x);
return a.square();
}
}
class Testt
{
public static void main(String []args)
{
ToTestInt x= new ToTestInt();
System.out.println("\nThe square of 12 is "+x.return_ans(12));
System.out.println("Name:Karan Saini, Roll No.:R214220590");
}
}
Output:-
Page 43 of 26
Experiment 7
Title: Package
1. Write a Java program to implement the concept of importing classes from
user defined package.
Algorithm:-
Step 1:- Start
Step 2:- Create a file and in the very first line declare that it is a package.
Step 3:- Make a public class and create a method in it (not main) to print hello world.
Step 4:- Save this file as the name of the class
Step 5:- To compile this package type javac -d . one.java
Step 6:- Create another file and import the package you created by packageName.*
Step 7:- Make a class and define the main method in it
Step 8:- Create an object of the class you defined in the package by packageName.className
obj = new packageName.className()
Step 9:- Call the method of that object.
Step 10:- End
Program Code:-
(Save this file as One.java)
package pack;
public class One
{
public void msg()
{
System.out.println("Name: Karan Saini, Roll No.:R214220590");
System.out.println("Hello World");
}
}
import pack.*;
public class Second
{
public static void main(String args[])
{
Page 44 of 26
pack.One obj=new pack.One();
obj.msg();
}
}
Output:-
2. Write a program to make a package Balance. This has an Account class with
Display_Balance method. Import Balance package in another program to
access Display_Balance method of Account class.
Algorithm:-
Step 1:- Start
Step 2:- Make a file and declare it as package names balance.
Step 3:- Define a public class in the called account
Step 4:- Define a method in class account and print the balance
Step 5:- Save this file as the name of the class
Step 6:- To compile this package type javac -d . filename.java
Step 7:- Make another file and import the package in this file
Step 8:- Declare class implebal and make the main method inside this class
Step 9:- Make object of the public class defined in the package
Step 10:- Call the method in that class using the object
Step 11:- End
Program Code:-
(Save this file as Account.java)
Page 45 of 26
package balance;
public class Account
{
public void Dispaly_Balance(int bal)
{
System.out.println ("Name: Karan Saini, Roll No.: R214220590");
System.out.println ("Balance in the account is :" + bal);
}
}
import balance.*;
class implBal
{
public static void main(String[] agrs)
{
balance.Account obj = new balance.Account();
obj.Dispaly_Balance(500000);
}
}
Output:-
Page 46 of 26
3. Write a java program that creates a package calculation. Add following
classes in it:
a) Addition
b) Subtraction
c) Division
d) Multiplication
Write another Test class, import and use the above package.
Algorithm:-
Step 1:- Start
Step 2:- Make 4 file and declare them as packages
Step 3:- In each file create one public class and name them add, subs, multi, divi
Step 4:- In each class define a method addition, substation, multiplication, division
respectively
Step 5:- Save each file with the name of the class
Step 6:- To compile this package type javac -d . filename.java
Step 7:- Make another file and import all the four packages in the new file.
Step 8:- Create objects for all the classes in all the packages
Step 9:- Call the methods in the classes using the objects and pass the arguments
Step 10:- End
Program Code:-
a) Save this file as add.java
package calc1;
public class add
{
public void addition(int a, int b)
{
System.out.println("Name: Karan Saini, Roll No.: R214220590");
System.out.println("Answer:"+ (a+b));
}
}
package calc2;
public class subs
Page 47 of 26
{
public void substraction(int a, int b)
{
System.out.println("Answer:" + (a-b));
}
}
package calc3;
public class multi
{
public void multiplication (int a, int b)
{
System.out.println("Answer:"+ (a*b));
}
}
package calc4;
public class div
{
public void division (int a, int b)
{
System.out.println("Answer:" + (a/b));
}
}
import calc1.*;
import calc2.*;
import calc3.*;
import calc4.*;
class Test
{
Page 48 of 26
public static void main(String[] args)
{
calc1.add obj1 = new calc1.add();
calc2.subs obj2 = new calc2.subs();
calc3.multi obj3 = new calc3.multi();
calc4.div obj4 = new calc4.div();
obj1.addition(12,5);
obj2.substraction(15,8);
obj3.multiplication(19,13);
obj4.division(45,5);
}
}
Output:-
Page 49 of 26
Experiment 8
TITLE: Exceptions
1) Write a program in Java to display the names and roll numbers of students.
Initialize respective array variables for 10 students. Handle
ArrayIndexOutOfBoundsExeption, so that any such problem doesn’t cause
illegal termination of program.
Algorithm:-
Step 1:-Start
Step 2:-Create a class student
Step 3:-Create a method read and throws exception and input the data in it for reading the line
and printing the output
Step 4:-Create a method dg and create the method to calculate the grades, print grades
Step 5:-Create a class Stu and create try and catch method to catch the exception
Step 6:-End
Program Code:-
import java.io.*;
class Stu
{
public static void main(String ar[])
{
int N=0;
student s=new student();
try
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Name:Karan Saini, Roll No.:R214220590");
System.out.println("Enter the number of student:");
N=Integer.parseInt( in.readLine());
for(int i=0;i< N;i++);
s.read();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Max students=10\n");
N=10;
}
Page 50 of 26
catch(Exception e)
{System.out.println(e); }
for(int i=0;i< N;i++);
s.disp();
}
}
class student
{
String name,x;
int reg,x1,x2,x3;
void read()throws Exception
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the register No:");
reg=Integer.parseInt(in.readLine());
System.out.println("Enter the name:");
name=in.readLine();
System.out.println("Enter mark1:");
x1=Integer.parseInt(in.readLine());
System.out.println("Enter mark2:");
x2=Integer.parseInt(in.readLine());
System.out.println("Enter mark3:");
x3=Integer.parseInt(in.readLine());
}
public void dm()
{
int tt=x1+x2+x3;
if(tt>=250) x="A";
else if(tt>=200) x="B";
else if(tt>=150) x="C";
else if(tt>=100) x="D";
else x="E";
System.out.println("Grade:"+x);
}
void disp()
{
System.out.println("MARK LIST OF STUDENTS");
Page 51 of 26
System.out.println("Register No :"+reg);
System.out.println("Name:"+name);
System.out.println("Mark1:"+x1);
System.out.println("Mark2:"+x2);
System.out.println("Mark3:"+x3);
dm();
}
}
Output:-
2) Write a Java program to enable the user to handle any chance of divide by zero
exception.
Algorithm:-
Step 1:- Start
Step 2:-Create a class Exp
Step 3:-Create try method and catch to catch the exception e
Step 4:-Now print the exception
Step 5:-End
Program Code:-
import java.io.*;
Page 52 of 26
class Exp
{
public static void main(String[] args)
{
int x = 45;
int y = 0;
try
{
System.out.println(x/y);
}
catch (ArithmeticException e)
{
System.out.println("Name:Karan Saini, Roll No.:R214220590");
System.out.println("Division by zero is not possible");
}
}
}
Output:-
Page 53 of 26
Program Code:-
class NonNumeric extends Exception
{
NonNumeric()
{
super("Value is non numeric \n");
}
}
class Mod
{
public static void main(String ar[])
{
int x,y,z=0;
System.out.println("Name:Karan Saini, Roll No.:R214220590");
try
{
x=Integer.parseInt(ar[0]);
throw new NonNumeric();
}
catch(NumberFormatException e)
{
System.out.println(e);
}
catch(NonNumeric e)
{
System.out.println(e);
}
}
}
Output:-
Page 54 of 26
4) Write a java program to throw an exception for an employee details.
If an employee name is a number, a name exception must be thrown.
If an employee age is greater than 50, an age exception must be thrown.
Algorithm:-
Step 1:- Start
Step 2:- Create class Emp and print the name and age, all the details of the employee throws
the exception
Step 3:- In the try block try to catch the exception and create a method, if age>50 throw new
exception and when the name is a number then its an exception
Step 4:- Print the exception e
Step 5:- End
Program Code:-
import java.io.*;
import java.util.*;
class Emp
{
public static void main(String args[])
{
String name;
int age;
System.out.println("Name:Karan Saini, Roll No.:R214220590");
System.out.println("Enter Name and Age:");
Scanner in=new Scanner(System.in);
try
Page 55 of 26
{
if(!(in.nextLine().matches("[a-zA-Z]+")))
{
throw new IOException();
}
age=in.nextInt();
if(age>50)
{
System.out.println("Age greater than 50 Exception");
throw new Exception();
}
Page 56 of 26
Experiment 9
TITLE: Threads
1) Write a program to implement the concept of multithreading by extending
Thread class.
Algorithm:-
Step 1: Start
Step 2: Create a class that extends the Thread class.
Step 3: Overrides the run() method available in the Thread class.
Step 4: Call start() method to start the execution of a thread.
Step 5: End
Program Code:-
class Multithread extends Thread
{
public void run()
{
System.out.println("Thread: Algo");
}
public static void main(String args[])
{
System.out.println ("Name:Karan Saini, Roll No.:R214220590");
Multithread obj = new Multithread();
obj.start();
}
}
Output:-
Page 57 of 26
2) Write a program to implement the concept of multithreading by implementing
Runnable interface.
Algorithm:-
Step 1: Start
Step 2: Create a class which implements Runnable interface in java.lang and override run()
method.
Step 3: Create a Thread object and call start() method on this object.
Step 4: End
Program Code:-
class Multi implements Runnable
{
public void run()
{
try
{
System.out.println(Thread.currentThread().getId() + "Thread running");
}
catch (Exception e)
{
System.out.println("Exception caught");
}
}
public static void main(String[] args)
{
int n = 10;
System.out.println ("Name:Karan Saini, Roll No.: R214220590");
for (int i = 0; i < n; i++)
{
Thread object = new Thread(new Multi());
object.start();
}
}
}
Output:-
Page 58 of 26
3) Write a program for generating 2 threads, one for printing even numbers and
the other for printing odd numbers.
Algorithm:-
Step 1: Start
Step 2: Create a class name OddEven and Thread object and using while & try throw exception
in odd function.
Step 3: Similarly, using while & try throw exception in Even function
Step 4: Create a Thread object and call start() method on this object.
Step 5: End
Program Code:-
class OddEven
{
int ct = 1;
static int n;
public void pOdd()
{
synchronized (this)
{
while (ct < n)
{
while (ct % 2 == 0)
{
try
{
Page 59 of 26
wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
System.out.print(ct + " ");
ct++;
notify();
}
}
}
public void pEven()
{
synchronized (this)
{
while (ct < n)
{
while (ct % 2 == 1)
{
try
{
wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println(ct + " ");
ct++;
notify();
}
}
}
public static void main(String[] args)
{
n = 20;
System.out.println ("Name:Karan Saini, Roll No.:R214220590");
OddEven mt = new OddEven();
Thread t1 = new Thread(new Runnable()
{
public void run()
Page 60 of 26
{
mt.pEven();
}
});
Thread t2 = new Thread(new Runnable()
{
public void run()
{mt.pOdd();}
});
t1.start();
t2.start();
}
}
Output:-
Page 61 of 26
Step 2: Create a class name Sleep which implements Runnable interface in java.lang and
override run() method and throw exception.
Step 3: Now, create another class name Join which implements Runnable interface in java.lang
and override run() method and throw exception.
Step 4: End
Program Code:-
import java.lang.*;
class Sleep implements Runnable
{
Thread T;
public void run()
{
for (int i = 0; i < 4; i++)
{
System.out.println(Thread.currentThread().getName()+ " " + i);
try
{
Thread.sleep(80);
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Page 62 of 26
}
5) Write a Java program that show multithreading between three threads. Set
different priority to each thread and show output.
Algorithm:-
Step 1: Start
Step 2: Create a class that extends the Thread class.
Step 3: Overrides the run() method available in the Thread class.
Step 4: Declare different Priority using get & set.
Step 5: Print the output.
Step 6: End
Program Code:-
Page 63 of 26
import java.lang.*;
class priority extends Thread
{
public void run()
{
System.out.println("Run Method");
}
public static void main(String[] args)
{
System.out.println ("Name:Karan Saini, Roll No.:R214220590");
priority T1 = new priority();
priority T2 = new priority();
priority T3 = new priority();
System.out.println("T1 thread priority : " + T1.getPriority());
System.out.println("T2 thread priority : " + T2.getPriority());
System.out.println("T3 thread priority : " + T3.getPriority());
T1.setPriority(1);
T2.setPriority(3);
T3.setPriority(7);
System.out.println("T1 thread priority : " + T1.getPriority());
System.out.println("T2 thread priority : " + T2.getPriority());
System.out.println("T3 thread priority : " + T3.getPriority());
System.out.println("Currently Executing Thread : "+ Thread.currentThread().getName());
System.out.println("Main thread priority : " + Thread.currentThread().getPriority());
Thread.currentThread().setPriority(10);
System.out.println("Main thread priority : " + Thread.currentThread().getPriority());
}
}
Output:-
Page 64 of 26
Experiment 10
TITLE: Strings Handling and Wrapper Class
1. Write Java program using the following string methods:
String concat(String str)
boolean equals(Object anObject)
boolean equalsIgnoreCase(String anotherString)
String toUpperCase()
char charAt(int index)
int compareTo(String anotherString)
Algorithm:-
Step 1:- Start
Step 2:- Create a class and a main method inside the class
Step 3:- In that main method ask for the user to input two strings
Perform the methods on the strings as per the question in the format:
stringName.methodName(stringName)
Step 4:- End
Program Code:-
import java.io.*;
import java.lang.String;
import java.util.Scanner;
class functions
{
public static void main(String[] args)
{
System.out.println ("Name:Karan Saini, Roll No.:R214220590");
Scanner scan = new Scanner(System.in);
String s1=" ";
String s2=" ";
int ch=0;
char choice;
System.out.println("Enter String 1");
s1=scan.nextLine();
System.out.println("Enter String 2");
s2=scan.nextLine();
System.out.println("ConCat");
System.out.println(s1.concat(s2));
System.out.println("Equals");
Page 65 of 26
System.out.println(s1.equals(s2));
System.out.println("EgualsIgnoreCase");
System.out.println(s1.equalsIgnoreCase(s2));
System.out.println("UpperCase");
System.out.println(s1.toUpperCase());
System.out.println(s2.toUpperCase());
System.out.println("CharAt");
int pos=0;
System.out.println("Enter Position of word ");
pos=scan.nextInt();
System.out.println(s1.charAt(pos));
System.out.println("CompareTo");
System.out.println(s1.compareTo(s2));
}
}
Output:-
Page 66 of 26
int length()
String replace(char oldChar, char newChar)
String substring(int beginIndex, int endIndex)
Algorithm:-
Step 1:- Start
Step 2:- Create a class and a main method inside the class
Step 3:- In that main method ask for the user to input a string
Perform the methods on the string as per the question in the format:
stringName.methodName(stringName)
Step 4:- End
Program Code:-
import java.io.*;
import java.lang.String;
import java.util.Scanner;
class Hit
{
public static void main(String[] args)
{
System.out.println ("Name:Karan Saini, Roll No.:R214220590");
Scanner scan = new Scanner(System.in);
String s1=" ";
int ch=0;
char choice;
System.out.println("Enter String");
s1=scan.nextLine();
System.out.println("HashCode");
System.out.println(s1.hashCode());
System.out.println("Trim");
System.out.println(s1.trim());
System.out.println("Intern");
System.out.println(s1.intern());
System.out.println("Length");
System.out.println(s1.length());
System.out.println("Replace");
String myStr = "Hello";
System.out.println(s1.replace('L', 'B'));
System.out.println("SubString");
String substr = s1.substring(2,7);
System.out.println(substr);
}
}
Page 67 of 26
Output:-
3. Write a Java code that converts int to Integer, converts Integer to String, converts
String to int, converts int to String, converts String to Integer, converts Integer to
int.
Algorithm:-
Step 1:- Start
Step 2:- Create classes for all the 6 conversions and also create a class to define main method in
In the int to Integer class declare an integer variable and create an Integer Object while
pass the integer variable to the constructor
In the Integer to String class create an object of Integer class while passing a number in
the constructor. Then create an object of the String class by passing the Integer class’s
object
In the string to integer class create a String and using the .parseInt method save it in an
integer variable
In the int to string class declare an integer variable and create object of String class by
passing the integer variable to the constructor
In the String to Integer class declare a string variable and make an object of Integer class
and pass the String variable using .valueOf
In the Integer to int class create an Integer object and pass an int value to its constructor.
Declare an int variable and using the intValue method assign the value of the variable
In the class where the main method is defined, create object of all these classes and class
the constructors.
Step 3:- End
Page 68 of 26
Program Code:-
class IntToInteger
{
void IntToInteger()
{
int i = 10;
Integer intObj = new Integer(i);
System.out.println(intObj);
}
}
class IntegerToString
{
void IntegerToString()
{
Integer intObj = new Integer(10);
String str = intObj.toString();
System.out.println("Integer converted to String = " + str);
}
}
class StringToInt
{
void StringToInt()
{
String s="10";
int i=Integer.parseInt(s);
System.out.println(i);
}
}
class IntToString
{
void IntToString()
{
int i=10;
String s=String.valueOf(i);
System.out.println(i+20);
System.out.println(s+20);
}
}
class StringToInteger
{
void StringToInteger()
{
String s="10";
Page 69 of 26
Integer i=Integer.valueOf(s);
System.out.println(i);
}
}
class IntegerToInt{
void IntegerToInt()
{
Integer intobject = new Integer(10);
int i = intobject.intValue();
System.out.println("The integer Value of i = " + i);
}
}
public class Combine
{
public static void main(String args[])
{
System.out.println ("Name:Karan Saini, Roll No.: R214220590");
IntToInteger obj1 = new IntToInteger();
IntegerToString obj2 = new IntegerToString();
StringToInt obj3 = new StringToInt();
IntToString obj4 = new IntToString();
StringToInteger obj5 = new StringToInteger();
IntegerToInt obj6 = new IntegerToInt();
obj1.IntToInteger();
obj2.IntegerToString();
obj3.StringToInt();
obj4.IntToString();
obj5.StringToInteger();
obj6.IntegerToInt();
}
}
Output:-
Page 70 of 26
4. Write a Java code that converts float to Float converts Float to String converts
String to float converts float to String converts String to Float converts Float to float.
Algorithm:-
Step 1:- Start
Step 2:- Create classes for all the 6 conversions and also create a class to define main method in
In the float to Float class declare an integer variable and create an Float Object while
pass the integer variable to the constructor
In the Float to String class create an object of Float class while passing a number in the
constructor. Then create an object of the String class by passing the Float class’s object
In the string to float class create a String and using the .parseFloat method save it in an
float variable
In the float to string class declare an float variable and create object of String class by
passing the float variable to the constructor
In the String to Float class declare a string variable and make an object of Float class and
pass the String variable using .valueOf
In the Float to float class create an Float object and pass an float value to its constructor.
Declare an float variable and using the intValue method assign the value of the variable
In the class where the main method is defined, create object of all these classes and
class the constructors.
Step 3:- End
Program Code:-
import java.util.Scanner;
class Convert
{
Page 71 of 26
public static void main(String args[])
{
System.out.println ("Name:Karan Saini, Roll No.:R214220590");
Scanner scan = new Scanner(System.in);
float f = 10.0f;
System.out.println("float TO Float");
Float f1 = new Float(f);
System.out.println(f1);
System.out.println("Float To String");
String sf = Float.toString(f1);
System.out.println(sf);
System.out.println("String To Float");
System.out.println("Enter a string");
String s = scan.next();
Float x=Float.valueOf(s);
System.out.println(x);
System.out.println("float TO String");
String st1 = Float.toString(x);
System.out.println(st1);
System.out.println("String TO Float");
Float in = new Float(st1.valueOf(st1));
System.out.println(in);
System.out.println("Float TO float");
float no = (float)in;
System.out.println(no);
}
}
Output:-
Page 72 of 26
Experiment 11
TITLE: JDBC
1) Show the steps of MYSQL/other database server Installation and start the MYSQL or Other
Database Server and MYSQL/other client.
Page 73 of 26
Page 74 of 26
Page 75 of 26
2) Create a database table to store the records of students in college. Use getConnection function
to connect the database. The statement object uses executeUpdate function to create a table.
3) Create a database of employee of company in mysql and then use java program to access the
database for inserting information of employees in database. The SQL statement can be used
to view the details of the data of employees in the database.
Page 76 of 26
Page 77 of 26