0% found this document useful (0 votes)
2 views13 pages

Corrected Java Lab Manual

Uploaded by

wcwmbm9hzz
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)
2 views13 pages

Corrected Java Lab Manual

Uploaded by

wcwmbm9hzz
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

Corrected Java Lab Manual

II [Link] II Semester CSE(DS) - Java Programming Lab

1(a). Method Overloading

File: [Link]

import [Link];

class Demo {
void add(int a, int b) {
[Link]("Sum = " + (a + b));
}
void add(int a, int b, int c) {
[Link]("Sum = " + (a + b + c));
}
}

public class MethodOverloading {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Demo d = new Demo();

[Link]("Enter 2 numbers:");
int a = [Link]();
int b = [Link]();
[Link](a, b);

[Link]("Enter 3 numbers:");
int x = [Link]();
int y = [Link]();
int z = [Link]();
[Link](x, y, z);
[Link]();
}
}
1(b). Constructor Overloading

File: [Link]

import [Link];

class Student {
int id;
Student() {
id = 0;
}
Student(int x) {
id = x;
}
void display() {
[Link]("ID = " + id);
}
}

public class ConstructorOverloading {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Student s1 = new Student();

[Link]("Enter ID:");
int n = [Link]();
Student s2 = new Student(n);

[Link]();
[Link]();
[Link]();
}
}
2. Polymorphism (Dynamic Method Dispatch)

File: [Link]

class Employee {
void salary() {
[Link]("Employee Salary");
}
}

class Manager extends Employee {


void salary() {
[Link]("Manager Salary = 80000");
}
}

class Developer extends Employee {


void salary() {
[Link]("Developer Salary = 60000");
}
}

public class DynamicDispatch {


public static void main(String[] args) {
Employee e;
e = new Manager();
[Link]();

e = new Developer();
[Link]();
}
}
3. Creation of Packages

File: [Link] (Save inside folder 'mypack')

package mypack;
public class Message {
public void display() {
[Link]("Package Created Successfully");
}
}

File: [Link]

import [Link];
public class PackageDemo {
public static void main(String[] args) {
Message m = new Message();
[Link]();
}
}
4. Multiple Catch Blocks

File: [Link]

import [Link];
public class MultipleCatchDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
try {
[Link]("Enter first number:");
int a = [Link]();
[Link]("Enter second number:");
int b = [Link]();
int result = a / b;
[Link]("Result = " + result);

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


[Link]("Array Element: " + arr[5]);
}
catch (ArithmeticException e) {
[Link]("Cannot divide by zero");
}
catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array index is invalid");
}
catch (Exception e) {
[Link]("Some error occurred");
}
[Link]();
}
}
5. User Defined Exception

File: [Link]

import [Link];
class InvalidAgeException extends Exception {
InvalidAgeException(String message) {
super(message);
}
}

public class UserDefinedExceptionDemo {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Age:");
int age = [Link]();
try {
if (age < 18) {
throw new InvalidAgeException("Not Eligible to Vote");
} else {
[Link]("Eligible to Vote");
}
} catch (InvalidAgeException e) {
[Link]([Link]());
}
[Link]();
}
}
6. Multithreading

File: [Link]

class MyThread extends Thread {


public void run() {
for (int i = 1; i <= 5; i++) {
[Link]("Child Thread: " + i);
}
}
}

public class MultithreadingDemo {


public static void main(String[] args) {
MyThread t = new MyThread();
[Link]();
for (int i = 1; i <= 5; i++) {
[Link]("Main Thread: " + i);
}
}
}
7. Thread Synchronization

File: [Link]

class Display {
synchronized void show() {
for (int i = 1; i <= 5; i++) {
[Link]([Link]().getName() + " : " +
i);
}
}
}

class MyThread extends Thread {


Display d;
MyThread(Display d, String name) {
super(name);
this.d = d;
}
public void run() {
[Link]();
}
}

public class SynchronizationDemo {


public static void main(String[] args) {
Display obj = new Display();
MyThread t1 = new MyThread(obj, "Thread-1");
MyThread t2 = new MyThread(obj, "Thread-2");
[Link]();
[Link]();
}
}
8. File Read and Write

File: [Link]

import [Link].*;
public class FileDemo {
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("[Link]");
FileWriter fw = new FileWriter("[Link]");
int ch;
while ((ch = [Link]()) != -1) {
[Link](ch);
}
[Link]();
[Link]();
[Link]("File Copied Successfully");
}
}
9. Keyboard Event Handling

File: [Link]

import [Link].*;
import [Link].*;

public class KeyEventDemo extends Frame implements KeyListener {


Label l;
KeyEventDemo() {
setTitle("Keyboard Event Handling");
setSize(400, 200);
setLayout(null);
l = new Label();
[Link](50, 100, 300, 30);
add(l);
addKeyListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
[Link](0);
}
});
setVisible(true);
}
public void keyPressed(KeyEvent e) {
[Link]("Key Pressed");
}
public void keyReleased(KeyEvent e) {
[Link]("Key Released");
}
public void keyTyped(KeyEvent e) {
[Link]("Key Typed: " + [Link]());
}
public static void main(String[] args) {
new KeyEventDemo();
}
}
10. Mouse Event Handling

File: [Link]

import [Link].*;
import [Link].*;

public class MouseEventDemo extends Frame implements MouseListener {


Label l;
MouseEventDemo() {
setTitle("Mouse Event Handling");
setSize(400, 250);
setLayout(null);
l = new Label("Move or click mouse inside");
[Link](50, 100, 300, 30);
add(l);
addMouseListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
[Link](0);
}
});
setVisible(true);
}
public void mouseClicked(MouseEvent e) { [Link]("Mouse Clicked"); }
public void mousePressed(MouseEvent e) { [Link]("Mouse Pressed"); }
public void mouseReleased(MouseEvent e) { [Link]("Mouse
Released"); }
public void mouseEntered(MouseEvent e) { [Link]("Mouse Entered"); }
public void mouseExited(MouseEvent e) { [Link]("Mouse Exited"); }

public static void main(String[] args) {


new MouseEventDemo();
}
}
11. StringTokenizer

File: [Link]

import [Link];
import [Link];

public class StringTokenizerDemo {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence:");
String str = [Link]();
StringTokenizer st = new StringTokenizer(str);
[Link]("Number of words = " + [Link]());
while ([Link]()) {
[Link]([Link]());
}
[Link]();
}
}
12. Thread Priorities

File: [Link]

class PriorityThread extends Thread {


PriorityThread(String name) {
super(name);
}
public void run() {
for (int i = 1; i <= 5; i++) {
[Link](getName() + " : " + i);
}
}
}

public class ThreadPriorityDemo {


public static void main(String[] args) {
PriorityThread t1 = new PriorityThread("Low Priority Thread");
PriorityThread t2 = new PriorityThread("High Priority Thread");
[Link](Thread.MIN_PRIORITY);
[Link](Thread.MAX_PRIORITY);
[Link]();
[Link]();
}
}

You might also like