Unit 3 App
Unit 3 App
Programming Paradigms
Concurrent Programming Paradigm: Multithreading and Multitasking; Thread classes
and methods - Declarative Programming Paradigm: Java Database Connectivity
(JDBC); Connectivity with MySQL – Query Execution; - Graphical User Interface
Based Programming Paradigm: Java Applet: Basics and Java Swing: Model View
Controller (MVC) and Widgets; Develop a java project dissertation based on the
programming paradigm.
Concurrent Programming:
• This means that tasks appear to run simultaneously, but under the hood, the system
might really be switching back and forth between the tasks.
• it is beneficial even on a single processor machine.
Threads are useful only when the task is relatively large and pretty much
self contained. When the user needs to perform only a small amount of
combination after a large amount of separate processing, there’s some
overhead to starting and using threads. So if the task is really small, one
never get paid back for the overhead.
threads are most useful when the users are waiting. For instance, while
one is waiting for one server, the other can be reading from another
server.
Advantages:
Loose Coupling: Since a separate class can be reused, it promotes loose coupling.
Constructors: Arguments can be passed to constructors for different cases. For example,
describing different loop limits for threads.
Race Conditions: If the data has been shared, it is unlikely that a separate class would be
used as an approach and if it does not have a shared data, then no need to worry about the race
conditions.
Disadvantages:
It was a little bit inconvenient to call back to the main application. A reference had to be
passed along the constructor, and even if there is access to reference, only public
methods(pause method in the given example) in the main application can be called.
Multithreading
// Main Class
public class Multithread {
public static void main(String[] args)
{
int n = 8; // Number of threads
for (int i = 0; i < n; i++) {
MultithreadingDemo object = new MultithreadingDemo();
object.start();
}
}
}
Multiprogramming and Multitasking
Multiprogramming:-
Multi-programming increases CPU utilization by organizing jobs (code and data) so that the CPU always has one
to execute.
The idea is to keep multiple jobs in main memory. If one job gets occupied with IO, CPU can be assigned to
other job.
Multi-tasking:-
Multi-tasking is a logical extension of multiprogramming.
ability of an OS to execute more than one task simultaneously on a CPU machine.
These multiple tasks share common resources (like CPU and memory).
In multi-tasking systems, the CPU executes multiple jobs by switching among them typically using a small
time quantum, and the switches occur so quickly that the users feel like interact with each executing task at
the same time.
Types of Multitasking:
• Process-based Multitasking: Multiple processes run concurrently. Each process
has its own memory space.
• Thread-based Multitasking: Multiple threads within the same process are
executed concurrently, as described in the multithreading section.
Example: Consider running multiple Java programs or scripts on your machine. Each
program is a separate process, and the operating system manages their execution.
Example Scenario:
• You have a Java-based server running in the background.
• Simultaneously, you run another Java program for data analysis.
• Both programs are independent processes that run concurrently, showcasing
process-based multitasking.
Thread Class in Java
import java.io.*;
import java.util.*;
A thread is a program that starts with a method() frequently used in this class only known
as the start() method.
This method looks out for the run() method which is also a method of this class and
begins executing the body of the run() method. Here, keep an eye over the sleep() method.
Every class that is used as thread must implement Runnable interface and over ride it’s run method.
SYNTAX
We can use JDBC API to access tabular data stored in any relational database.
By the help of JDBC API, we can save, update, delete and fetch data from the database.
It is like Open Database Connectivity (ODBC) provided by Microsoft.
Components of JDBC
There are generally four main components of JDBC through which it can interact with a database.
1. JDBC API: It provides various methods and interfaces for easy communication with the database.
It provides two packages as follows, which contain the java SE and Java EE platforms to exhibit WORA(write
once run anywhere) capabilities.
The java.sql package contains interfaces and classes of JDBC API.
2. JDBC Driver manager: It loads a database-specific driver in an application to establish a connection with a
database. It is used to make a database-specific call to the database to process the user request.
3. JDBC Test suite: It is used to test the operation(such as insertion, deletion, updation) being performed by
JDBC Drivers.
4. JDBC-ODBC Bridge Drivers: It connects database drivers to the database. This bridge translates the JDBC
method call to the ODBC function call. It makes use of the sun.jdbc.odbc package which includes a native
library to access ODBC characteristics.
Architecture of JDBC
Application: It is a java applet or a servlet that communicates with a data source.
The JDBC API: The JDBC API allows Java programs to execute SQL statements and
retrieve results.
Some of the important interfaces defined in JDBC API are as follows: Driver interface ,
ResultSet Interface , RowSet Interface , PreparedStatement interface, Connection inteface,
and cClasses defined in JDBC API are as follows: DriverManager class, Types class, Blob
class, clob class.
JDBC drivers: To communicate with a data source through JDBC, you need a JDBC
driver that intelligently communicates with the respective data source.
//Java program to implement a simple JDBC application
package com.vinayak.jdbc; // Obtain a statement
Statement st = con.createStatement();
import java.sql.*;
// Execute the query
public class JDBCDemo { int count = st.executeUpdate(query);
System.out.println(
public static void main(String args[]) "number of rows affected by this query= "
throws SQLException, ClassNotFoundException + count);
{
String driverClassName= "sun.jdbc.odbc.JdbcOdbcDriver"; // Closing the connection as per the
String url = "jdbc:odbc:XE"; // requirement with connection is completed
String username = "scott"; con.close();
String password = "tiger"; }
String query = "insert into students values(109, 'bhatt')"; } // class
// Obtain a connection
Connection con = DriverManager.getConnection(
url, username, password);
SQL
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the International
Organization for Standardization (ISO) in 1987
SQL commands are extensively used to interact with databases, enabling users to perform a wide range of actions
on database systems. Understanding these commands is crucial for effectively managing and manipulating data.
mysql --version
mysql –h localhost –P 3306 –u root –p
show databases;
create database oracle;
use oracle;
create table emp (dept int, Ename varchar(100), Job varchar(100), Sal int,Comm int);
insert into emp values(1, ‘queen’, ‘manager’, 1000, null);
insert into emp values(2, ‘king’, ‘chairman’, 1000, null);
select * from emp;
// Create a button
JButton button = new JButton("Click Me");
Here’s a sample structure and guide for a Java project dissertation based on programming paradigms.
The dissertation could explore Object-Oriented Programming (OOP), Functional Programming (FP), or other paradigms. I
will use OOP as the central focus, as Java is primarily an OOP language.
Title:
Exploring Object-Oriented Programming Paradigm in Java
Abstract
This dissertation explores the Object-Oriented Programming (OOP) paradigm within Java development. The project
focuses on key OOP principles such as inheritance, encapsulation, polymorphism, and abstraction. A Java project will
be used to demonstrate the practical application of these principles through the development of a simple system, such as
a library management system. This work aims to show how Java's OOP capabilities make software design more
modular, maintainable, and scalable.
Table of Contents
1. Introduction
2. Literature Review
3. Overview of Object-Oriented Programming
4. Project Implementation
1. Project Description
2. System Design
3. Class Structure and Hierarchy
4. Inheritance and Polymorphism in Action
5. Encapsulation and Abstraction in Action
5. Testing and Evaluation
6. Conclusion
7. Future Work
8. References
9. Appendix
1. Introduction
The introduction will present the purpose of the dissertation and provide background on
programming paradigms, specifically focusing on OOP. Java will be introduced as a
prominent OOP language used in real-world applications. The objective of the project will
be outlined, such as building a library management system.
Problem Statement
The complexity of real-world software demands a modular approach to make systems
scalable and maintainable. The OOP paradigm facilitates this by focusing on creating
objects that represent real-world entities.
Objectives
To demonstrate the principles of OOP in Java.
To create a simple Java-based library management system.
To evaluate the benefits of OOP such as modularity, reusability, and maintainability.
2. Literature Review
This section will cover a review of existing programming paradigms with an emphasis
on OOP. It will explore the historical evolution of programming paradigms and discuss
how OOP has become a dominant approach, especially in Java. You can compare OOP
with procedural programming and functional programming in this section.
Key References:
•Books like "Effective Java" by Joshua Bloch.
•Research papers on the impact of OOP in large software systems.
•Comparative analysis of OOP and other paradigms.
@Test
public void testBookAvailability() {
Book book = new Book("Java Programming", "Author Name");
assertTrue(book.isAvailable());
book.issueBook();
assertFalse(book.isAvailable());
}
}
5.2 Evaluation Criteria
Code reusability through inheritance.
Flexibility with polymorphism.
System maintainability due to modular class structure.
6. Conclusion
This section summarizes the key findings of the project. It highlights how Java’s OOP features contribute to creating
maintainable, modular, and scalable applications. The dissertation will conclude that Java’s OOP capabilities can
efficiently model real-world problems in software.
7. Future Work
Future work could focus on extending the project by adding more advanced OOP features, like interfaces and design
patterns. The system could be scaled to include more complex real-world scenarios, such as handling multiple
libraries.
8. References
List academic references, textbooks, Java documentation, and research papers used in the dissertation.
9. Appendix
This section will include the complete source code of the project, screenshots of
the system in action, and additional UML diagrams if necessary.
This structure provides a comprehensive approach for a Java project dissertation based on
the programming paradigm, with a focus on OOP.
JAVA SWING
Swing is a Java Foundation Classes [JFC] library and an extension of the Abstract Window Toolkit [AWT].
// Java program using label (swing)
// to display the message “GFG WEB Site Click”
import java.io.*;
import javax.swing.*;
// Main class
class swingg {
// Main driver method
public static void main(String[] args)
{
// Creating instance of JFrame
JFrame frame = new JFrame();
// Creating instance of JButton
JButton button = new JButton(" GFG WebSite Click");
// x axis, y axis, width, height
button.setBounds(150, 200, 220, 50);
// adding button in JFrame
frame.add(button);
// 400 width and 500 height
frame.setSize(500, 600);
// using no layout managers
frame.setLayout(null);
// making the frame visible
frame.setVisible(true);}}
// Java program to create three buttons
// with caption OK, SUBMIT, CANCEL
import java.awt.*;
class button {
button()
{ Frame f = new Frame();
// Button 1 created
// OK button
Button b1 = new Button("OK");
b1.setBounds(100, 50, 50, 50);
f.add(b1);
// Button 2 created
// Submit button
Button b2 = new Button("SUBMIT");
b2.setBounds(100, 101, 50, 50);
f.add(b2);
// Button 3 created
// Cancel button
Button b3 = new Button("CANCEL");
b3.setBounds(100, 150, 80, 50);
f.add(b3);
f.setSize(500, 500);
f.setLayout(null);
f.setVisible(true);
JAVA SWING
MVC