Professional Documents
Culture Documents
EXPERIMENT NO 1
AIM: - Write a program to count the number of words that start with a capital letter.
Code:-
import java.util.*;
class Cap
{
public static void main(String m[])
{
Scanner in=new Scanner(System.in);
String s=new String();
System.out.println("Enter a line:");
s=in.nextLine();
char c;
int ct=0;
for(int i=0;i<s.length();i++)
{
c=s.charAt(i);
if(c>=65 && c<=90)
{
ct++;
}
}
System.out.println("total number of words start with capital letters are :"+ct);
}
}
Faculty of Engineering and Technology
Subject name : OOPJ
Subject Code : 203105334
B-tech : CSE
Year :3
Semester :5
Name : Ayan Siddiqui
Enrollment No : 190305105250
EXPERIMENT NO 1.1
AIM: - Write a java program to take an array of string as an input, and arrange strings in
ascending order.
Code:-
public class SortAsc {
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;
//Displaying elements of original array
System.out.println("Elements of original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
//Sort the array in ascending order
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();
//Displaying elements of array after sorting
System.out.println("Elements of array sorted in ascending order: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
Output:
EXPERIMENT NO 2
AIM: - Write a program to find the largest number in an array of numbers using
command line arguments.
Code:-
public class LargestInArrayExample{
public static int getLargest(int[] a, int total){
int temp;
for (int i = 0; i < total; i++)
{
for (int j = i + 1; j < total; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[total-1];
}
public static void main(String args[]){
int a[]={1,2,5,6,3,2};
int b[]={44,66,99,77,33,22,55};
System.out.println("Largest: "+getLargest(a,6));
System.out.println("Largest: "+getLargest(b,7));
}}
Output:
Largest: 6
Largest: 99
EXPERIMENT NO 2.1
AIM: - Write a program to find factorial of number, Here, take number as command line
argument.
Code:-
class GFG {
// Driver code
public static void main(String[] args)
{
Code:-
class GFG {
System.out.println(
"Student data in student arr 1: ");
arr[1].display();
}
}
Output
Student data in student arr 0:
Student id is: 190305105250 and Student name is: Ayan
EXPERIMENT NO 3.1
AIM: - Declare a class box. Overload Box constructions with zero argument, one
argument and three argument to initialize the members of the class. Declare a method to
find the volume of the box.
Code:-
// Driver code
public class Test
{
public static void main(String args[])
{
// create boxes using the various
// constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
EXPERIMENT NO 4
AIM: - Write a program to show demonstrate garbage collection using System.gc() or
Runtime.gc()
Code:-
@Override
// finalize method is called on object once
// before garbage collecting it
protected void finalize() throws Throwable
{
System.out.println("Garbage collector called");
System.out.println("Object garbage collected : " + this);
}
}
EXPERIMENT NO 4.1
AIM: - Write a program to show the use of finalize method for garbage collection.
Code:-
class A
{
int i;
public A(int i)
{
this.i = i;
}
@Override
protected void finalize() throws Throwable
{
System.out.println("From Finalize Method, i = "+i);
//Calling super class finalize() method explicitly
super.finalize();
}
}
public class Test
{
public static void main(String[] args)
{
//Creating two instances of class A
A a1 = new A(10);
A a2 = new A(20);
//Calling finalize() method of a1 before it is abandoned
try
{
a1.finalize();
}
catch (Throwable e)
{
e.printStackTrace();
}
//Assigning a2 to a1
a1 = a2;
//Now both a1 and a2 will be pointing same object
//An object earlier referred by a1 will become abandoned
System.out.println("done");
}
}
EXPERIMENT NO 5
AIM: - Write a program to demonstrate static constants and final constants.
Code:-
//Java Program to demonstrate the use of static variable
class Student{
int rollno;//instance variable
String name;
static String college ="ITS";//static variable
//constructor
Student(int r, String n){
rollno = r;
name = n;
}
//method to display the values
void display (){System.out.println(rollno+" "+name+" "+college);}
}
//Test class to show the values of objects
public class TestStaticVariable1{
public static void main(String args[]){
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
//we can change the college of all objects by the single line of code
//Student.college="BBDIT";
s1.display();
s2.display();
}
}
Output:
class Bike{
final void run(){System.out.println("running");}
}
class Honda extends Bike{
void run(){System.out.println("running safely with 100kmph");}
public static void main(String args[]){
Honda honda= new Honda();
honda.run();
}
}
EXPERIMENT NO 5.1
AIM: - Write a program to create a class named as bike which consist one final method
called as run(). Declare a subclass Bike & demonstrate the use of final method.
Code:-
class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
}//end of class
class Bike10{
final int speedlimit;//blank final variable
Bike10(){
speedlimit=70;
System.out.println(speedlimit);
}
public static void main(String args[]){
new Bike10();
}
}
EXPERIMENT NO 6
AIM: - Write a program to explain static polymorphism in java.
Code:-
EXPERIMENT NO 6.1
AIM: - Write a program to find volume of box using concept of method
overloading .
Code:-
1. Create a user-defined package box which has a class definition. For box having
data member and disp( )method . (Assume suitable data )
2. Source file imports above package and calculates the volume of box .
import java.lang.*;
import java.io.*;
Pacakage BoxPackage
Length = l;
breadth= b;
height = h;
int volume=length*breadth*height;
import.BoxPackage.*;
class Boxdemo
b.display();
}
EXPERIMENT NO 7
AIM: - Write a program to find the factorial of a number using interface.
Code:-
class FactorialExample2{
static int factorial(int n){
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
public static void main(String args[]){
int i,fact=1;
int number=4;//It is the number to calculate factorial
fact = factorial(number);
System.out.println("Factorial of "+number+" is: "+fact);
}
}
Output:
Factorial of 4 is: 24
EXPERIMENT NO 7.1
AIM: - Write a program to implement multiple inheritance in java using interface.
Code:-
interface AnimalEat {
void eat();
interface AnimalTravel {
void travel();
System.out.println("Animal is eating");
System.out.println("Animal is travelling");
a.travel();
Output :-
Animal is eating
Animal is travelling
EXPERIMENT NO 7.2
AIM: - Create a package called Mathsoperation1, which contain classes to perform
addition, subtraction, Create another package called Mathoperation2, which must
contain classes to perform multiplication and division operation. Create a main
class and import the Mathsoperation1, Mathsoperation1 package in to perform all
the operations on the input numbers provided by the user. Finally, display the
result of each operation on the console.
Code:-
EXPERIMENT NO 8
AIM: - Write a program to design student registration form using AWT
components.
Code:-
PROCEDURE:
1)Start the program.
2) Create a class frame that extends frame and implements ActionListener interface.
SOURCE CODE:
import java.awt.*;
import java.awt.event.*;
class frame extends Frame implements ActionListener
{
Frame f;
frame(String title,Frame f)
{
super(title);
MenuBar mb=new MenuBar();
setMenuBar(mb);
Menu mr=new Menu("Registration");
MenuItem mi=new MenuItem("Register");
MenuItem m1=new MenuItem("-");
MenuItem m=new MenuItem("Exit");
mb.add(mr);
mr.add(mi);
mr.add(m1);
mr.add(m);
mi.addActionListener(this);
m.addActionListener(this);
this.f=f;
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("Register"))
f.setVisible(true);
if(str.equals("Exit"))
System.exit(0);
}
}
Code:-
import java.awt.*;
import java.awt.event.*;
int c,n;
String s1,s2,s3,s4,s5;
Frame f;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17;
Panel p;
TextField tf;
GridLayout g;
calculator()
p = new Panel();
f.setLayout(new FlowLayout());
b1 = new Button("0");
b1.addActionListener(this);
b2 = new Button("1");
b2.addActionListener(this);
b3 = new Button("2");
b3.addActionListener(this);
b4 = new Button("3");
b4.addActionListener(this);
b5 = new Button("4");
b5.addActionListener(this);
b6 = new Button("5");
b6.addActionListener(this);
b7 = new Button("6");
b7.addActionListener(this);
b8 = new Button("7");
b8.addActionListener(this);
b9 = new Button("8");
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
tf = new TextField(20);
f.add(tf);
g = new GridLayout(4,4,10,20);
p.setLayout(g);
p.add(b1);p.add(b2);p.add(b3);p.add(b4);p.add(b5);p.add(b6);p.add(b7);p.add(b8
);p.add(b9);
p.add(b10);p.add(b11);p.add(b12);p.add(b13);p.add(b14);p.add(b15);p.add(b16);
p.add(b17);
f.add(p);
f.setSize(300,300);
f.setVisible(true);
if(e.getSource()==b1)
s3 = tf.getText();
s4 = "0";
s5 = s3+s4;
tf.setText(s5);
if(e.getSource()==b2)
s3 = tf.getText();
s4 = "1";
s5 = s3+s4;
tf.setText(s5);
if(e.getSource()==b3)
s3 = tf.getText();
s4 = "2";
s5 = s3+s4;
tf.setText(s5);
}if(e.getSource()==b4)
s3 = tf.getText();
s4 = "3";
s5 = s3+s4;
tf.setText(s5);
if(e.getSource()==b5)
s3 = tf.getText();
s4 = "4";
s5 = s3+s4;
tf.setText(s5);
if(e.getSource()==b6)
s3 = tf.getText();
s4 = "5";
s5 = s3+s4;
tf.setText(s5);
if(e.getSource()==b7)
s3 = tf.getText();
s4 = "6";
s5 = s3+s4;
tf.setText(s5);
if(e.getSource()==b8)
s3 = tf.getText();
s4 = "7";
s5 = s3+s4;
tf.setText(s5);
if(e.getSource()==b9)
s3 = tf.getText();
s4 = "8";
s5 = s3+s4;
tf.setText(s5);
}
if(e.getSource()==b10)
s3 = tf.getText();
s4 = "9";
s5 = s3+s4;
tf.setText(s5);
if(e.getSource()==b11)
s1 = tf.getText();
tf.setText("");
c=1;
if(e.getSource()==b12)
s1 = tf.getText();
tf.setText("");
c=2;
}
if(e.getSource()==b13)
s1 = tf.getText();
tf.setText("");
c=3;
if(e.getSource()==b14)
s1 = tf.getText();
tf.setText("");
c=4;
if(e.getSource()==b15)
s1 = tf.getText();
tf.setText("");
c=5;
if(e.getSource()==b16)
{
s2 = tf.getText();
if(c==1)
n = Integer.parseInt(s1)+Integer.parseInt(s2);
tf.setText(String.valueOf(n));
else
if(c==2)
n = Integer.parseInt(s1)-Integer.parseInt(s2);
tf.setText(String.valueOf(n));
else
if(c==3)
n = Integer.parseInt(s1)*Integer.parseInt(s2);
tf.setText(String.valueOf(n));
if(c==4)
try
{
int p=Integer.parseInt(s2);
if(p!=0)
n=
Integer.parseInt(s1)/Integer.parseInt(s2);
tf.setText(String.valueOf(n));
else
tf.setText("infinite");
catch(Exception i){}
if(c==5)
n = Integer.parseInt(s1)%Integer.parseInt(s2);
tf.setText(String.valueOf(n));
if(e.getSource()==b17)
{
tf.setText("");
OUTPUT:
EXPERIMENT NO 9
AIM: - Write a program to demonstrate array index out of bounds exception.
Code:-
Example
import java.util.Arrays;
import java.util.Scanner;
Output
Elements in the array are::
[897, 56, 78, 90, 12, 123, 75]
Enter the index of the required element ::
7
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at AIOBSample.main(AIOBSample.java:12)
Handling the exception
You can handle this exception using try catch as shown below.
Example
import java.util.Arrays;
import java.util.Scanner;
EXPERIMENT NO 9.1
AIM: - Create an interface Account with two methods deposit and withdraw. Create class
Savings Account which implements the interface. Write a custom Exception handler for
Savings Account to handle the scenarios when withdraw amount is larger than the
balance in the account.
Code:-
class TestAccountInterface
transactOnAccount(account);
System.out.println();
transactOnAccount(account);
System.out.println("------------------------------");
account.deposit(10000.0);
account.withdraw(2500.0);
account.withdraw(4100.0);
account.deposit(5000.0);
System.out.println("------------------------------");
interface IAccount
double getBalance();
}
class HDFCAccount implements IAccount
double deposits;
double withdrawals;
deposits += amount;
withdrawals += amount;
double balance;
{
return balance;
balance += amount;
balance -= amount;
OUTPUT
------------------------------
------------------------------
------------------------------
The balance after depositing 10,000.0 is 10000.0.
------------------------------
EXPERIMENT NO 10
AIM: - Write a program to demonstrate class object locking using method level
synchronization.
Code:-
System.out.println(
Thread.currentThread().getName());
synchronized (Geek.class)
System.out.println(
System.out.println(
+ Thread.currentThread().getName()
+ " end");
t1.setName("t1");
t2.setName("t2");
t3.setName("t3");
t1.start();
t2.start();
t3.start();
}
}
Output:-
t1
t2
t3
in block t1
in block t1 end
in block t3
in block t3 end
in block t2
in block t2 end
EXPERIMENT NO 10.1
AIM: - Write a program that excludes two threads. One thread will print the even
numbers and another thread will print odd number 1 to 50.
Code:-
// Starting counter
int counter = 1;
static int N;
synchronized (this)
while (counter % 2 == 0) {
// Exception handle
try {
wait();
catch (
InterruptedException e) {
e.printStackTrace();
// Increment counter
counter++;
notify();
synchronized (this)
while (counter % 2 == 1) {
// Exception handle
try {
wait();
catch (
InterruptedException e) {
e.printStackTrace();
System.out.print(
// Increment counter
counter++;
notify();
// Driver Code
// Given Number N
N = 10;
// Create thread t1
mt.printEvenNumber();
});
// Create thread t2
mt.printOddNumber();
});
t1.start();
t2.start();
---------------------------------------*--------------------------------------------------*-----------------------------*----