1.
Arrays in Java
1.1 Different Formats of Arrays in Java
An array is a data structure that stores multiple values of the same type in a single variable.
Java supports three main formats of arrays:
1. One-Dimensional Array
Linear collection of elements.
Syntax:
int[] numbers = {10, 20, 30, 40};
Accessing elements:
[Link](numbers[2]); // Output: 30
2. Two-Dimensional Array
Represents data in rows and columns (a table or matrix).
Syntax:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Accessing elements:
[Link](matrix[1][2]); // Output: 6
3. Jagged Array
An array of arrays with different column lengths.
Syntax:
int[][] jagged = new int[2][];
jagged[0] = new int[3];
jagged[1] = new int[5];
1.2 Data Storage and Retrieval in Arrays
One-Dimensional Array
Storage: Stored in contiguous memory locations.
Retrieval: Accessed by index starting at 0.
Example:
int[] scores = {90, 85, 70};
[Link]("First Score: " + scores[0]); // Output: 90
Two-Dimensional Array
Stored in row-major order (row by row).
Accessed using two indices (row and column).
Example:
int[][] data = {{1,2}, {3,4}};
[Link]("Element at row 1, col 0: " + data[1][0]); // 3
1.3 Java Program: Input Array of Strings Using Dialog Box
import [Link];
public class StringArrayInput {
public static void main(String[] args) {
int n = [Link]([Link]("Enter number of
names:"));
String[] names = new String[n];
for (int i = 0; i < n; i++) {
names[i] = [Link]("Enter name " + (i + 1));
}
String result = "You entered:\n";
for (String name : names)
result += name + "\n";
[Link](null, result);
}
}
2. Event-Driven Programming
2.1 Definition
Event-driven programming is a paradigm where the flow of the program is determined by user
actions (events) such as clicking a button or typing text.
Main Components:
1. Event Source: The object that generates the event (e.g., button).
2. Event Object: Stores details of the event.
3. Event Listener: Method that responds to the event.
2.2 Placing Buttons and Controls on a Java Frame
Example:
import [Link].*;
import [Link].*;
public class MyFrame {
public static void main(String[] args) {
JFrame frame = new JFrame("Example Frame");
JButton btn = new JButton("Click Me");
[Link](btn);
[Link](new FlowLayout());
[Link](300, 200);
[Link](true);
}
}
2.3 Java Program: Button Click Event
import [Link].*;
import [Link].*;
public class ButtonEvent {
public static void main(String[] args) {
JFrame frame = new JFrame("Event Example");
JButton button = new JButton("Click Me");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link](null, "Button Clicked!");
}
});
[Link](button);
[Link](200, 150);
[Link](new FlowLayout());
[Link](true);
}
}
3. Classes and Objects in Java
3.1 Definition of Class
A class is a blueprint or template for creating objects.
It defines attributes (variables) and behaviors (methods) that describe an entity.
Syntax:
class ClassName {
// fields (attributes)
int id;
String name;
// methods (behaviors)
void display() {
[Link](id + " " + name);
}
}
3.2 Definition of Object
An object is an instance of a class.
Objects occupy memory and can call methods or access fields defined in their class.
Syntax:
ClassName obj = new ClassName();
3.3 Creating and Using Classes and Objects
Example:
class Student {
int id;
String name;
void display() {
[Link]("ID: " + id + ", Name: " + name);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student(); // Creating an object
[Link] = 1;
[Link] = "Salim Bello";
[Link]();
}
}
Output:
ID: 1, Name: Salim Bello
3.4 Importance of Classes and Objects
Promotes modularity and reusability.
Supports encapsulation (data hiding).
Forms the foundation of Object-Oriented Programming (OOP).
4. Object-Oriented Programming Concepts
4.1 Key Concepts
1. Inheritance:
Mechanism where one class (child) derives attributes and methods from another class (parent).
2. Encapsulation:
Wrapping of data and methods into a single unit, restricting direct access to data.
3. Polymorphism:
Ability of objects to behave differently based on their type (achieved through method
overriding).
4.2 Program Demonstrating Inheritance
class Animal {
Animal() {
[Link]("Animal Created");
}
void sound() {
[Link]("Animal makes sound");
}
}
class Dog extends Animal {
Dog() {
super(); // Calls parent constructor
[Link]("Dog Created");
}
void sound() {
[Link]("Dog barks");
}
}
public class InheritanceDemo {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
}
}
4.3 Polymorphism in Method Overriding
Polymorphism allows a superclass reference to call subclass methods at runtime.
Example:
Animal a = new Dog(); // Upcasting
[Link](); // Calls Dog’s sound()
5. Java Servlets
5.1 Definition and Purpose
A Servlet is a Java program that runs on a web server and handles HTTP requests and responses.
Purpose: Used for building dynamic web pages and server-side applications.
5.2 Servlet Development Process
1. Create Servlet Class:
import [Link].*;
import [Link].*;
import [Link].*;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h2>Hello from Servlet!</h2>");
}
}
2. Map Servlet in [Link]:
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
3. Deploy: Place in WEB-INF/classes and run on Tomcat or similar server.
5.3 Difference Between HTTP GET and POST
GET POST
Sends data in URL Sends data in request body
Limited size Large data supported
Less secure More secure
Used for fetching data Used for submitting data
6. JDBC (Java Database Connectivity)
6.1 Definition and Role
JDBC is an API that enables Java applications to connect to and interact with databases.
6.2 Steps to Establish JDBC Connection
1. Import JDBC packages
2. Load the driver
3. Establish connection
4. Create statement
5. Execute query
6. Process results
7. Close connection
6.3 Example: Retrieve Data from “students” Table
import [Link].*;
public class FetchData {
public static void main(String[] args) {
try {
[Link]("[Link]");
Connection con = [Link](
"jdbc:mysql://localhost:3306/school", "root", "");
Statement st = [Link]();
ResultSet rs = [Link]("SELECT * FROM students");
while ([Link]()) {
[Link]([Link]("name") + " - " +
[Link]("age"));
}
[Link]();
} catch (Exception e) {
[Link]();
}
}
}
7. Enterprise Solutions in Java
7.1 Definition and Purpose
An Enterprise Solution is large-scale software that integrates various business functions across an
organization.
Purpose: Improve collaboration, automate processes, and manage resources efficiently.
7.2 Developing Enterprise Solutions Using Java
Steps:
1. Requirement Analysis
2. Design Architecture
3. Use Java EE Technologies:
Servlets & JSP: Web presentation
EJB: Business logic
JPA/Hibernate: Data handling
Spring Framework: Integration and dependency management
4. Testing and Deployment
7.3 Application Areas of Enterprise Solutions
1. Banking Systems – Handle customer transactions.
2. Healthcare Management – Manage patient data and appointments.
3. Educational Portals – Support student information, results, and course management.