You are on page 1of 46

Shri Ramdeobaba College of Engineering & Management Nagpur-13

Department of Electronic Design Technology


Laboratory Journal
Subject: Object Oriented Programming Lab (EDP357)
(B.E. VI-SEM)

(Session 2020-21)

Name of Student: Rushikesh Mahajan


Roll Number: 35
EDP 357
Object Oriented Programming

Rushikesh Mahajan 35
EDP 357
Contents:
Experiment number: 1

(A) Write a Java program to print the odd numbers from 1 to 99. Print one

number per line.

(B) Write a Java program to check whether the number entered by the user

is Armstrong number or not.

Experiment number: 2

(A) Write a Java program to reverse a word. (Do not use built-in method)

(B) Write a Java program to convert a binary number to decimal number.

Experiment number: 3

(A) Write a Java program to find the common elements between two arrays

of integers.

(B) W Write a java program to multiply the two matrices of the same size

(3x3).

Experiment number: 4

(A) Write a Java program to compare two strings lexicographically, ignoring

case differences.

(B) Write a Java program to concatenate a given string to the end of another

string. (Do not use built-in method)

Experiment number: 5

(A) Create a class named 'Employee' with String variable 'Employee_name',

integer variable 'basic_sal', ‘age’, ‘year_of_Experience’. Create a display

method in this class which will display Employee_Name, 'basic_sal' and age

and year of experience of employee in proper format.

(B) Write a java program to Handel the exception in divide by zero situation.

Use try... catch exception.

Rushikesh Mahajan 35
EDP 357
Experiment number: 6

(A) Write a java program to create a LinkedList and to remove an element

from a LinkedList using following methods:

• remove(Object): This method is used to simply remove an object from

the LinkedList.

• remove(int index): Since a LinkedList is indexed, this method takes an

integer value which simply removes the element present at that

specific index in the LinkedList.

(B) Write a java program to create a LinkedList and iterate through the

elements in LinkedList. Use get() method to get the element at a specific

index and the advanced for loop..

Experiment number: 7

(A) Write a java program to create a ArrayList and add elements in the

created ArrayList using following methods:

• add(Object): This method is used to add an element at the end of the

ArrayList.

• add(int index, Object): This method is used to add an element at a

specific index in the ArrayList.

(B) Write a java program to create a ArrayList and change the elements in

ArrayList using set() method.

Experiment number: 8

(A) Write a Java program to write and read a plain text file.

(B) Write a Java program to check if a file or directory has read and write

permission.

Rushikesh Mahajan 35
EDP 357
Experiment Number: 9

(A) Write a java code to create a frame with your name as text message.

(B) Write a java code to create a chat window.

Experiment Number: 10

(A) Write a Java program for design of user login window using swing

components.

(B) Write a Java program to display message on mouse click by

implementing ActionListener.

Rushikesh Mahajan 35
EDP 357
Experiment no. 1 A)
Program:

import java.util.*;

