Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
42 views

Assignment On Java

The document is an assignment submission for a Programming with Java course. It contains the student's answers to 6 questions about Java programming concepts. The questions cover exception handling, Java servlets architecture, SQL packages in Java, socket programming, threads and synchronization, and file handling techniques in Java. For each question, the student provides explanations of the key concepts and terms in 3 paragraphs or more.

Uploaded by

Mehrab Hossain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Assignment On Java

The document is an assignment submission for a Programming with Java course. It contains the student's answers to 6 questions about Java programming concepts. The questions cover exception handling, Java servlets architecture, SQL packages in Java, socket programming, threads and synchronization, and file handling techniques in Java. For each question, the student provides explanations of the key concepts and terms in 3 paragraphs or more.

Uploaded by

Mehrab Hossain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

BANGAMATA SHEIKH FOJILATUNNESA MUJIB SCIENCE

& TECHNOLOGY UNIVERSITY

Department of Computer Science & Engineering

Course Code: CSE 2121


Course Title: Programming with JAVA

Assignment On: Programming with Java

Submitted By : Submitted To :

Mehrab Hossain Sujit Roy


ID: 19111120 Lecturer
Year: 2nd Department of CSE
Semester: 1st BSFMSTU
Session: 2018-19

Submission Date: 9 September,2020


1. What do you mean by Exception Handling? Describe the Exception
Handling.

Answer:
Exception handling is one of the most important feature of java programming that allows us to
handle the runtime errors caused by exceptions.
First of all, we know about exception:

An Exception is an unwanted event that interrupts the normal flow of the program. When an
exception occurs program, execution gets terminated. In such cases we get a system generated
error message. The good thing about exceptions is that they can be handled in Java. By handling
the exceptions, we can provide a meaningful message to the user about the issue rather than a
system generated message, which may not be understandable to a user.
But The question arises that why an exception occurs?

There can be several reasons that can cause a program to throw exception. For example: Opening
a non-existing file in your program, Network connection problem, bad input data provided by
user etc.

Now, we will know about Exception Handling.

Exception Handling:
If an exception occurs, which has not been handled by programmer then program execution gets
terminated and a system generated error message is shown to the user. For example, look at the
system generated exception below:
An exception generated by the system is given below

Fig-01: An example of Exception Handling.


This message is not user friendly so a user will not be able to understand what went wrong. In
order to let them know the reason in simple language, we handle exceptions. We handle such
conditions and then prints a user-friendly warning message to user, which lets them correct the
error as most of the time exception occurs due to bad data provided by user.
In the end, Exception handling ensures that the flow of the program doesn’t break when an
exception occurs.

2. Draw the Java servlets architecture and explain it.

Java Servlets:

Java Servlets are software component that runs on a web server or an application server and be
responsible to receive the request from the web server, process the request, and respond back to
the server.

Servlets extends the capabilities of a server as they can respond to many types of requests, they
act like web containers for hosting web applications on web servers. They support nearly all the
client–server protocols, but is often used with HTTP and also known as HTTP servlet.

Architecture:

Fig-02: Java Servlet Architecture.


• The user sends HTTP requests to the web server

• The server has the web container containing servlet, which gathers data from the database
and creates a response.

• The response created by servlet is sent through HTTP Response to the client browser. The

question arises that how is the servlet’s response converted to HTTP Response format? As

the web server works purely on the HTTP protocol. Hence, this conversion from servlet’s

response to HTTP response is taken care by the web container.

3. Explain the JAVA SQL packages.

java.sql:

We have relational databases, from which at many times we need to access the data. For various
data processing related matters from RDDBMS we have java.sql package. The various classes in
the package are shown below:

Class Description

Date It gives time in milliseconds. It is a wrapper type. It provides sql


with dates. The class is declared as:
public class Date extends Date
The class methods are inherited from date class.
DriverManager The class is designed for managing the various JDBC drivers. The
class is declared as follows:
public class DriverManager extends Object
The class methods are inherited from Object class.
DriverPropertyInfo The class keeps an account for managing the various properties of
JDBC drivers which are required for making a secure connection.
The class is declared as follows:
public class DriverProperty Info extends Object
The class methods are inherited from Object class.
SQLPermission The class manages the various SQL related permissions which are
provided to the accessing objects. The class is declared as follows:
public final class SQLPermission extends BasicPermission
The class methods are inherited from BasicPermission class.
Time It is wrapper class around java.util. The class provides time related
information. The class is declared as follows:
public class Time extends Date
The class methods are inherited from Date class.
Timestamp It is wrapper class around java.util. The class allows JDBC API to
identify as TIMESTAMP value. The class is declared as follows:
public class Timestamp extends Date
The class methods are inherited from Date class.
Types The class defines various SQL constants. The class is declared as
follows:
public class Types extends Object
The class methods are inherited from Object class.

