[go: up one dir, main page]

0% found this document useful (0 votes)
23 views30 pages

UNIT THREE Java

3 java

Uploaded by

gunjan Sathawane
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)
23 views30 pages

UNIT THREE Java

3 java

Uploaded by

gunjan Sathawane
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
You are on page 1/ 30

Join Our Telegram Community - Aspire2Learn

“We are here to provide you with simplified


notes for BCA and to help you throughout
your BCA journey...
Team, Aspire2Learn”

Join Our Telegram Community - Aspire2Learn


PROGRAMMING IN JAVA
SYLLABUS
UNIT - I :
Introduction to Java: -History of Java, features of Java, getting started with Java.
Java programs:-Introduction of Application & Applets. Variables: -Variable
naming,
variable initialization, assign values, Rules of variables, Scope of variable.
Operators:
Arithmetic, Assignment, Unary, Comparison, Shift, Bit- Wise, Logical, Conditional,
New, Special, Relational. Data types:-Integers, Char, String, Float etc. Typecasting:
Tokens: -Java tokens Order of precedence of operators Streams: - Input and
output.

UNIT - II :
Creating a class & subclass: -Declaring a class, Naming class, Rules to assign Class
&
Subclass, Creating a new object, Class of an object. Data members: -Declaring
data
member, Naming variables, using class members. Methods: -Using data members,
Invoke a method, passing arguments to a method, calling method. Access
Specifier &
Modifiers: -Public, Private, Protected, Static & Final. Overloading: -Method
overloading, Constructor overloading. Java class library: - Different types of
classes.
Decision making & loops:-If-then-else, Switch,? : operator, While-loop, do-while
loop,
for. Array: -Creating an array, one-dimensional array, two-dimensional array.
String:
String array, string methods. Inheritance: -Single & multiple inheritances
Interfaces:
Defining interfaces, extending interfaces, implementing interfaces.

UNIT - III :
Join Our Telegram Community - Aspire2Learn
Packages: -Java API packages, creating packages, accessing packages, adding a
class to
packages. Import statement: - Introduction & implementation of import
statement.
Applets:-Introduction to Applets & Application, how applets application are
different
creating An applet. Applets life cycle, designing a web page, creating an
executable
applet, running the applet, applet tags, passing a parameter to applet, HTML tag,
Converting applet to application. Threads:-Overview of threads, single & multiple
threads, lift cycle of threads, stopping & blocking threads, working with threads,
priority
to thread, synchronization. Exceptions & Errors:-Introduction, types of error,
exception,
syntax of exception, handling techniques, exception for Debugging.

UNIT - IV :
Event: -Event driven programming, handling an (AWT) events. Graphic class:
Introduction, the graphic classes, drawing & filling of lines, rectangle, circle &
ellipse,
arcs, polygons, text & fonts, creating a font class, font objects, text, coloring
object.
Streams:-Introduction, Abstract stream classes, file input & output.
AWI Applications: -Creating a GUI using AWT toolkit, using component class,
frames.
Components & Control: -Textfield, textarea class, label, button, choice, list,
checkbox,
class, and combo. Menus: -Creating a popup menus. Image: - Type of image,
Properties
of an image, Displaying an image. Layouts: -Using Window Listener interface,
Different
types of Layout, Layout manager, Flow manager, Grid manager. Container: -
Different
types of container (Frame, Dialog, Panel).

Join Our Telegram Community - Aspire2Learn


UNIT THREE –
------------------------------------------------------------------------------
1. What is thread ? Description the life cycle of thread.
S[2024,2019,2018].
OR
2. What is thread ? Explain thread life cycle. S[2023].
Ans –
Definition:
 A thread in Java is the smallest unit of a process that
executes independently. It allows a program to perform
multiple tasks concurrently, improving performance and
efficiency.

Key Features of Threads in Java:


 Lightweight Process: Runs independently but shares
memory with other threads.

 Multitasking: Helps in executing multiple operations


simultaneously.

 Concurrent Execution: Reduces execution time by


handling multiple tasks in parallel.

 Life Cycle of a Thread: Includes New → Runnable →


Running → Blocked → Terminated states.
Join Our Telegram Community - Aspire2Learn
Real-World Example of Threads
 Downloading multiple files at the same time.

 Processing user requests in a web server.

 Playing a video while buffering it.

Lifecycle of a Thread in Java


 The lifecycle of a thread in Java is defined by several
states that a thread can be in during its execution.
Understanding these states helps in managing thread
behavior and resource allocation effectively.

 The main states in the lifecycle of a thread are New,


Runnable, Blocked, Waiting, Timed Waiting, and
Terminated.