public class odd {

public static void main(String[] args){

for (int i = 1; i < 100; i++) {

if (i % 2 != 0) {

System.out.println(i);

Output:

Rushikesh Mahajan 35
EDP 357
Experiment no.1 B)
Program:

import java.util.Scanner;

public class ArmStrong

public static void main(String[] args)

int n, count = 0, a, b, c, sum = 0;

Scanner s = new Scanner(System.in);

System.out.print("Enter any integer you want to check:");

n = s.nextInt();

a = n;

c = n;

while(a > 0)

a = a / 10;

count++;

while(n > 0)

b = n % 10;

sum = (int) (sum+Math.pow(b, count));

n = n / 10;

if(sum == c)

System.out.println("Given number is Armstrong");

Rushikesh Mahajan 35
EDP 357
else

System.out.println("Given number is not Armstrong");

Output:

Rushikesh Mahajan 35
EDP 357
Experiment 2 A)
Program:

import java.util.Scanner;

public class program2a {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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

String originalStr = scanner.nextLine();

scanner.close();

String words[] = originalStr.split("\\s");

String reversedString = "";

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

String word = words[i];

String reverseWord = "";

for (int j = word.length() - 1; j >= 0; j--) {

reverseWord = reverseWord + word.charAt(j);

reversedString = reversedString + reverseWord + " ";

// Displaying the string after reverse

System.out.print("Reversed string : " + reversedString);

Rushikesh Mahajan 35
EDP 357
}

Output:

Rushikesh Mahajan 35
EDP 357
Experiment 2 B)
Program:

import java.util.Scanner;

class program2b {

public static void main(String args[]){

Scanner input = new Scanner( System.in );

System.out.print("Enter a binary number: ");

String binaryString =input.nextLine();

System.out.println("Output: "+Integer.parseInt(binaryString,2));

Output:

Rushikesh Mahajan 35
EDP 357
Experiment 3 A)
Program:

import java.util.Arrays;

public class Exercise15 {

public static void main(String[] args)

int[] array1 = {1, 2, 5, 5, 8, 9, 7, 10};

int[] array2 = {1, 0, 6, 15, 6, 4, 7, 0};

System.out.println("Array1 : "+Arrays.toString(array1));

System.out.println("Array2 : "+Arrays.toString(array2));

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

for (int j = 0; j < array2.length; j++)

if(array1[i] == (array2[j]))

System.out.println("Common element is : "+(array1[i]));

Rushikesh Mahajan 35
EDP 357
Output:

Experiment 3 B)
Program:

public class program3b {

public static void main(String args[]){

//creating two matrices

int a[][]={{1,1,1},{2,2,2},{3,3,3}};

int b[][]={{1,1,1},{2,2,2},{3,3,3}};

//creating another matrix to store the multiplication of two matrices

int c[][]=new int[3][3]; //3 rows and 3 columns

Rushikesh Mahajan 35
EDP 357
//multiplying and printing multiplication of 2 matrices

for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

c[i][j]=0;

for(int k=0;k<3;k++)

c[i][j]+=a[i][k]*b[k][j];

}//end of k loop

System.out.print(c[i][j]+" "); //printing matrix element

}//end of j loop

System.out.println();//new line

}}

Output:

Rushikesh Mahajan 35
EDP 357
Rushikesh Mahajan 35
EDP 357
Experiment 4 A)
Program :

public static void main(String[] args)

String str1 = "This is exercise 1";

String str2 = "This is Exercise 1";

System.out.println("String 1: " + str1);

System.out.println("String 2: " + str2);

// Compare the two strings.

int result = str1.compareToIgnoreCase(str2);

// Display the results of the comparison.

if (result < 0)

System.out.println("\"" + str1 + "\"" +

" is less than " +

"\"" + str2 + "\"");

else if (result == 0)

System.out.println("\"" + str1 + "\"" +

" is equal to " +

"\"" + str2 + "\"");

else // if (result > 0)

Rushikesh Mahajan 35
EDP 357
System.out.println("\"" + str1 + "\"" +

" is greater than " +

"\"" + str2 + "\"");

Output:

Rushikesh Mahajan 35
EDP 357
Experiment 4 B)
Program:

public class fourBee {

public static void main(String[] args)

String str1 = "PHP Exercises and ";

String str2 = "Python Exercises";

System.out.println("String 1: " + str1);

System.out.println("String 2: " + str2);

// Concatenate the two strings together.

String str3 = str1.concat(str2);

// Display the new String.

System.out.println("The concatenated string: " + str3);

Output:

Rushikesh Mahajan 35
EDP 357
Experiment 5 A)
Program:

import java.util.Scanner;