4. Short notes: Socket programming in JAVA.

Socket Programing: Socket programming is a way of connecting two nodes on a network to


communicate with each other. One socket (node) listens on a particular port at an IP, while
other socket reaches out to the other in order to form a connection.

Fig-03: Java Socket Programming.


The server forms the listener socket while the client reaches out to the server. Socket and Server
Socket classes are used for connection-oriented socket programming

java socket:

A socket is one end-point of a two-way communication link between two programs running on
the network.

Socket class:

A socket is simply an endpoint for communications between the machines. The Socket class can
be used to create a socket.

Important methods

ServerSocket class:

The ServerSocket class can be used to create a server socket. This object is used to establish
communication with the clients.

Important methods:
Example of Java Socket Programming:

5. Define: Thread, Multi-thread, Synchronization and Deadlock.

Answer:

Thread: A thread, in the context of Java, is the path followed when executing a program. All
Java programs have at least one thread, known as the main thread, which is created by the Java
Virtual Machine (JVM).

Multi-thread:

Multithreading in Java is a process of executing multiple threads simultaneously. Multithreading


is used to achieve multitasking.
Synchronization:

Synchronization in java is the capability to control the access of multiple threads to any shared

resource. Java Synchronization is better option where we want to allow only one thread to access
the shared resource.

Deadlock:

Deadlock describes a situation where two or more threads are blocked forever, waiting for each
other. Deadlock occurs when multiple threads need the same locks but obtain them in different
order.

6. Explain the JAVA’s file management/handling techniques.

File handling:

File handling refers to working with the file in java. Reading files & writing into java files is
known as file handling in java. The FIle is a container that can contain different types of
information. The file can contain text, images, videos, tables, etc.

In Java, File class enables us to work with different types of files. File class is a member of the
java.io packages. Java provides different methods to read, write, update & delete files.

Different types of operation which can be performed on a file is given below:


• Creating a file
• Updating a file
• Deleting a file
• Uploading a file to a specific location
• Opening file
• Closing file
• Syntax:

• In the below-given syntax, package java.io is imported into the program to work with the
file. java.io package exposes a File class that can be initiated by referencing the file to the
constructor of File class.

File Handling Methods

Some of the methods are given below for performing different operations in java:

1. createNewFile(): createNewFile method used to create an empty file. It returns the


response as boolean.
2. getName(): This method is used to get the file name. It returns the string i.e. name of the
file in response.
3. getAbsolutePath(): It returns the absolute path of the file. The return type of this method
is a string.
4. canRead(): This method used to check whether the file is readable or not. It returns a
boolean value.
5. canWrite(): This method used to check whether the file is writable or not. It returns a
boolean value.
6. delete(): This method used in deleting a file. It returns a boolean value.
7. exists(): This method used to check whether a file exists or not. It returns a boolean value.
8. length(): This method returns the size of the file in bytes. The return type of this method
is long.
9. list(): This method returns an array of the files available in the directory. It returns an
array of string values.
10. mkdir(): This method is used to create a directory. It returns a boolean value.

Example of File handling:


In this case, to create a file you can use the createNewFile() method. This method returns true if
the file was successfully created, and false if the file already exists.

Output:
7. Short note: Exception handling, Try catch block.

Answer:

Exception handling:

Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException,


IOException, SQLException, RemoteException, etc.

Catching and handling exceptions:

In Java, we use the exception handler components try, catch and finally blocks to handle
exceptions.
To catch and handle an exception, we place the try...catch...finally block around the code that
might generate an exception. The finally block is optional.
The syntax for try...catch...finally is:

Java try...catch block:

The code that might generate an exception is placed in the try block.
Every try block should be immediately followed by the catch or finally block. When an
exception occurs, it is caught by the catch block that immediately follows it.
catch blocks cannot be used alone and must always be preceded by a try block.
Example 1: try...catch blocks

Output:

You might also like