You are on page 1of 52

SRI KRISHNA ARTS AND SCIENCE COLLEGE

COIMBATORE – 641 008

RECORD NOTE

DEPARTMENT: COMPUTER TECHNOLOGY AND DATA SCIENCE

NAME: ROLL NO:

PROGRAMME CLASS________________________

COURSE
SRI KRISHNA ARTS AND SCIENCE COLLEGE
COIMBATORE – 641 008

ROLL NO:

Certified Bonafide record of work done by


during the year 2023-2024.

Staff In-charge Head of the Department

Submitted to the Sri Krishna Arts & Science College (Autonomous) end

semester examination held on .

INTERNAL EXAMINER EXTERNAL EXAMNER


DECLARATION

I hereby declare that this record

of observations is based on the experiments carried out and recorded by me during

the laboratory classes “__________________________________” conducted

by SRI KRISHNA ARTS AND SCIENCE COLLEGE, Coimbatore-641 008.

Date: Signature of the Student

Name of the Student :


Roll Number :

Countersigned by Staff
INDEX

PAGE
EX.NO DATE JAVA PROGRAM SIGNATURE
NO

01 CLASSES AND OBJECTS 01

02 INTERFACE 08

03 USER DEFINED PACKAGES 15

04 MULTITHREADING 20

05 EXCEPTION HANDLING 23

06 APPLET TAGS 26

07 BINARY SEARCH IMPLEMENTATION 29

08 STACK IMPLEMENTATION 33

09 QUEUE IMPLEMENTATION 36

10 BINARY TREE IMPLEMENTATION 39

11 MERGE SORT IMPLEMENTATION 42

12 RADIX SORT IMPLEMENTATION 46


EX NO: 1 CLASSES AND OBJECTS

AIM:

ALGORITHM:

1
PROGRAM: 1

import java.util.Scanner;