1. New State –
 Description: A thread is in the new state when it is
created but not yet started. At this point, the thread
object is instantiated but has not begun execution.

 How to Enter: A thread enters this state when an


instance of the Thread class is created.

 Example:
Join Our Telegram Community - Aspire2Learn
2. Runnable State
 Description: In this state, the thread is ready to run and
waiting for CPU time. It can be in this state either
because it has just been started or it has been waiting for
its turn to execute.

 How to Enter: A thread enters this state after calling the


start() method or when it transitions from the running
state after being paused or preempted.

 Example:

3. Running State
 Description: The thread is actively executing its task.
Only one thread can be in this state at any given time per
processor core.

Join Our Telegram Community - Aspire2Learn


 How to Enter: A thread enters the running state from
the runnable state when the CPU scheduler picks it for
execution.

 Note: The actual running state may not always be visible,


as it is managed by the JVM and the operating system.

4. Blocked State
 Description: A thread enters the blocked state when it
tries to access a synchronized block or method that is
currently held by another thread. It cannot proceed until
the lock becomes available.

 How to Enter: This occurs when a thread attempts to


acquire a lock that another thread holds.

 Example:

5. Waiting State -
Join Our Telegram Community - Aspire2Learn
 Description: A thread enters the waiting state when it is
waiting indefinitely for another thread to perform a
particular action (like notifying or interrupting).

 How to Enter: This can happen when:

 The wait() method is called on an object.

 The join() method is called on another thread.

 The LockSupport.park() method is used.

 Example:

6. Timed Waiting State -


 Description: This state is similar to waiting, but the
thread will wait for a specified period before
automatically waking up.

 How to Enter: A thread enters this state when:


Join Our Telegram Community - Aspire2Learn
 The sleep(milliseconds) method is called.

 The wait(milliseconds) method is called.

 The join(milliseconds) method is called.

 The LockSupport.parkNanos() or parkUntil() methods


are used.

 Example:

7. Terminated State -
 Description: A thread enters this state when it has
completed its execution, either successfully or due to an
exception. Once a thread reaches this state, it cannot be
restarted.

 How to Enter: This occurs when the run() method


finishes executing or if an unhandled exception occurs
within the thread.
Join Our Telegram Community - Aspire2Learn
 Example:

------------------------------------------------------------------------------
3. What do you mean by package ? How can we create our
own package. S[2024],W[2018,2017].
OR
4. What is package ? How to create a user defined package?
Explain. S[2023,2019],W[2019].
Ans –
Definition -
1. A package in Java is a way to organize related classes and
interfaces into a group to avoid name conflicts and
enhance reusability. It acts like a folder that stores Java
classes.

2. Types of Packages:

Join Our Telegram Community - Aspire2Learn


 Built-in Packages: Predefined in Java (e.g., java.util,
java.io, java.lang).

 User-defined Packages: Created by users to organize


their own classes.

How to Create User Define Package in Java?

Steps to Create and User Define Package:

1. Create a Package
 Use the package keyword at the beginning of the Java
file.

 Save the file inside a directory matching the package


name.

 Example: Creating a Package (mypackage)

Join Our Telegram Community - Aspire2Learn


2. Compile the Package -
 Open the terminal or command prompt and navigate to
the directory where the mypackage folder is located.

 Run the following command to compile:

 This creates a mypackage directory with a compiled


.class file.

3. Use the Package in Another Class


 Use import to include the package in another Java
program.

 Example: Using the Package

Join Our Telegram Community - Aspire2Learn


4. Compile and Run the Program

Compile:

Run:

Expected Output:

Join Our Telegram Community - Aspire2Learn


Key Points About Packages -
 Helps in organizing Java classes logically.

 Avoids naming conflicts.

 Provides access control (public, private, protected).

 Enhances reusability and maintainability.

------------------------------------------------------------------------------
6. Write an applet to display the message “Hello Java”.
S[2023],W[2018].
Ans –
1. Below is a simple Java applet that displays the message
"Hello Java". Please note that applets are considered
deprecated in modern Java development, and many
browsers no longer support them.

2. However, for educational purposes, here's how you can


create a basic applet.

Java Applet Example: Displaying "Hello Java"


Join Our Telegram Community - Aspire2Learn
Steps to Create and Run the Applet

1. Save the Code:

 Save the above code in a file named HelloJavaApplet.java.

2. Compile the Applet:

 Open your terminal or command prompt.

 Navigate to the directory where you saved the file.

 Compile the applet using:

Join Our Telegram Community - Aspire2Learn