public class Main {

int empid;

String name;

float salary;

public void getInput() {

Scanner in = new Scanner(System.in);

System.out.print("Enter the empid :: ");

empid = in.nextInt();

System.out.print("Enter the name :: ");

name = in.next();

System.out.print("Enter the salary :: ");

salary = in.nextFloat();

public void display() {

System.out.println("Employee id = " + empid);

System.out.println("Employee name = " + name);

System.out.println("Employee salary = " + salary);

public static void main(String[] args) {

Main e[] = new Main[5];

for(int i=0; i<5; i++) {

e[i] = new Main ();

System.out.println("enter detail of emp "+(i+1));

Rushikesh Mahajan 35
EDP 357
e[i].getInput();

System.out.println("**** Data Entered as below ****");

for(int i=0; i<5; i++) {

e[i].display();

} } }

Output:

Rushikesh Mahajan 35
EDP 357
Experiment 5 B)
Program:

class program5b {

public static void main (String args[]) {

int num1 = 15, num2 = 0, result = 0;

try{

result = num1/num2;

System.out.println("The result is" +result);

catch (ArithmeticException e) {

System.out.println ("Can't be divided by Zero " + e);

Output:

Rushikesh Mahajan 35
EDP 357
Experiment 6 A)
Program:

import java.io.*;

import java.util.LinkedList;

public class program6a {

public static void main(String args[])

// Creating an empty LinkedList

LinkedList<String> list = new LinkedList<String>();

// Using add() method to add elements in the list

list.add("Geeks");

list.add("for");

list.add("Geeks");

list.add("10");

list.add("20");

// Output the list

System.out.println("LinkedList:" + list);

// Remove the head using remove()

list.remove();

// Print the final list

System.out.println("Final LinkedList:" + list);

Rushikesh Mahajan 35
EDP 357
}

Output:

Rushikesh Mahajan 35
EDP 357
Experiment 6 B)
Program:

import java.util.LinkedList;

import java.util.ListIterator;

public class linked {

public static void main(String args[])

// Creating an empty LinkedList

LinkedList<String> list = new LinkedList<String>();

// Use add() method to add elements in the list

list.add("Si");

list.add("for");

list.add("Ne");

list.add("10");

list.add("20");

// Displaying the linkedlist

System.out.println("LinkedList:" + list);

// Setting the ListIterator at a specified position

ListIterator list_Iter = list.listIterator(2);

// Iterating through the created list from the position

System.out.println("The list is as follows:");

while(list_Iter.hasNext()){

System.out.println(list_Iter.next());

Rushikesh Mahajan 35
EDP 357
}

Output:

Rushikesh Mahajan 35
EDP 357
Experiment 7 A)
Program:

import java.util.ArrayList;

public class obj1 {

public static void main(String[] args)

// create an empty array list with an initial capacity

ArrayList<Integer> arrlist = new ArrayList<Integer>(5);

// use add() method to add elements in the list

arrlist.add(9);

arrlist.add(18);

arrlist.add(27);

// prints all the elements available in list

for (Integer number : arrlist) {

System.out.println("Number = " + number);

Rushikesh Mahajan 35
EDP 357
Output:

Rushikesh Mahajan 35
EDP 357
Experiment 7 B)
Program:

import java.util.ArrayList;

public class program7b {

public static void main(String args[]) {

ArrayList<Integer> arraylist = new ArrayList<Integer>();

arraylist.add(1);

arraylist.add(2);

arraylist.add(3);

arraylist.add(4);

arraylist.add(5);

arraylist.add(6);

arraylist.add(7);

System.out.println("ArrayList before update: "+arraylist);

//Updating 1st element

arraylist.set(0, 11);

//Updating 2nd element

arraylist.set(1, 22);

//Updating 3rd element

arraylist.set(2, 33);

//Updating 4th element

arraylist.set(3, 44);

//Updating 5th element

arraylist.set(4, 55);

System.out.println("ArrayList after Update: "+arraylist);

Rushikesh Mahajan 35
EDP 357
Output:

Rushikesh Mahajan 35
EDP 357
Experiment 8 A)
Program:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.FileReader;

import java.io.FileWriter;

public class oopphh {

public static void main(String a[]){

StringBuilder sb = new StringBuilder();

String strLine = "";

try

String filename= "/home/students/myfile.txt";

FileWriter fw = new FileWriter(filename,false);

//appends the string to the file

fw.write("Python Exercises\n");

fw.close();

BufferedReader br = new BufferedReader(new FileReader("/home/students/myfile.txt"));

//read the file content

while (strLine != null)

sb.append(strLine);

sb.append(System.lineSeparator());

strLine = br.readLine();

System.out.println(strLine);

br.close();

Rushikesh Mahajan 35
EDP 357
}

catch(IOException ioe)

System.out.println("IOException: " + ioe.getMessage());

Output:

Rushikesh Mahajan 35
EDP 357
Experiment 8 B)
Program:

import java.io.File;

public class oopphh {

public static void main(String[] args) {

// Create a File object

File my_file_dir = new File("/home/students/abc.txt");

if (my_file_dir.canWrite())

System.out.println(my_file_dir.getAbsolutePath() + " can write.\n");

else

System.out.println(my_file_dir.getAbsolutePath() + " cannot write.\n");

if (my_file_dir.canRead())

System.out.println(my_file_dir.getAbsolutePath() + " can read.\n");

else

System.out.println(my_file_dir.getAbsolutePath() + " cannot read.\n");

Rushikesh Mahajan 35
EDP 357
Output:

Rushikesh Mahajan 35
EDP 357
Experiment 9 A)
Program:

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Component;

import java.awt.Frame;

import java.awt.TextArea;

public class Program9a {

public static void main(String[] args) {

// Create frame with specific title

Frame frame = new Frame("Example Frame");

// Create a component to add to the frame; in this case a text area with sample text

Component textArea = new TextArea("Rushikesh Mahajan");

// Create a component to add to the frame; in this case a button

Component button = new Button("Click Me!!");

// Add the components to the frame; by default, the frame has a border layout

frame.add(textArea, BorderLayout.NORTH);

frame.add(button, BorderLayout.SOUTH);

// Show the frame

int width = 300;

int height = 300;

frame.setSize(width, height);

frame.setVisible(true);

Rushikesh Mahajan 35
EDP 357
Output:

Rushikesh Mahajan 35
EDP 357
Experiment 9 B)
Program:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.event.*;

class Chatbox extends JFrame

JPanel jp;

JTextField jt;

JTextArea ta;

JLabel l;

boolean typing;

Timer t;

public Chatbox()

createAndShowGUI();

private void createAndShowGUI()

// Set frame properties

setTitle("Chatbox");

setDefaultCloseOperation(EXIT_ON_CLOSE);

// Create a JPanel and set layout

Rushikesh Mahajan 35
EDP 357
jp=new JPanel();

jp.setLayout(new GridLayout(2,1));

l=new JLabel();

jp.add(l);

// Create a timer that executes every 1 millisecond

t=new Timer(1,new ActionListener(){

public void actionPerformed(ActionEvent ae)

// If the user isn't typing, he is thinking

if(!typing)

l.setText("Thinking..");

});

// Set initial delay of 2000 ms

// That means, actionPerformed() is executed 2500ms

// after the start() is called

t.setInitialDelay(2000);

// Create JTextField, add it.

jt=new JTextField();

jp.add(jt);

// Add panel to the south,

add(jp,BorderLayout.SOUTH);

Rushikesh Mahajan 35
EDP 357
// Add a KeyListener

jt.addKeyListener(new KeyAdapter(){

public void keyPressed(KeyEvent ke)

// Key pressed means, the user is typing

l.setText("You are typing..");

// When key is pressed, stop the timer

// so that the user is not thinking, he is typing

t.stop();

// He is typing, the key is pressed

typing=true;

// If he presses enter, add text to chat textarea

if(ke.getKeyCode()==KeyEvent.VK_ENTER) showLabel(jt.getText());

public void keyReleased(KeyEvent ke)

// When the user isn't typing..

typing=false;

// If the timer is not running, i.e.

// when the user is not thinking..

if(!t.isRunning())

Rushikesh Mahajan 35
EDP 357
// He released a key, start the timer,

// the timer is started after 2500ms, it sees

// whether the user is still in the keyReleased state

// which means, he is thinking

t.start();

});

// Create a textarea

ta=new JTextArea();

// Make it non-editable

ta.setEditable(false);

// Set some margin, for the text

ta.setMargin(new Insets(7,7,7,7));

// Set a scrollpane

JScrollPane js=new JScrollPane(ta);

add(js);

addWindowListener(new WindowAdapter(){

public void windowOpened(WindowEvent we)

// Get the focus when window is opened

jt.requestFocus();

});

Rushikesh Mahajan 35
EDP 357
setSize(400,400);

setLocationRelativeTo(null);

setVisible(true);

private void showLabel(String text)

// If text is empty return

if(text.trim().isEmpty()) return;

// Otherwise, append text with a new line

ta.append(text+"\n");

// Set textfield and label text to empty string

jt.setText("");

l.setText("");

public static void main(String args[])

SwingUtilities.invokeLater(new Runnable(){

public void run()

new Chatbox();

});

Rushikesh Mahajan 35
EDP 357
Output:

Rushikesh Mahajan 35
EDP 357
Experiment 10 A)
Program:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.sql.*;

public class loginapp extends JFrame implements ActionListener

JLabel l1, l2, l3;

JTextField tf1;

JButton btn1;

JPasswordField p1;

loginapp()

setTitle("Login Form in Windows Form");

setVisible(true);

setSize(800, 800);

setLayout(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

l1 = new JLabel("Login Form in Windows Form:");

l1.setForeground(Color.blue);

l1.setFont(new Font("Serif", Font.BOLD, 20));

l2 = new JLabel("Enter Email:");

l3 = new JLabel("Enter Password:");

tf1 = new JTextField();

p1 = new JPasswordField();

Rushikesh Mahajan 35
EDP 357
btn1 = new JButton("Submit");

l1.setBounds(100, 30, 400, 30);

l2.setBounds(80, 70, 200, 30);

l3.setBounds(80, 110, 200, 30);

tf1.setBounds(300, 70, 200, 30);

p1.setBounds(300, 110, 200, 30);

btn1.setBounds(150, 160, 100, 30);

add(l1);

add(l2);

add(tf1);

add(l3);

add(p1);

add(btn1);

btn1.addActionListener(this);

public void actionPerformed(ActionEvent e)

showData();

public void showData()

JFrame f1 = new JFrame();

JLabel l, l0;

String str1 = tf1.getText();

char[] p = p1.getPassword();

String str2 = new String(p);

try

{ Class.forName("oracle.jdbc.driver.OracleDriver");
Rushikesh Mahajan 35
EDP 357
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521:xe",
"sandeep", "welcome");

PreparedStatement ps = con.prepareStatement("select name from reg where email=? and


pass=?");

ps.setString(1, str1);

ps.setString(2, str2);

ResultSet rs = ps.executeQuery();

if (rs.next())

f1.setVisible(true);

f1.setSize(600, 600);

f1.setLayout(null);

l = new JLabel();

l0 = new JLabel("you are succefully logged in..");

l0.setForeground(Color.blue);

l0.setFont(new Font("Serif", Font.BOLD, 30));

l.setBounds(60, 50, 400, 30);

l0.setBounds(60, 100, 400, 40);

f1.add(l);

f1.add(l0);

l.setText("Welcome " + rs.getString(1));

l.setForeground(Color.red);

l.setFont(new Font("Serif", Font.BOLD, 30));

} else

JOptionPane.showMessageDialog(null,

"Incorrect email-Id or password..Try Again with correct detail");

Rushikesh Mahajan 35
EDP 357
}

catch (Exception ex)

System.out.println(ex);

public static void main(String arr[])

new loginapp();

Output:

Rushikesh Mahajan 35
EDP 357
Experiment 10 B)
Program:

import java.awt.*;

import java.awt.event.*;

public class program10b {

public static void main(String[] args) {

Frame f=new Frame("ActionListener Example");

final TextField tf=new TextField();

tf.setBounds(50,50, 150,20);

Button b=new Button("Click Here");

b.setBounds(50,100,60,30);

b.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

tf.setText("Welcome to Javatpoint.");

});

f.add(b);f.add(tf);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

Rushikesh Mahajan 35
EDP 357
Output:

Rushikesh Mahajan 35
EDP 357

You might also like