ASSIGNMENT - 02
[Prog-111 : Object Oriented Programming Language]
[Sadman Sami]
[2301033]
Topic 01 : Exception Handling in JAVA
No matter how smart we are, errors are our constant companion. With practice, we
keep get better at finding and correcting them. There are three types of errors. Such
as :
1. Syntax Error
2. Logical Error
3. Runtime Error
Runtime errors are called Exception. An exception is a runtime problem that occurs
during program execution and interrupts the normal flow of a program.
Example problems:
1. Dividing by zero
2. Invalid input
3. File not found
4. Null value access etc
There are two types of Exception, such as :
Checked Exception - Checked at compile time (Exc : IOException)
Unchecked Exception - Occurs at runtime (Exc : ArithMaticException)
And finally there are multiple keywords used in Exception Handling. They are :
1. Try – When we write risky code
2. Catch – To Handle Errors
3. Finally – Code that always executes
4. Throw – Manually throw exception
5. Throws – Declare Exception
Code :
class Exception {
public static void main(String[] args) {
try {
int a = 33, b = 0;
int result = a / b;
[Link]("Result: " + result);
catch (ArithmeticException e) {
[Link]("Error: Division by zero is not allowed");
finally {
[Link]("Program Ended Safely");
Topic 02 : Threading in JAVA
A Thread is the smallest unit of execution within a program. A Java
program normally runs in one thread, called the main thread. When we
create multiple threads, the program becomes multithreaded, meaning
Multiple tasks are executed simultaneously inside a single process.
Why we need Multithreading ?
1. Better CPU utilization - Download & processing at same time
2. Faster program execution - Games, animations
3. Responsive applications - GUI should not freeze
4. Background processing - Auto-save, notifications
Life Cycle of a Thread :
1. New - Thread created but not started
2. Runnable - Thread is ready to run
3. Running - CPU executing thread
4. Blocked / Waiting - Thread paused for resource
5. Terminated - Thread finished execution
Thread Priority :
1. MIN_PRIORITY - 1
2. NORM_PRIORITY - 5
3. MAX_PRIORITY - 10
Code :
class Task extends Thread {
public void run() {
[Link]("Running Thread: " +
[Link]().getName());
}
}
class PriorityDemo {
public static void main(String args[]) {
Task t1 = new Task();
Task t2 = new Task();
[Link]("Low Priority");
[Link]("High Priority");
[Link](2);
[Link](8);
[Link]();
[Link]();
}
}
(and with Thread Sleep Cycle)
class SleepDemo extends Thread {
public void run() {
for(int i=1; i<=5; i++) {
[Link](i);
try {
[Link](1000);
} catch(Exception e) {}
}
}
public static void main(String args[]) {
SleepDemo t = new SleepDemo();
[Link]();
}
}
(To avoid Deadlock)
class Table {
synchronized void print(int n) {
for(int i=1;i<=5;i++) {
[Link](n*i);
try { [Link](400); } catch(Exception e) {}
}
}
}
class MyThread1 extends Thread {
Table t;
MyThread1(Table t){ this.t=t; }
public void run(){ [Link](5); }
}
class MyThread2 extends Thread {
Table t;
MyThread2(Table t){ this.t=t; }
public void run(){ [Link](10); }
}
class SyncDemo {
public static void main(String args[]) {
Table obj = new Table();
new MyThread1(obj).start();
new MyThread2(obj).start();
}
}
Topic 03 : AWT (Abstract Window Toolkit)
AWT is Java’s GUI toolkit used to build desktop applications with
buttons, text fields, labels etc.
Common AWT Components
• Frame - Window
• Label - Display text
• TextField - Input
• Button - Clickable button
• ActionListener - Handle events
(Code)
import [Link].*;
import [Link].*;
class LoginAWT {
public static void main(String args[]) {
Frame f = new Frame("Login Form");
Label l1 = new Label("Username:");
Label l2 = new Label("Password:");
TextField t1 = new TextField();
TextField t2 = new TextField();
[Link]('*');
Button b = new Button("Login");
Label result = new Label();
[Link](50, 60, 80, 30);
[Link](150, 60, 150, 30);
[Link](50, 110, 80, 30);
[Link](150, 110, 150, 30);
[Link](150, 160, 80, 30);
[Link](100, 200, 200, 30);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
if([Link]().equals("admin") && [Link]().equals("1234"))
[Link]("Login Successful");
else
[Link]("Login Failed");
}
});
[Link](l1); [Link](t1);
[Link](l2); [Link](t2);
[Link](b); [Link](result);
[Link](400,300);
[Link](null);
[Link](true);
}
}
In output, a login window opens. If UserName = admin & Password is
123456, then its visible