Oops
Oops
AIM:
ALGORITHM:
1. Start the program.
2. To create sequential search algorithm.
3. Searching the values and solve the problems.
4. To create binary values.
5. To create quadratic sorting and searching algorithm.
6. To included solve the problems (Selection and Insertion).
7. Stop the program.
PROGRAM:
SEQUENTIAL SEARCH
ob.sort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}
INSERTION SORT
public class InsertionSort {
public static void insertionSort(int array[]) {
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = key;
}}
public static void main(String a[]){
int[] arr1 = {9,14,3,2,43,11,58,22};
System.out.println("Before Insertion Sort");
for(int i:arr1){
System.out.print(i+" ");
}
System.out.println();
insertionSort(arr1);//sorting array using insertion sort
System.out.println("After Insertion Sort");
for(int i:arr1){
System.out.print(i+" ");
}
}
}
OUTPUT:
SELECTION SORT:
SEQUENTIAL SEARCH:
BINARY SEARCH:
OUTPUT:
RESULT:
Thus the program using sequential search, binary search, and quadratic
sorting algorithms was executed successfully.
EX.NO: 2 DEVELOP STACK AND QUEUE DATA STRUCTURES
USING CLASSES AND OBJECTS
AIM:
To develop stack and queue data structures using classes and objects.
ALGORITHM:
1. Start the program.
2. An array is a random access data.
3. Stack using classes and objects.
4. Queue using also classes and objects.
5. End the program.
PROGRAM:
STACK:
public class Stack {
// store elements of stack
private int arr[];
// represent top of stack
private int top;
// total capacity of the stack
private int capacity;
// Creating a stack
Stack(int size) {
// initialize the array
// initialize the stack variables
arr = new int[size];
capacity = size;
top = -1;
}
// if stack is empty
// no element to pop
if (isEmpty()) {
System.out.println("STACK EMPTY");
// terminates the program
System.exit(1);
}
// pop element from top of stack
return arr[top--];
}
// return size of the stack
public int getSize() {
return top + 1;
}
// check if the stack is empty
public Boolean isEmpty() {
return top == -1;
}
// check if the stack is full
public Boolean isFull() {
return top == capacity - 1;
}
// display elements of stack
public void printStack() {
for (int i = 0; i <= top; i++) {
System.out.print(arr[i] + ", ");
}
}
public static void main(String[] args) {
Stack stack = new Stack(5);
stack.push(1);
stack.push(2);
stack.push(3);
System.out.print("Stack: ");
stack.printStack();
// remove element from stack
stack.pop();
System.out.println("\nAfter popping out");
stack.printStack();}}
QUEUE
public class Queue {
int SIZE = 5;
int items[] = new int[SIZE];
int front, rear;
Queue() {
front = -1;
rear = -1;
}
// if queue is full
if (isFull()) {
System.out.println("Queue is full");
}
else {
if (front == -1) {
// mark front denote first element of queue
front = 0;
}
rear++;
// insert element at the rear
items[rear] = element;
System.out.println("Insert " + element);
}
}
// delete element from the queue
int deQueue() {
int element;
// if queue is empty
if (isEmpty()) {
System.out.println("Queue is empty");
return (-1);
}
else {
// remove element from the front of queue
element = items[front];
OUTPUT:
RESULT:
AIM:
To develop a Java application with employee class and generate pay slips for
the employees with their gross and net salary.
ALGORITHM:
PROGRAM:
class Person {
String name;
int age;
OUTPUT:
RESULT:
Thus the Java application has been created with employee class and pay
slips are generated for the employees with their gross and net salary.
EX.NO:4 CALCULATE AREA OF RECTANGLE, TRIANGLE
AND CIRCLE
AIM:
To Write a java program to create an abstract class named Shape that
contains two integers and an empty method named print Area().Each one of the
classes contain only the method print Area( ) that prints the area of the given shape.
ALGORITHMS:
1. Start the program
2. To create an abstract class name.
3. That contain two integers and an empty method named print Area().
4. To provide three classes named Rectangle, Triangle and Circle.
5. Stop the program.
PROGRAM:
class Rectangle extends Shape {
void printArea() {
System.out.println("*** Finding the Area of Rectangle ***");
System.out.print("Enter length and breadth: ");
length = input.nextInt();
breadth = input.nextInt();
System.out.println("The area of Rectangle is: " + length * breadth);
}
}
OUTPUT:
RESULT:
Thus the java program to create an abstract class named Shape that contains two
Integer sand an empty method named print Area() executed successfully.
EX.NO:5 INTERFACE
AIM:
To solve the above problem using an interface.
ALGORITHM:
1. Start the program.
2. Interface are used to implement abstraction.
3. It is used to achieve total abstraction.
4. Interface are declared by specifying a keyword “interface”.
5. Stop the program.
PROGRAM:
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void sleep(); // interface method (does not have a body)
}
RESULT:
AIM:
ALGORITHM:
PROGRAM:
class JavaException{
public static void main(String args[])
{
Try
{
throw new MyException(2); // throw is used to create a new exception and throw it.
}
catch(MyException e)
{
System.out.println(e);
}
}
}
class MyException extends Exception{
int a;
MyException(int b)
{
a=b;
}
public String toString()
{
return("Exception Number = "+a);
}
}
OUTPUT:
RESULT:
AIM:
To write a java program that implements a multi-thread application that has three
threads.
ALGORITHMS:
1. Start the program
2. Design the first thread that generates a random integer for every 1 second.
3. If the first thread value is even, design the second thread as the square of the number
and then print it.
4. If the first thread value is odd, then third thread will print the value of cube of the
number.
5. Stop the program.
PROGRAM:
import java.util.Random;
class Square extends Thread {
int x;
Square(int n) {
x = n;
}
public void run() {
int sqr = x * x;
System.out.println("Square of " + x + " = " + sqr);
}
}
class Cube extends Thread {
int x;
Cube(int n) {
x = n;
}
public void run() {
int cub = x * x * x;
System.out.println("Cube of " + x + " = " + cub);
}
}
class Rnumber extends Thread {
public void run() {
Random random = new Random();
for (int i = 0; i < 5; i++) {
int randomInteger = random.nextInt(10);
System.out.println("Random Integer generated : " + randomInteger);
Square s = new Square(randomInteger);
s.start();
Cube c = new Cube(randomInteger);
c.start();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
}
}
public class ThreadP {
public static void main(String[] args) {
Rnumber n = new Rnumber();
n.start();
}
}
RESULT:
Thus the java program that implements a multi-thread application that has
three threads was successfully.
EX.NO:8 READ AND WRITE OPERATIONS
AIM:
ALGORITHM:
1. Start the program.
2. To write data into the file.
3. To read the data from the file.
4. To write a character into the file.
5. Open new file or existing file
6. Stop the program.
PROGRAM:
import java.io.File;
// Importing the IOException class for handling errors
import java.io.IOException;
class CreateFile {
public static void main(String args[]) {
try {
// Creating an object of a file
File f0 = new File("D:FileOperationExample.txt");
if (f0.createNewFile()) {
System.out.println("File " + f0.getName() + " is created successfully.");
} else {
System.out.println("File is already exist in the directory.");
}} catch (IOException exception) {
System.out.println("An unexpected error is occurred.");
exception.printStackTrace();
} }}
OUTPUT:
RESULT:
AIM:
ALGORITHM:
1. Start the program.
2. To write and develop the program.
3. Open the application files.
4. Run the program.
5. Stop the program.
PROGRAM:
class Main {
public static void main(String[] args) {
// initialize generic class
// with Integer data
GenericsClass<Integer> intObj = new GenericsClass<>(5);
System.out.println("Generic Class returns: " + intObj.getData());
// initialize generic class
// with String data
GenericsClass<String> stringObj = new GenericsClass<>("Java Programming");
System.out.println("Generic Class returns: " + stringObj.getData());
}
}
// create a generics class
class GenericsClass<T> {
// variable of T type
private T data;
public GenericsClass(T data) {
this.data = data;
}
// method that return T type variable
public T getData() {
return this.data;
}
}
OUTPUT
RESULT:
AIM:
To develop applications using JavaFX controls, layouts and menus.
ALGORITHM:
1. Start the program.
2. To write the program.
3. Using the java FX controls.
4. To run the program.
5. Stop the program.
PROGRAM:
import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import static javax.swing.LayoutStyle.ComponentPlacement.UNRELATED;
public class PasswordEx extends JFrame {
private JTextField loginField;
private JPasswordField passField;
public PasswordEx() {
initUI();
}
private void initUI() {
var lbl1 = new JLabel("Login");
var lbl2 = new JLabel("Password");
loginField = new JTextField(15);
passField = new JPasswordField(15);
var submitButton = new JButton("Submit");
submitButton.addActionListener(new SubmitAction());
createLayout(lbl1, loginField, lbl2, passField, submitButton);
setTitle("Login");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class SubmitAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
doSubmitAction();
}
private void doSubmitAction() {
var login = loginField.getText();
var passwd = passField.getPassword();
if (!login.isEmpty() && passwd.length != 0) {
System.out.format("User %s entered %s password%n", login, String.valueOf(passwd));
}
Arrays.fill(passwd, '0');
}
}
private void createLayout(Component... arg) {
var pane = getContentPane();
var gl = new GroupLayout(pane);
pane.setLayout(gl);
gl.setAutoCreateGaps(true);
gl.setAutoCreateContainerGaps(true);
gl.setHorizontalGroup(gl.createSequentialGroup()
.addGap(50)
.addGroup(gl.createParallelGroup()
.addComponent(arg[0])
.addComponent(arg[1])
.addComponent(arg[2])
.addComponent(arg[3])
.addComponent(arg[4]))
.addGap(50)
);
gl.setVerticalGroup(gl.createSequentialGroup()
.addGap(50)
.addGroup(gl.createSequentialGroup()
.addComponent(arg[0])
.addComponent(arg[1], GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(arg[2])
.addComponent(arg[3], GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(UNRELATED)
.addComponent(arg[4]))
.addGap(50));
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
var ex = new PasswordEx();
ex.setVisible(true);
});
}
}
OUTPUT:
RESULT:
Thus the develop application using JAVAFX controls, layouts and menus was
executed successfully.