0% found this document useful (0 votes)
14 views26 pages

Java Search and Employee Classes

The document contains various Java code snippets demonstrating different programming concepts such as sequential search, stack operations, employee management using inheritance, exception handling, method overloading, and JavaFX for GUI applications. It includes examples of sorting algorithms, user-defined exceptions, and the implementation of interfaces for a calculator. The code also showcases the use of AWT and Swing for creating simple GUI applications.

Uploaded by

safe814866
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views26 pages

Java Search and Employee Classes

The document contains various Java code snippets demonstrating different programming concepts such as sequential search, stack operations, employee management using inheritance, exception handling, method overloading, and JavaFX for GUI applications. It includes examples of sorting algorithms, user-defined exceptions, and the implementation of interfaces for a calculator. The code also showcases the use of AWT and Swing for creating simple GUI applications.

Uploaded by

safe814866
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Public class SequentialSearch {

// Sequential search method

Public static int sequentialSearch(int[] arr, int target) {

// Iterate through each element of the array

For (int I = 0; I < [Link]; i++) {

// If the target element is found, return its index

If (arr[i] == target) {

Return I;

// If the target element is not found, return -1

Return -1;

Public static void main(String[] args) {

Int[] arr = { 10, 25, 5, 15, 30, 20 };

Int targetElement = 15;

Int result = sequentialSearch(arr, targetElement);

If (result != -1) {

[Link](“Element found at index: “ + result);

} else {

[Link](“Element not found in the array.”);

Import [Link];

1,2

Class Main {

Public static void main(String[] args) {

Stack<String> animals= new Stack<>();


// Add elements to Stack

[Link](“Dog”);

[Link](“Horse”);

[Link](“Cat”);

[Link](“Stack: “ + animals);

// Search an element

Int position = [Link](“Horse”);

[Link](“Position of Horse: “ + position);

2 ,1 Import [Link];

Class Employee{

String Emp_name;

Int Emp_id;

String Address;

String Mail_id;

Int Mobile_no;

Void display(){

[Link](Emp_name);

//[Link](Address);

[Link](Emp_id);

[Link](Mail_id);

[Link](Mobile_no);

Class Programmer extends Employee{

Int BP;
/*int DA= (int) (0.97*BP);

HRA=(int) (0.10*BP);

PF=(int) (0.12*BP); */

Void display(){

[Link](BP);

[Link](“DA”+0.97*BP);

[Link](“HRA”+0.10*BP);

[Link](“PF”+0.12*BP);

[Link](“SATFF CLUD FUND”+0.001*BP);

Class Assistant_Professor extends Employee{

Int BP;

Void display(){

[Link](BP);

[Link](“DA”+0.97*BP);

[Link](“HRA”+0.10*BP);

[Link](“PF”+0.12*BP);

[Link](“SATFF CLUD FUND”+0.001*BP);

Class Associate_Professor extends Employee{

Int BP;

Void display(){
[Link](BP);

[Link](“DA”+0.97*BP);

[Link](“HRA”+0.10*BP);

[Link](“PF”+0.12*BP);

[Link](“SATFF CLUD FUND”+0.001*BP);

Class Professor extends Employee{

Int BP;

Void display(){

[Link](BP);

[Link](“DA”+0.97*BP);

[Link](“HRA”+0.10*BP);

[Link](“PF”+0.12*BP);

[Link](“SATFF CLUD FUND”+0.001*BP);

Class Main{

Public static void main(String args[]){

[Link](“\n [Link]\n2.Assistant_Professor\n3.Associate_Professor\[Link]”);

Scanner input=new Scanner([Link]);

[Link](“Enter an integer: “);

Int ch=[Link]();

Switch (ch) {

Case 1:

Employee e1=new Employee();


Programmer p1=new Programmer();

E1.Emp_name=”ABC”;

[Link]=”y-city”;

E1.Mail_id=praw@[Link];

E1.Emp_id=567;

E1.Mobile_no=2345678;

[Link]=15000;

[Link]();

[Link]();

Break;

Case 2:

Employee e2=new Employee();

Assistant_Professor p2=new Assistant_Professor();

E2.Emp_name=”DEF”;

[Link]=”A-city”;

E2.Mail_id=RAJAN@[Link];

E2.Emp_id=123;

E2.Mobile_no=987321;

[Link]=30000;

[Link]();

[Link]();

Break;

Case 3:

Employee e3=new Employee();

Associate_Professor p3=new Associate_Professor();

E3.Emp_name=”GHF”;

[Link]=”B-city”;

E3.Mail_id=MAIN@[Link];

E3.Emp_id=456;
E3.Mobile_no=98710;

[Link]=30000;

[Link]();

[Link]();

Break;

Case 4:

Employee e4=new Employee();

Professor p4=new Professor();

E4.Emp_name=”KANNAN”;

[Link]=”TRICHY”;

E4.Mail_id=kanna@[Link];

E4.Emp_id=789;

E4.Mobile_no=9810;

[Link]=30000;

[Link]();

[Link]();

Break;

Case 5:

//exit(1);

Default:

[Link](“enter correct choice”);

2,2

Import [Link];
Class Test {

Int n, I, p = 1;

Scanner sc = new Scanner([Link]);

Test() {

[Link](“Enter a number:”);

N = [Link]();

Void check() {

If (n % 2 == 0) {

[Link](“Number is even :” + n);

} else {

[Link](“Number is odd :” + n);

Class Main {

Public static void main(String args[]) {

Test obj = new Test();

[Link]();
}

// A Class that represents use-defined exception

Class MyException extends Exception {

// A Class that uses above MyException

Public class setText {

// Driver Program

Public static void main(String args[])

Try {

// Throw an object of user defined exception

Throw new MyException();

Catch (MyException ex) {


[Link](“Caught”);

[Link]([Link]());

8,2

Class Overload_Demo implements IterF1

Public void add()

[Link](“Addition of “+a+” and “+b+” is: “+(a+b));

Public void sub()

[Link](“Subtraction of “+a+” and “+b+” is: “+(a-b));

Public static void main(String[] args)

Overload_Demo obj = new Overload_Demo();

[Link]();

[Link]();

9.2

Java program to demonstrate

// the multiple inheritance


// in interface

// Interface to implement the

// addition and subtraction methods

Interface Add_Sub {

Public void add(double x, double y);

Public void subtract(double x, double y);

// Interface to implement the

// multiplication and division

Interface Mul_Div {

Public void multiply(double x, double y);

Public void divide(double x, double y);

// Calculator interface which extends

// both the above defined interfaces

Interface Calculator extends Add_Sub, Mul_Div {


Public void printResult(double result);

// Calculator class which

// implements the above

// interface

Public class MyCalculator implements Calculator {

// Implementing the addition

// method

Public void add(double x, double y)

Double result = x + y;

printResult(result);

}
// Implementing the subtraction

// method

Public void subtract(double x, double y)

Double result = x – y;

printResult(result);

// Implementing the multiplication

// method

Public void multiply(double x, double y)

Double result = x * y;

printResult(result);

}
// Implementing the division

// method

Public void divide(double x, double y)

Double result = x / y;

printResult(result);

// Implementing a method

// to print the result

Public void printResult(double result)

[Link](
“The result is : “ + result);

// Driver code

Public static void main(String args[])

// Creating the object of

// the calculator

MyCalculator c = new MyCalculator();

[Link](5, 10);

[Link](35, 15);

[Link](6, 9);

[Link](45, 6);

}
}

12,1

Public class MaxValueFinder {

Public static <T extends Comparable<T>> T findMax(T[] arr) {

If (arr == null || [Link] == 0) {

Return null; // return null if array is empty

T max = arr[0];

For (int I = 1; I < [Link]; i++) {

If (arr[i].compareTo(max) > 0) {

Max = arr[i];

Return max;

Public static void main(String[] args) {

Integer[] intArray = {3, 7, 2, 10, 5};

Double[] doubleArray = {3.5, 8.9, 1.2, 6.7, 4.0};

String[] stringArray = {“apple”, “orange”, “banana”, “grape”};

[Link](“Maximum value in integer array: “ + findMax(intArray));

[Link](“Maximum value in double array: “ + findMax(doubleArray));

[Link](“Maximum value in string array: “ + findMax(stringArray));

12,2
Public class BubbleSort {

Public static void bubbleSort(int[] arr) {

Int n = [Link];

For (int I = 0; I < n – 1; i++) {

For (int j = 0; j < n – I – 1; j++) {

If (arr[j] > arr[j + 1]) {

// Swap arr[j] and arr[j+1]

Int temp = arr[j];

Arr[j] = arr[j + 1];

Arr[j + 1] = temp;

Public static void main(String[] args) {

Int[] arr = {64, 34, 25, 12, 22, 11, 90};

[Link](“Array before sorting:”);

printArray(arr);

bubbleSort(arr);

[Link](“\nArray after sorting:”);

printArray(arr);

Public static void printArray(int[] arr) {

Int n = [Link];
For (int I = 0; I < n; i++) {

[Link](arr[i] + “ “);

[Link]();

```

13

Import [Link];

Import [Link];

Import [Link];

Import [Link];

Import [Link];

Import [Link];

Import [Link];

Import [Link];

Import [Link];

Import [Link];

Public class JavaFXControlsDemo extends Application {

@Override

Public void start(Stage primaryStage) {

// Create a TextField for user input

TextField textField = new TextField();

[Link](“Enter item”);

// Create a Button to add items to the list

Button addButton = new Button(“Add”);


[Link](event -> {

String newItem = [Link]().trim();

If (![Link]()) {

[Link]().add(newItem);

[Link]();

});

// Create a ListView to display the items

ListView<String> listView = new ListView<>();

ObservableList<String> items = [Link]();

[Link](items);

// Create a VBox to layout the controls

VBox root = new VBox(10);

[Link](new Insets(10));

[Link]().addAll(textField, addButton, listView);

// Create the scene and set it to the stage

Scene scene = new Scene(root, 300, 200);

[Link](scene);

[Link](“JavaFX Controls Demo”);

[Link]();

Public static void main(String[] args) {

Launch(args);

}
14

Import [Link];

Import [Link];

Import [Link];

Import [Link];

Import [Link];

Import [Link];

Import [Link];

Import [Link];

Import [Link];

Import [Link];

Public class JavaFXLayoutsDemo extends Application {

@Override

Public void start(Stage primaryStage) {

// Create a BorderPane layout

BorderPane borderPane = new BorderPane();

[Link](new Insets(10));

// Create an HBox layout for the top region

HBox topHBox = new HBox(10);

Label titleLabel = new Label(“JavaFX Layouts Demo”);

[Link]().add(titleLabel);

[Link](topHBox);

// Create a VBox layout for the center region

VBox centerVBox = new VBox(10);


Label nameLabel = new Label(“Name:”);

TextField nameTextField = new TextField();

[Link]().addAll(nameLabel, nameTextField);

[Link](centerVBox);

// Create an HBox layout for the bottom region

HBox bottomHBox = new HBox(10);

Button submitButton = new Button(“Submit”);

[Link]().add(submitButton);

[Link](bottomHBox);

// Create the scene and set it to the stage

Scene scene = new Scene(borderPane, 300, 200);

[Link](scene);

[Link](“JavaFX Layouts Demo”);

[Link]();

Public static void main(String[] args) {

Launch(args);

15

Import [Link];

Import [Link];

Import [Link];

Import [Link];

Import [Link];

Import [Link];
Import [Link];

Import [Link];

Public class JavaFXMenuDemo extends Application {

@Override

Public void start(Stage primaryStage) {

BorderPane root = new BorderPane();

// Create MenuBar

MenuBar menuBar = new MenuBar();

// Create File menu

Menu fileMenu = new Menu(“File”);

MenuItem openItem = new MenuItem(“Open”);

[Link](event -> {

// Handle open action

showAlert(“Open”, “Open file dialog”);

});

MenuItem exitItem = new MenuItem(“Exit”);

[Link](event -> [Link]());

[Link]().addAll(openItem, exitItem);

// Create Help menu

Menu helpMenu = new Menu(“Help”);

MenuItem aboutItem = new MenuItem(“About”);

[Link](event -> {

// Handle about action

showAlert(“About”, “JavaFX Menu Demo\nVersion 1.0”);


});

[Link]().add(aboutItem);

// Add menus to menu bar

[Link]().addAll(fileMenu, helpMenu);

// Set menu bar at the top of the BorderPane

[Link](menuBar);

Scene scene = new Scene(root, 400, 300);

[Link](scene);

[Link](“JavaFX Menu Demo”);

[Link]();

Private void showAlert(String title, String message) {

Alert alert = new Alert([Link]);

[Link](title);

[Link](null);

[Link](message);

[Link]();

Public static void main(String[] args) {

Launch(args);

```

16
Import [Link].*;

Import [Link].*;

Import [Link];

Import [Link];

Public class SimpleAWTAndSwingApp {

Public static void main(String[] args) {

// Create a frame (AWT)

Frame frame = new Frame(“Simple AWT and Swing App”);

// Create a button (Swing)

JButton button = new JButton(“Click Me!”);

// Add ActionListener to the button

[Link](new ActionListener() {

@Override

Public void actionPerformed(ActionEvent e) {

// Show a dialog box when the button is clicked

[Link](frame, “Hello, Swing!”);

});

// Add the button to the frame (AWT)

[Link](button, [Link]);

// Set frame properties

[Link](300, 200);

[Link](true);

}
}

18

Import [Link];

Public class SortingAlgorithms {

// Selection Sort

Public static void selectionSort(int[] arr) {

Int n = [Link];

For (int I = 0; I < n – 1; i++) {

Int minIndex = I;

For (int j = I + 1; j < n; j++) {

If (arr[j] < arr[minIndex]) {

minIndex = j;

// Swap the found minimum element with the first element

Int temp = arr[minIndex];

Arr[minIndex] = arr[i];

Arr[i] = temp;

// Merge Sort

Public static void mergeSort(int[] arr) {

If ([Link] <= 1) {

Return;

}
Int mid = [Link] / 2;

Int[] left = [Link](arr, 0, mid);

Int[] right = [Link](arr, mid, [Link]);

mergeSort(left);

mergeSort(right);

merge(left, right, arr);

Private static void merge(int[] left, int[] right, int[] arr) {

Int leftLength = [Link];

Int rightLength = [Link];

Int I = 0, j = 0, k = 0;

While (I < leftLength && j < rightLength) {

If (left[i] <= right[j]) {

Arr[k++] = left[i++];

} else {

Arr[k++] = right[j++];

While (I < leftLength) {

Arr[k++] = left[i++];

While (j < rightLength) {


Arr[k++] = right[j++];

Public static void main(String[] args) {

Int[] arr = {5, 3, 8, 1, 2, 7, 4};

[Link](“Array before sorting: “ + [Link](arr));

// Sorting using Selection Sort

selectionSort([Link]());

[Link](“Array after Selection Sort: “ + [Link](arr));

// Sorting using Merge Sort

mergeSort([Link]());

[Link](“Array after Merge Sort: “ + [Link](arr));

You might also like