3. Create an HTML File:

 To run the applet, you need an HTML file that loads it.
Create a file named HelloJavaApplet.html with the
following content:

4. Run the Applet:

 Use the appletviewer tool to run your applet. In your


terminal or command prompt, execute:
Join Our Telegram Community - Aspire2Learn
Output –

 When you run the applet using appletviewer, you should


see a window displaying the message "Hello Java".

7. Write an applet that receives three numeric values as


input from the user and then displays the largest of the three
on the screen. S[2019],W[2019].
Ans –
Code:

Join Our Telegram Community - Aspire2Learn


Join Our Telegram Community - Aspire2Learn
Steps to Run the Applet:

1. Save the file as LargestNumberApplet.java

2. Compile the program using:

3. Create an HTML file (LargestNumberApplet.html)


with the following content:

Join Our Telegram Community - Aspire2Learn


4. Run the applet using Applet Viewer:

------------------------------------------------------------------------------
10. What is exception ? Write a program in java to handle
divided by zero exception. W[2019].
OR
11. Write a program in java to handle divided by zero
exception. S[2024,2023,2019,2018].
Ans -
Exception in Java
 An exception in Java is an unexpected event that occurs
during the execution of a program, disrupting the normal
flow. It is used to handle runtime errors, such as divide
by zero, invalid array index, file not found, etc.

Types of Exceptions –
Join Our Telegram Community - Aspire2Learn
1. Checked Exceptions (Compile-time) -
 Occur during compilation.

 Must be handled using try-catch or throws.

Examples:

 IOException

 SQLException

 FileNotFoundException

2. Unchecked Exceptions (Runtime) -


 Occur at runtime due to logic errors.

 Not required to handle explicitly.

Examples:
 ArithmeticException

 NullPointerException

 ArrayIndexOutOfBoundsException

3. Errors

Join Our Telegram Community - Aspire2Learn


 Critical issues beyond user control.

Examples:
 StackOverflowError

 OutOfMemoryError

Java Program to Handle Divide by Zero Exception –

 This program demonstrates how to handle


ArithmeticException when dividing a number by zero.

Program:

Join Our Telegram Community - Aspire2Learn


Explanation:

1. Try Block (try)

 The program attempts to divide num1 by num2 (which


is 0).

 Since division by zero is not allowed, an


ArithmeticException occurs.

2. Catch Block (catch)

 The exception is caught, and an error message is


displayed.

3. Finally Block (finally)

 Executes regardless of an exception, ensuring cleanup or


final statements.

Expected Output:

Join Our Telegram Community - Aspire2Learn


12. What is exception ? How can exceptions be handled in
java. W[2018].
OR
13. What do you mean by exception ? How can we handle it ?.
W[2017].
Ans –
Exception in Java
 An exception in Java is an unexpected event that occurs
during the execution of a program, disrupting the normal
flow. It is used to handle runtime errors, such as divide
by zero, invalid array index, file not found, etc.

Types of Exceptions –

1. Checked Exceptions (Compile-time) -


 Occur during compilation.

 Must be handled using try-catch or throws.

Examples:

 IOException

 SQLException

Join Our Telegram Community - Aspire2Learn


 FileNotFoundException

2. Unchecked Exceptions (Runtime) -


 Occur at runtime due to logic errors.

 Not required to handle explicitly.

Examples:
 ArithmeticException

 NullPointerException

 ArrayIndexOutOfBoundsException

3. Errors
 Critical issues beyond user control.

Examples:
 StackOverflowError

 OutOfMemoryError

Exceptions in Java can be handled using the following


main approaches:

1. try-catch block:

Join Our Telegram Community - Aspire2Learn


 Place code that may throw an exception in the try block.

 Catch specific exceptions in catch blocks.

2. finally block:

 Used with try-catch to execute code regardless of


whether an exception occurred.

3. throw keyword:

 Used to explicitly throw an exception.


Join Our Telegram Community - Aspire2Learn
4. throws clause:

 Used in method declarations to specify exceptions that


may be thrown.

5. try-with-resources:

 Automatically closes resources that implement


AutoCloseable interface.

------------------------------------------------------------------------------

Join Our Telegram Community - Aspire2Learn


Test Your Knowledge Through
Aspire2Learn Online Quiz –

------------------- Aspire2Learn Online Quiz-------------------

------------------------------------------------------------------------------
Aspire2Learn Feedback Form –
- Your Voice, Our Priority

----------------------- Feedback Link -----------------------------


------------------------------------------------------------------------------

Join Our Telegram Community - Aspire2Learn


Join Our Telegram Community - Aspire2Learn

You might also like