public class AddTwo{

public static void main(String[]args)

Scanner tc=new Scanner(System.in);

int first=tc.nextInt();

int sec=tc.nextInt();

int sum=first+sec;

System.out.println(sum);

2
OUTPUT:1

3
PROGRAM:2

import java.util.Scanner;

class multiplication{

public static void main(String args[])

int n1,n2,product;

Scanner a=new Scanner(System.in);

System.out.println("enter no 1:");

n1=a.nextInt();

System.out.println("enter no 2:");

n2=a.nextInt();

a.close();

product=n1*n2;

System.out.println("answer:"+ product);

4
OUTPUT:2

5
PROGRAM:3

import java.util.Scanner;
class oddeven
{
public static void main(String args[])
{
Scanner Scan=new Scanner(System.in);
System.out.println("enter number to check odd or
even:");
int num=Scan.nextInt();
find_Oddeven(num);
}
static void find_Oddeven(int num)
{
if (num%2==0)
System.out.println(num+"is even.");
else
System.out.println(num+"is odd.");
}
}

6
OUTPUT:3

RESULT:
Thus, the above java program has been executed successfully.
7
EX NO: 2 INTERFACE

AIM:

ALGORITHM:

8
PROGRAM: 1

interface Printer
{
void print(String message);
}
class ConsolePrinter implements Printer
{
public void print(String message)
{
System.out.println("printing :" + message);
}
}
public class InterfaceExample
{
public static void main(String[]args)
{
Printer printer = new
ConsolePrinter();
printer.print ("Hello, Interface!");
}
}

9
OUTPUT:1

10
PROGRAM:2

interface Shape{
void draw();
}
class Circle implements Shape{
@Override
public void draw() {
System.out.println("drawing a circle.");
}
}
class Square implements Shape{
@Override
public void draw() {
System.out.println("drawing a square.");
}
}
public class Main{
public static void main(String[] args)
{
Shape circle = new Circle();
Shape square = new Square();
boolean drawCircle = true;
if (drawCircle) {
circle.draw();
}
else{
square.draw();
}
}
}

11
OUTPUT:2

12
PROGRAM:3

interface Shape
{
void draw();
}
class Circle implements Shape {
@Override
public void draw() {
System.out.println("Drawing a circle");
}
}
class Square implements Shape {
@Override
public void draw() {
System.out.println("Drawing a square ");
}
}
public class shapes{
public static void main(String[]args)
{
Shape circle = new Circle();
Shape square = new Square();
String shapeType = "Circle";
switch
(shapeType.toLowerCase()){
case"circle":
circle.draw();
break;
case"square":
square.draw();
break;
default:
System.out.println("unknow shape");
}}}
13
OUTPUT:3

RESULT:
Thus, the above java program has executed successfully.
14
EX NO: 3 USER DEFINED PACKAGES

AIM:

ALGORITHM:

15
PROGRAM:1

import mypack.Hello;
public class MyClass
{
public static void main(String args[])
{
Hello a = new Hello();
a.display();
}
}

package mypack;
public class Hello
{
public void display()
{
System.out.println("Welcome to the world of java
!!");
}
}

16
OUTPUT:1

17
PROGRAM:2

package userinput;

import java.util.Scanner;

public class UserInput {


public static int getUserInput() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number:");
return scanner.nextInt();
}
}

package calculator;

public class Addition {


public static int addNumbers(int num1, int num2 )
{
return num1+num2;
}
}

import userinput.UserInput;
import calculator.Addition;
public class add {
public static void main(String[]args) {
int number1 = UserInput.getUserInput();
int number2 = UserInput.getUserInput();
int sum = Addition.addNumbers(number1,number2);
System.out.println("sum:"+sum);
}}
18
OUTPUT:2

RESULT:
Thus, the above java program has executed successfully.
19
EX NO:4 MULTITHREADING

AIM:

ALGORITHM:

20
PROGRAM:1
class MyThread extends Thread
{
public void run() {
for(int i=1; i<=5; i++) {
System.out.println(Thread.currentThread().getId()+
"-value"+i);
}}}
public class MultiThreadExample {
public static void main(String args[]) {
MyThread thread1 = new MyThread();
MyThread thread2 = new MyThread();
thread1.start();
thread2.start();
}
}

21
OUTPUT:1

RESULT:
Thus, the above java program has executed successfully.
22
EX NO:5 EXCEPTION HANDLING

AIM:

ALGORITHM:
PROGRAM:1

public class exceptionhandlingexample {


public static void main(String[] args) {
try {
int result = divideNumbers(10, 0);
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.err.println("Error: Division by zero is
not allowed.");
} finally {
System.out.println("Finally block executed.");
}
}

private static int divideNumbers(int numerator,


int denominator) {
return numerator / denominator;
}
}

24
OUTPUT:1

RESULT:
Thus, the above java program has executed successfully.
25
EX NO: 6 APPLET TAGS

AIM:

ALGORITHM:

26
PROGRAM:1

package prg9;
import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet
{
int height, width;public void init()
{
height = getSize().height; width
= getSize().width; setName("MyApplet");
}
public void paint(Graphics g)
{
g.drawRoundRect(10, 30, 120, 120, 2, 3);
}
}

27
OUTPUT:1

RESULT:
Thus, the above java program has executed successfully.
28
EX NO: 7 BINARY SEARCH IMPLEMENTATION

AIM:

ALGORITHM:

29
PROGRAM:1

import java.util.Scanner;
public class BinarySearch {
static int binarySearch(int[] arr, int target)
{
int left = 0;
int right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;}
return -1;}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the
array: ");
int size = scanner.nextInt();
int[] sortedArray = new int[size];
System.out.println("Enter the sorted
30
elements of the array:");
for (int i = 0; i < size; i++) {
sortedArray[i] = scanner.nextInt();}
System.out.print("Enter the target element
to search: ");
int target = scanner.nextInt();
int result = binarySearch(sortedArray,
target);
if (result != -1)
System.out.println("Element found at
index " + result);
else
System.out.println("Element not found
in the array");
scanner.close();}}

31
OUTPUT:1

RESULT:
Thus, the above java program has executed successfully.
32
EX NO: 8 STACK IMPLEMENTATION

AIM:

ALGORITHM:

33
PROGRAM:1

import java.util.*;

public class StackExample {


public static void main(String args[]) {
Stack<Integer> stack = new Stack<>();

// Push elements onto the stack


stack.push(10);
stack.push(20);
stack.push(30);

// Pop and print elements


System.out.println("Pop: " + stack.pop());
System.out.println("Pop: " + stack.pop());

// Peek at the top element


System.out.println("Top element: "+
stack.peek());

// Check if the stack is empty


System.out.println("Is the stack empty? "
+ stack.isEmpty());
}
}

34
OUTPUT:1

RESULT:
Thus, the above java program has executed successfully.
35
EX NO: 9 QUEUE IMPLEMENTATION

AIM:

ALGORITHM:

36
PROGRAM:1

import java.util.LinkedList;
import java.util.Queue;

public class QueueExample {


public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();

// Enqueue elements
queue.offer(10);

queue.offer(20);
queue.offer(30);

// Dequeue and print elements


System.out.println("Dequeue: " +
queue.poll());
System.out.println("Dequeue: " +
queue.poll());

// Peek at the front element


System.out.println("Front element: " + q
queue.peek());

// Check if the queue is empty


System.out.println("Is queue empty? " +
queue.isEmpty());
}
}

37
OUTPUT:1

RESULT:
Thus, the above java program has executed successfully.
38
EX NO: 10 BINARY TREE IMPLEMENTATION

AIM:

ALGORITHM:

39
PROGRAM:1
class Node {
int data;
Node left, right;
public Node(int item) {
data = item;
left = right = null;}}
public class BinaryTree {
Node root;
public BinaryTree() {
root = null;}
private void inorderTraversal(Node node) {
if (node != null) {
inorderTraversal(node.left);
System.out.print(node.data + " ");
inorderTraversal(node.right);}}
public void inorderTraversal() {
inorderTraversal(root);
System.out.println();}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
System.out.println("Inorder Traversal:");
tree.inorderTraversal();}}

40
OUTPUT:1

RESULT:
Thus, the above java program has executed successfully.
41
EX NO: 11 MERGE SORT IMPLEMENTATION

AIM:

ALGORITHM:

42
PROGRAM:1

public class mergesort {


public static void mergeSort(int[] array) {
if (array == null || array.length <= 1)
return;
int[] helper = new int[array.length];
mergeSort(array, helper, 0, array.length - 1);
}
private static void mergeSort(int[] array, int[]
helper, int low, int high) {
if (low < high) {
int middle = (low + high) / 2;
mergeSort(array, helper, low, middle);
mergeSort(array, helper, middle + 1,
high);
merge(array, helper, low, middle,
high);
} }
private static void merge(int[] array, int[]
helper, int low, int middle, int high) {
for (int i = low; i <= high; i++) {
helper[i] = array[i];
}
int i = low;
int j = middle + 1;
int k = low;
while (i <= middle && j <= high) {
if (helper[i] <= helper[j]) {
array[k] = helper[i];
i++;
43
} else {

j++;}
k++;}
while (i <= middle) {
array[k] = helper[i];
k++;
i++;}}
public static void main(String[] args) {
int[] array = {12, 11, 13, 5, 6, 7};
System.out.println("Original array:");
printArray(array);
mergeSort(array);
System.out.println("Sorted array:");
printArray(array);}
private static void printArray(int[] array) {
for (int num : array) {
System.out.print(num + " ");}
System.out.println();
}
}

44
OUTPUT:1

RESULT:
Thus, the above java program has executed successfully.
45
EX NO: 12 RADIX SORT IMPLEMENTATION

AIM:

ALGORITHM:

46
PROGRAM:1
import java.util.Arrays;
public class RadixSort {
public static void radixSort(int[] arr) {
int max =
Arrays.stream(arr).max().orElse(0);
for (int exp = 1; max / exp > 0; exp *=
10) {
countingSort(arr, exp); }}
private static void countingSort(int[] arr, int
exp) {
int n = arr.length;
int[] output = new int[n];
int[] count = new int[10];
Arrays.fill(count, 0);
for (int i = 0; i < n; i++) {
count[(arr[i] / exp) % 10]++;}
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];}
for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1]
= arr[i];
count[(arr[i] / exp) % 10]--;}
System.arraycopy(output, 0, arr, 0, n);}
public static void main(String[] args) {
int[] arr = {170, 45, 75, 90, 802, 24, 2,
66};
radixSort(arr);
System.out.println("Sorted array: " +
Arrays.toString(arr));}}

47
OUTPUT:1

RESULT:
Thus, the above java program has executed successfully.
48

You might also like