Java Pratham Kakkar
Java Pratham Kakkar
OF
Pratham Kakkar
Roll no:09115603120
INDEX
Pratham Kakkar
Roll no:09115603120
19. Convert the content of a given file into the
uppercase content of the same file.
20. Write a program to perform operations such as
insertion, deletion, updation and
retrieval of records on Employee database using
JDBC.
21. Develop a scientific calculator using swings.
22. Create an editor like MS-word using swings.
Pratham Kakkar
Roll no:09115603120
Experiment No. 1
Aim: Write a program to perform addition of two numbers using command line.
Theory: In Java, finding the sum of two or more numbers is very easy. First,
declare and initialize two variables to be added. Another variable to store the sum
of numbers. Apply mathematical operator (+) between the declared variable and
store the result. The following program calculates and prints the sum of two
numbers.
Program:
Output:
Pratham Kakkar
Roll no:09115603120
Experiment No. 2
2) Instance Variable
A variable declared inside the class but outside the body of the method, is called an
instance variable. It is not declared as static.
3) Static variable
A variable that is declared as static is called a static variable. It cannot be local.
You can create a single copy of the static variable and share it among all the
instances of the class. Memory allocation for static variables happens only once
when the class is loaded in the memory.
Program:
public class A
{
static int m=100;//static variable
void method()
{
int n=90;//local variable
}
public static void main(String args[])
{
int data=50;//instance variable
}
}
Pratham Kakkar
Roll no:09115603120
Experiment No. 3
class Main {
public static void main(String[] args) {
char operator;
Double number1, number2, result;
Scanner input = new Scanner(System.in);
switch (operator) {
case '+':
result = number1 + number2;
System.out.println(number1 + " + " + number2 + " = " + result);
break;
case '-':
result = number1 - number2;
System.out.println(number1 + " - " + number2 + " = " + result);
break;
case '*':
result = number1 * number2;
System.out.println(number1 + " * " + number2 + " = " + result);
break;
case '/':
result = number1 / number2;
System.out.println(number1 + " / " + number2 + " = " + result);
Pratham Kakkar
Roll no:09115603120
break;
default:
System.out.println("Invalid operator!");
break;
}
input.close();
}
}
Output:
Pratham Kakkar
Roll no:09115603120
Experiment No. 4
Theory: Sorting an array means to arrange the elements in the array in a certain
order. Various algorithms have been designed that sort the array using different
methods. Some of these sorts are more useful than the others in certain situations.
Program:
import java.util.Arrays;
public class program {
Output:
Pratham Kakkar
Roll no:09115603120
Experiment No. 5
Theory: Stack- Stack is a linear data structure which follows a particular order in
which the operations are performed. The order may be LIFO(Last In First Out) or
FILO(First In Last Out).
Queue - A Queue is a linear structure which follows a particular order in which the
operations are performed. The order is First In First Out (FIFO).
Program:
Stack
class Stack {
static final int MAX = 1000;
int top;
int a[] = new int[MAX]; // Maximum size of Stack
boolean isEmpty()
{
return (top < 0);
}
Stack()
{
top = -1;
}
boolean push(int x)
{
if (top >= (MAX - 1)) {
System.out.println("Stack Overflow");
return false;
}
else {
a[++top] = x;
System.out.println(x + " pushed into stack \n");
return true;
}
Pratham Kakkar
Roll no:09115603120
}
int pop()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[top--];
return x;
}
}
int peek()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[top];
return x;
}
}
void print(){
for(int i = top;i>-1;i--){
System.out.print(" "+ a[i]);
}
}
}
class Main {
public static void main(String args[])
{
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
System.out.println(s.pop() + " Popped from stack");
Pratham Kakkar
Roll no:09115603120
System.out.println("Top element is :" + s.peek());
System.out.print("Elements present in stack :");
s.print();
}
}
Queue
class Queue {
private static int front, rear, capacity;
private static int queue[];
Queue(int c)
{
front = rear = 0;
capacity = c;
queue = new int[capacity];
}
static void queueEnqueue(int data)
{
if (capacity == rear) {
System.out.printf("\nQueue is full\n");
return;
}
else {
queue[rear] = data;
rear++;
}
return;
}
static void queueDequeue()
{
if (front == rear) {
System.out.printf("\nQueue is empty\n");
return;
}
else {
for (int i = 0; i< rear - 1; i++) {
queue[i] = queue[i + 1];
}
if (rear < capacity)
Pratham Kakkar
Roll no:09115603120
queue[rear] = 0;
rear--;
}
return;
}
static void queueDisplay()
{
int i;
if (front == rear) {
System.out.printf("\nQueue is Empty\n");
return;
}
for (i = front; i< rear; i++) {
System.out.printf(" %d <-- ", queue[i]);
}
return;
}
static void queueFront()
{
if (front == rear) {
System.out.printf("\nQueue is Empty\n");
return;
}
System.out.printf("\nFront Element is: %d", queue[front]);
return;
}
}
Output:
Pratham Kakkar
Roll no:09115603120
Experiment No. 6
Program:
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}
}
Output:
weeping...
barking...
eating...
Pratham Kakkar
Roll no:09115603120
Experiment No. 7
Theory: A class inherits properties from a class which again has inherits
properties.
Program:
class Shape {
public void display() {
System.out.println("Inside display");
}
}
class Rectangle extends Shape {
public void area() {
System.out.println("Inside area");
}
}
class Cube extends Rectangle {
public void volume() {
System.out.println("Inside volume");
}
}
public class Tester {
public static void main(String[] arguments) {
Cube cube = new Cube();
cube.display();
cube.area();
cube.volume();
}
}
Output:
Pratham Kakkar
Roll no:09115603120
Experiment No. 8
Theory:
Pratham Kakkar
Roll no:09115603120
Program for Polymorphism:
class A{
public void display(){
System.out.println("Printing from the Super Class");
}
}
class B extends A{
public void display(){
super.display();
System.out.println("Printing from the Sub Class");
}
public static void main(String[] args){
B obj=new B();
obj.display();
}
}
Output:
Pratham Kakkar
Roll no:09115603120
Experiment No. 9
Theory:
Program:
class overloading {
int a;
int b;
int c;
public overloading(int a, int b, int c){
this.a=a;
this.b=b;
this.c=c;
}
public overloading(int a, int b){
this.a=a;
this.b=b;
this.c=0;
}
public overloading(){
this.a=0;
this.b=0;
this.c=0;
}
public void display(){
System.out.println("a = "+a+"\tb = "+b+"\tc = "+c);
}
public static void main(String[] args){
overloading first= new overloading(1,2,3);
overloading second=new overloading(1,2);
overloading third=new overloading();
System.out.println("Values from first object : ");
first.display();
System.out.println("============================================
================================");
System.out.println("Values from second object : ");
Pratham Kakkar
Roll no:09115603120
second.display();
System.out.println("============================================
================================");
System.out.println("Values from third object : ");
third.display();
}
}
Output:
Pratham Kakkar
Roll no:09115603120
Experiment No. 10
Aim: Create a customized exception and also make use of all the 5 exception
keywords.
Theory:
Program:
class myException extends Exception{
public myException(String message){
super(message);
}
}
public class main{
static void check(int number) throws myException{
if(number<0 || number>99)
throw new myException("Entered Number is not a whole
number(<100).");
else
System.out.println("You've entered a whole number.");
}
public static void main(String[] args)
{
try{
check(Integer.valueOf(args[0]));
}
catch(myException e){
System.out.println("Exception Caught" + e);
}
finally{
System.out.println("This is how we managed the custom Exception :)
");
}
}
}
Pratham Kakkar
Roll no:09115603120
Output:
Pratham Kakkar
Roll no:09115603120
Experiment No. 11
Theory:
Program:
package IPU;
public class ADGITM{
int numberOfConsents;
int totalStudents;
int numberOfRegularStudents;
boolean collegeOpen;
public ADGITM(int a, int b, int d){
numberOfConsents = a;
totalStudents = b;
numberOfRegularStudents = d;
collegeOpen=false;
}
public void showConsentOfStudents(){
System.out.println("Total Consents : "+ numberOfConsents);
System.out.println("Total Students : "+ totalStudents);
System.out.println("Total Number Of Students who attend classes
regularly : "+ numberOfRegularStudents);
System.out.println("Are Colleges Open? : "+ collegeOpen);
System.out.println("OOPS!! College authorities don't want it open.
YET!!!!!!\n :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :(
:( :( ");
}
}
package IPU;
public class MAIT{
int numberOfConsents;
int totalStudents;
int numberOfRegularStudents;
boolean collegeOpen;
public MAIT(int a, int b, int d){
numberOfConsents = a;
totalStudents = b;
Pratham Kakkar
Roll no:09115603120
numberOfRegularStudents = d;
if(numberOfConsents > totalStudents/2)
collegeOpen = true;
else
collegeOpen = false;
}
public void showConsentOfStudents(){
System.out.println("Total Consents : "+ numberOfConsents);
System.out.println("Total Students : "+ totalStudents);
System.out.println("Total Number Of Students who attend classes
regularly : "+ numberOfRegularStudents);
System.out.println("Are Colleges Open? : "+ collegeOpen);
if(collegeOpen)
System.out.println("YAYY!!!!!!\n :) :) :) :) :) :) :) :) :) :) :) :) :)
:) :) :) :) :) :) :)");
}
}
Code for Main Class:
import IPU.*;
class College
{
public static void main(String[] args){
ADGITM niec = new ADGITM(38, 70, 35);
MAIT mait= new MAIT(40, 70, 50);
System.out.println("For ADGITM : ");
niec.showConsentOfStudents();
System.out.println("\n\nFor MAIT : ");
mait.showConsentOfStudents();
}
}
Output:
Pratham Kakkar
Roll no:09115603120
Output:
Pratham Kakkar
Roll no:09115603120
Experiment No. 12
Aim: Write a program to show that String is immutable and String buffer is
mutable.
Program:
For immutable
class Testimmutablestring{
public static void main(String args[]){
String s="Sachin";
s.concat(" Tendulkar");
System.out.println(s);
}
}
Output:
Sachin
For mutable
class Testimmutablestring1{
public static void main(String args[]){
String s="Sachin";
s=s.concat(" Tendulkar");
System.out.println(s);
}
}
Output:
Sachin Tendulkar
Pratham Kakkar
Roll no:09115603120
Experiment No. 13
Theory:
Program:
import java.util.*;
class StringOperation
{
public static void main(String[] args)
{
String first="",second="";
Scanner sc=new Scanner(System.in);
System.out.println("String Operation");
System.out.println();
System.out.print("Enter the first Sting: ");
first=sc.nextLine();
System.out.print("Enter the second Sting: ");
second=sc.nextLine();
System.out.println("The strings are: "+first+" , "+second);
System.out.println("The length of the first string is :"+first.length());
System.out.println("The length of the second string is
:"+second.length());
System.out.println("The concatenation of first and second string is
:"+first.concat(" "+second));
System.out.println("The first character of " +first+" is: "+first.charAt(0));
System.out.println("The uppercase of " +first+" is: "+first.toUpperCase());
System.out.println("The lowercase of " +first+" is: "+first.toLowerCase());
System.out.print("Enter the occurance of a character in "+first+" : ");
String str=sc.next();
char c=str.charAt(0);
System.out.println("The "+c+" occurs at position " + first.indexOf(c)+ " in
" + first);
System.out.println("The substring of "+first+" starting from index 3 and
ending at 6 is: " + first.substring(3,7));
System.out.println("Replacing 'a' with 'o' in "+first+" is:
"+first.replace('a','o'));
boolean check=first.equals(second);
Pratham Kakkar
Roll no:09115603120
if(!check)
System.out.println(first + " and " + second + " are not same.");
else
System.out.println(first + " and " + second + " are same.");
}
}
Output:
Pratham Kakkar
Roll no:09115603120
Experiment No. 14
Theory:
Program:
classpractice {
publicstaticvoidmain(String[] args) {
intstrLength=str.length();
if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
System.out.println(str +" is a Palindrome");
} else {
System.out.println(str +"is Not a Palindrome");
}
}
}
Output:
Pratham Kakkar
Roll no:09115603120
Experiment No. 15
Theory:
Program:
import java.applet.Applet;
import java.awt.*;
import java.util.*;
public class analogClock extends Applet {
@Override
public void init()
{
// Applet window size & color
this.setSize(new Dimension(800, 400));
setBackground(new Color(50, 50, 50));
new Thread() {
@Override
public void run()
{
while (true) {
repaint();
delayAnimation();
}
}
}.start();
}
Pratham Kakkar
Roll no:09115603120
}
}
// 12 hour format
if (hour > 12) {
hour -= 12;
}
// Labeling
g.setColor(Color.black);
g.drawString("12", 390, 120);
g.drawString("9", 310, 200);
g.drawString("6", 400, 290);
g.drawString("3", 480, 200);
Output:
Pratham Kakkar
Roll no:09115603120
Experiment No. 16
Theory:
Program: (a)
class Table
{
/*synchronized*/ void printTable(int n)
{
//synchronized(this)
{
for(int i=1;i<=10;i++)
{
System.out.println(+n+"*"+i+"="+(n*i));
try
{
Thread.sleep(400);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
}
Pratham Kakkar
Roll no:09115603120
public void run()
{
t.printTable(2);
}
}
class ThreadWithoutSync
{
public static void main(String args[])
{
Table obj = new Table();
Mythread1 th1 = new Mythread1(obj);
Mythread2 th2 = new Mythread2(obj);
th1.start();
th2.start();
}
}
Pratham Kakkar
Roll no:09115603120
Output:
Program: (a)
class Table
{
synchronized void printTable(int n)
{
//synchronized(this)
{
for(int i=1;i<=10;i++)
{
System.out.println(+n+"*"+i+"="+(n*i));
try
{
Thread.sleep(400);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
Pratham Kakkar
Roll no:09115603120
}
class ThreadWithoutSync
{
public static void main(String args[])
{
Table obj = new Table();
Mythread1 th1 = new Mythread1(obj);
Mythread2 th2 = new Mythread2(obj);
th1.start();
th2.start();
}
}
Pratham Kakkar
Roll no:09115603120
Output:
Pratham Kakkar
Roll no:09115603120
Experiment No. 17
Theory:
Program:
import java.util.LinkedList;
public class Threadexample {
public static void main(String[] args)
throws InterruptedException
{
final PC pc = new PC();
Pratham Kakkar
Roll no:09115603120
t1.start();
t2.start();
t1.join();
t2.join();
}
public static class PC {
LinkedList<Integer> list = new LinkedList<>();
int capacity = 2;
public void produce() throws InterruptedException
{
int value = 0;
while (true) {
synchronized (this)
{
System.out.println("Producer produced-"
+ value);
list.add(value++);
notify();
Thread.sleep(1000);
}
}
}
public void consume() throws InterruptedException
{
while (true) {
synchronized (this)
{
while (list.size() == 0)
wait();
int val = list.removeFirst();
System.out.println("Consumer consumed-"
+ val);
notify();
Thread.sleep(1000);
Pratham Kakkar
Roll no:09115603120
}
}
}
}
}
Output:
Pratham Kakkar
Roll no:09115603120
Experiment No. 18
Aim: Write a program to perform event handling in Java. Create a GUI using
AWT that performs arithmetic operations.
Program:
#For Event Handling
import java.awt.*;
import java.awt.event.*;
class AEvent extends Frame implements ActionListener{
TextField tf;
AEvent(){
//create components
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);
//register listener
b.addActionListener(this);//passing current instance
Pratham Kakkar
Roll no:09115603120
new AEvent();
}
}
Output:
Program:
#For Arithmetic Operations
import java.awt.*;
public class AWTExample1 extends Frame {
AWTExample1() {
Button b = new Button("Click Me!!");
b.setBounds(30,100,80,30);
add(b);
setSize(300,300);
setTitle("This is our basic AWT example");
setLayout(null);
setVisible(true);
}
public static void main(String args[]) {
Pratham Kakkar
Roll no:09115603120
AWTExample1 f = new AWTExample1();
}
}
Output:
Pratham Kakkar
Roll no:09115603120
Experiment No. 19
Aim: Convert the content of a given file into the uppercase content of the same
file.
Theory:
Output:
Pratham Kakkar
Roll no:09115603120
Experiment No. 20
Theory:
Program:
import java.sql.*;
class EmployeeRecord
{
public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:XE";
public static final String DBUSER = "local";
public static final String DBPASS = "test";
public static void main(String args[])
{
try
{
//Loading the driver
Class.forName("oracle.jdbc.driver.OracleDriver");
//Cretae the connection object
Connection con = DriverManager.getConnection(DBURL, DBUSER,
DBPASS);
//Insert the record
String sql = "INSERT INTO emp (emp_id, empname, email, city)
VALUES (?, ?, ?, ?)";
PreparedStatement statement = con.prepareStatement(sql);
statement.setInt(1, 100);
statement.setString(2, "Prashant");
statement.setString(3, "[email protected]");
statement.setString(4, "Pune");
Pratham Kakkar
Roll no:09115603120
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery(sql1);
while (result.next())
{
System.out.println (result.getInt(1)+" "+
result.getString(2)+" "+
result.getString(3)+" "+
result.getString(4));
}
Pratham Kakkar
Roll no:09115603120
Output:
Pratham Kakkar
Roll no:09115603120
Experiment No. 21
Program:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
ScientificCalculator() {
cont = getContentPane();
cont.setLayout(new BorderLayout());
JPanel textpanel = new JPanel();
tfield = new JTextField(25);
tfield.setHorizontalAlignment(SwingConstants.RIGHT);
tfield.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent keyevent) {
char c = keyevent.getKeyChar();
Pratham Kakkar
Roll no:09115603120
if (c >= '0' && c <= '9') {
} else {
keyevent.consume();
}
}
});
textpanel.add(tfield);
buttonpanel = new JPanel();
buttonpanel.setLayout(new GridLayout(8, 4, 2, 2));
boolean t = true;
mr = new JButton("MR");
buttonpanel.add(mr);
mr.addActionListener(this);
mc = new JButton("MC");
buttonpanel.add(mc);
mc.addActionListener(this);
mp = new JButton("M+");
buttonpanel.add(mp);
mp.addActionListener(this);
mm = new JButton("M-");
buttonpanel.add(mm);
mm.addActionListener(this);
b1 = new JButton("1");
buttonpanel.add(b1);
b1.addActionListener(this);
b2 = new JButton("2");
buttonpanel.add(b2);
b2.addActionListener(this);
b3 = new JButton("3");
buttonpanel.add(b3);
b3.addActionListener(this);
b4 = new JButton("4");
buttonpanel.add(b4);
b4.addActionListener(this);
b5 = new JButton("5");
buttonpanel.add(b5);
Pratham Kakkar
Roll no:09115603120
b5.addActionListener(this);
b6 = new JButton("6");
buttonpanel.add(b6);
b6.addActionListener(this);
b7 = new JButton("7");
buttonpanel.add(b7);
b7.addActionListener(this);
b8 = new JButton("8");
buttonpanel.add(b8);
b8.addActionListener(this);
b9 = new JButton("9");
buttonpanel.add(b9);
b9.addActionListener(this);
eq = new JButton("=");
buttonpanel.add(eq);
eq.addActionListener(this);
Pratham Kakkar
Roll no:09115603120
double fact(double x) {
int er = 0;
if (x < 0) {
er = 20;
return 0;
}
double i, s = 1;
for (i = 2; i <= x; i += 1.0)
s *= i;
return s;
}
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFee
l");
} catch (Exception e) {
}
ScientificCalculator f = new ScientificCalculator();
f.setTitle("ScientificCalculator");
f.pack();
f.setVisible(true);
}
}
Output:
Pratham Kakkar
Roll no:09115603120
Experiment No. 22
Theory:
Program:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.hwpf.extractor.WordExtractor;
fs.writeFilesystem(out);
out.close();
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
Pratham Kakkar
Roll no:09115603120
}
TextAreaInDocument(){
area=new JTextArea(10,50);
area.setFont(new Font("courier New",Font.BOLD,12));
pane=new JScrollPane(area);
b1=new JButton("Create");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String text=area.getText();
String f=file.getPath();
writeDoc(f,text);
area.setText("");
}
});
Output:
Pratham Kakkar
Roll no:09115603120