You are on page 1of 59

Sant Longowal Institute of Engineering and

Technology, Longowal, Sangrur

Sub:-Internet Programming Lab


Sub Code:PCCS -713

Submitted By:- Submitted To:-


Rohit Rana Professor Jaspal Singh
GCS-18cd - 1930065 CSE Department
Sr. LIST OF PRACTICALS Page No. Date Sign
No.

1 Write a Java Program and explain it’s 4 07/08/2021


paradigm of execution along-with the concept
of Programming Logic & Business
Logic( Class & Object).

2 Write a Java Program and explain the 6 13/08/2021


different Data types along-with the default
values.

3 Write a Java Program to demonstrate 9 20/08/2021


different control structures & operators in
Java.

4 Write a Java Program to handle the command 17 27/08/2021


line arguments (display them) .

5 Write a Java Program to demonstrate the 18 05/09/2021


insertion, deletion & searching of element in an
Array.

6 Write a Java Program to demonstrate the 21 12/09/2021


concept of Wrapper Class (String class
methods).

7 Write a Java Program to demonstrate the 22 19/09/2021


comparative analysis of Vector Class & Arrays
in Java.

8 Write a Java Program to demonstrate the 23 19/09/2021


concept of Exception handling & its various
forms.

9 Write a Java Program to demonstrate the 26 27/09/2021


concept of Inheritance & Interface in Java.

10 Write a Java Program to demonstrate the 31 07/10/2021


concept of Packages & Interfaces.

11 Write a Java Program to demonstrate the 39 14/10/2021


concept of Constructor Overloading,
(Parameterized constructors).

2
12 Write a Java Program to demonstrate the 40 21/10/2021
concept of Inner classes & Anonymous classes.

13 Write a Java Program to demonstrate the 41 26/10/2021


concept of Multithreading in Java.

14 Write a Java Program to demonstrate the I/O 43 07/11/2021


streams handling & its different forms.

15 Write a Java Program that shows the 45 14/11/2021


implementation of abstract class.

16 Write a Java Program to demonstrate the 46 21/11/2021


concept of Applet programming & loading an
image into applet.

17 Write a Java Program to demonstrate the 48 21/11/2021


concept of event-driven programming in Java
for different awt- components.

18 Write a Java Program to close the window of 50 27/11/2021


awt.

19 Write a Java Program to demonstrate the 52 27/11/2021


collection framework in Java( LinkedList &
ArrayList).

20 Write a comparative analysis of different 55 02/12/2021


features in different versions of Java.

Q1. Write a Java Program and explain it’s paradigm of execution along-with the
concept of Programming Logic & Business Logic( Class & Object).

3
Ans:-

OUTPUT :-

4
public :- a Java keyword which declares a member's access as public. Public members are visible to
all other classes. This means that any other class can access a public field or method.

static :- the keyword static means that the particular member belongs to a type itself, rather than to an
instance of that type.

void :- Used at method declaration and definition to specify that the method does not return any type,
the method returns void.

main:- It is the name of Java main method. It is the identifier that the JVM looks for as the starting
point of the java program. It’s not a keyword.

String[] args: It stores Java command line arguments and is an array of type java.lang.String class.
Here, the name of the String array is args but it is not fixed and user can use any name in place of it.

System.out.println() is used to print an argument that is passed to it.

CLASS :- A class is a group of objects which have common properties. It is a template or blueprint
from which objects are created. It is a logical entity. It can't be physical.
OBJECT :- An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen,
table, car, etc. It can be physical or logical (tangible and intangible).

Q2. Write a Java Program and explain the different Data types along-with the
default values.

5
Ans:- Java has two categories of Data types :-
1) Primitive Data Type
2) Non-Primitive Data Type or Object Data type

(1) Primitive Data Type: They are only single values and have no special capabilities.Example:
boolean, char, int, short, byte, long, float, and double.
1. boolean: boolean data type represents only one bit of information either true or false, but the
size of the boolean data type is virtual machine-dependent. Values of type boolean are not
converted implicitly or explicitly (with casts) to any other type. But the programmer can easily
write conversion code.
2. byte: The byte data type is an 8-bit signed two’s complement integer. The byte data type is
useful for saving memory in large arrays.
3. short: The short data type is a 16-bit signed two’s complement integer. Similar to byte, use a
short to save memory in large arrays, in situations where the memory savings actually matters.
4. int: It is a 32-bit signed two’s complement integer.
5. long: The long data type is a 64-bit two’s complement integer.
6. float: The float data type is a single-precision 32-bit floating-point. Use a float (instead of
double) if you need to save memory in large arrays of floating-point numbers.
7. double: The double data type is a double-precision 64-bit floating-point. For decimal values,
this data type is generally the default choice.
8. char: The char data type is a single 16-bit Unicode character.

(2) Non-Primitive Data Type or Reference Data Types


The Reference Data Types will contain a memory address of variable value because the reference
types won’t store the variable value directly in memory. Example strings, objects, arrays, etc.

6
● String: Strings are defined as an array of characters. The difference between a character array
and a string in Java is, the string is designed to hold a sequence of characters in a single
variable whereas, a character array is a collection of separate char type entities.
● Array: An array is a group of like-typed variables that are referred to by a common name.

Code:-

Output:-

7
8
Q3. Write a Java Program to demonstrate different control structures & operators
in Java.

1) Simple If statement :- It is the most basic statement among all control flow statements in
Java. It evaluates a Boolean expression and enables the program to enter a block of code if the
expression evaluates to true.

OUTPUT:-

2) if-else statement :- The if-else statement is an extension to the if-statement, which uses
another block of code, i.e., else block. The else block is executed if the condition of the if-
block is evaluated as false.

9
OUTPUT:-

3) if-else-if ladder:- The if-else-if statement contains the if-statement followed by multiple else-if
statements.

10
OUTPUT:-

4. Nested if-statement:- In nested if-statements, the if statement can contain a if or if-else


statement inside another if or else-if statement.

OUTPUT:-

11
Switch Statement:- Switch statements are similar to if-else-if statements. The switch statement
contains multiple blocks of code called cases and a single case is executed based on the variable which
is being switched.

OUTPUT:-

for loop:- It enables us to initialize the loop variable, check the condition, and increment/decrement
in a single line of code. We use the for loop only when we exactly know the number of times, we want
to execute the block of code.

12
OUTPUT:-

for-each loop:- In the for-each loop, we don't need to update the loop variable.

OUTPUT:-

13
while loop:- The while loop is also used to iterate over the number of statements multiple times.
However, if we don't know the number of iterations in advance, it is recommended to use a while loop.
Unlike for loop, the initialization and increment/decrement doesn't take place inside the loop statement
in while loop.

OUTPUT:-

14
do-while loop:- The do-while loop checks the condition at the end of the loop after executing the
loop statements. When the number of iteration is not known and we have to execute the loop at least
once, we can use do-while loop.

OUTPUT:-

15
Jump Statements

Jump statements are used to transfer the control of the program to the specific statements.

Java break statement

As the name suggests, the break statement is used to break the current flow of the program and transfer
the control to the next statement outside a loop or switch statement. However, it breaks only the inner
loop in the case of the nested loop.

The break statement cannot be used independently in the Java program, i.e., it can only be written
inside the loop or switch statement.

Java continue statement

Unlike break statement, the continue statement doesn't break the loop, whereas, it skips the specific
part of the loop and jumps to the next iteration of the loop immediately.

16
Q4. Write a Java Program to handle the command line arguments (display them) .

Program Code:

Output:

Q5. Write a Java Program to demonstrate the insertion, deletion & searching of
element in an Array.

17
Program to insert an element in an array

OUTPUT:-

Program to delete an element in an array

18
OUTPUT:-

Program to search an element in an array

19
OUTPUT:-

Q6. Write a Java Program to demonstrate the concept of Wrapper Class (String
class methods).

20
A Wrapper class is a class whose object wraps or contains primitive data types. When we create an
object to a wrapper class, it contains a field and in this field, we can store primitive data types. In other
words, we can wrap a primitive value into a wrapper class object.

Code:-

Output:-

Q7. Write a Java Program to demonstrate the comparative analysis of Vector


Class & Arrays in Java.

21
● A vector is a dynamic array, whose size can be increased, where as an array size can not be
changed.
● A vector is a class where as an array is not.
● Vectors can store any type of objects, where as an array can store only homogeneous values.
● They are very similar to ArrayList, but Vector is synchronized and has some legacy methods
that the collection framework does not contain.
● It also maintains an insertion order like an ArrayList. Still, it is rarely used in a non-thread
environment as it is synchronized, and due to this, it gives a poor performance in adding,
searching, deleting, and updating its elements.
● The Iterators returned by the Vector class are fail-fast. In the case of concurrent modification, it
fails and throws the ConcurrentModificationException.

Code:-

22
Output:-

23
Code:-

Output:-

24
Advantages of Arrays:
- Arrays supports efficient random access to the members.
- It is easy to sort an array.
- They are more appropriate for storing fixed number of elements

Disadvantages of Arrays:
- Elements can not be deleted
- Dynamic creation of arrays is not possible
- Multiple data types can not be stored

Advantages of Vector:
- Size of the vector can be changed
- Multiple objects can be stored
- Elements can be deleted from a vector

Disadvantages of Vector:
- A vector is an object, memory consumption is more.

25
Q8. Write a Java Program to demonstrate the concept of Exception handling
& its various forms.
Java defines several types of exceptions that relate to its various class libraries. Java also allows users
to define their own exceptions.

There are two types of exception in java:-


Built-in Exceptions
User-Defined Exceptions

(a) Built-in Exceptions


Built-in exceptions are the exceptions which are available in Java libraries. These exceptions are
suitable to explain certain error situations. Below is the list of important built-in exceptions in Java.
1. ArithmeticException
It is thrown when an exceptional condition has occurred in an arithmetic operation.
2. ArrayIndexOutOfBoundsException
It is thrown to indicate that an array has been accessed with an illegal index. The index is either
negative or greater than or equal to the size of the array.
3. ClassNotFoundException
This Exception is raised when we try to access a class whose definition is not found
4. FileNotFoundException
This Exception is raised when a file is not accessible or does not open.
5. IOException
It is thrown when an input-output operation failed or interrupted
6. InterruptedException
It is thrown when a thread is waiting, sleeping, or doing some processing, and it is interrupted.
7. NoSuchFieldException
It is thrown when a class does not contain the field (or variable) specified
8. NoSuchMethodException
It is thrown when accessing a method which is not found.
9. NullPointerException
This exception is raised when referring to the members of a null object. Null represents nothing

26
10. NumberFormatException
This exception is raised when a method could not convert a string into a numeric format.
11. RuntimeException
This represents any exception which occurs during runtime.
12. StringIndexOutOfBoundsException
It is thrown by String class methods to indicate that an index is either negative or greater than
the size of the string

Code:-

27
Output:-

28
(b) User-Defined Exceptions
Sometimes, the built-in exceptions in Java are not able to describe a certain situation. In such cases,
user can also create exceptions which are called ‘user-defined Exceptions’.

29
Output:-

30
Q9. Write a Java Program to demonstrate the concept of Inheritance & Interface
in Java.

Inheritance

Inheritance is an important pillar of OOP(Object-Oriented Programming). It is the mechanism in java


by which one class is allowed to inherit the features(fields and methods) of another class.

Interfaces
Like a class, an interface can have methods and variables, but the methods declared in an interface are
by default abstract (only method signature, no body).
● Interfaces specify what a class must do and not how. It is the blueprint of the class.

31
● An Interface is about capabilities like a Player may be an interface and any class
implementing Player must be able to (or must implement) move(). So it specifies a set of
methods that the class has to implement.
● If a class implements an interface and does not provide method bodies for all functions
specified in the interface, then the class must be declared abstract.
● A Java library example is, Comparator Interface. If a class implements this interface, then it
can be used to sort a collection.

A real-world example:
Let’s consider the example of vehicles like bicycle, car, bike………, they have common
functionalities. So we make an interface and put all these common functionalities. And lets Bicycle,
Bike, car ….etc implement all these functionalities in their own class in their own way.

Code:-

32
33
Output:-

Q10. Write a Java Program to demonstrate the concept of Packages.

34
Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces.
Packages are used for:

● Preventing naming conflicts. For example there can be two classes with name Employee in two
packages, college.staff.cse.Employee and college.staff.ee.Employee
● Making searching/locating and usage of classes, interfaces, enumerations and annotations easier
● Providing controlled access: protected and default have package level access control. A protected
member is accessible by classes in the same package and its subclasses. A default member
(without any access specifier) is accessible by classes in the same package only.
● Packages can be considered as data encapsulation (or data-hiding).

Code:-

35
Output:-

36
Q11. Write a Java Program to demonstrate the concept of Constructor
Overloading, (Parameterized constructors).

Constructor overloading
The constructor overloading can be defined as the concept of having more than one
constructor with different parameters so that every constructor can perform a different
task.

OUTPUT:-

37
Q12. Write a Java Program to demonstrate the concept of Inner classes &
Anonymous classes.

Inner Classes
Inner classes are a security mechanism in Java. We know a class cannot be associated with the access
modifier private, but if we have the class as a member of other class, then the inner class can be made
private. And this is also used to access the private members of a class.

Output:

38
Anonymous Inner Class
An inner class declared without a class name is known as an anonymous inner class. In case of
anonymous inner classes, we declare and instantiate them at the same time. Generally, they are used
whenever you need to override the method of a class or an interface.

Output:-

Q13. Write a Java Program to demonstrate the concept of Multithreading in Java.

39
Multithreading in Java is a process of executing multiple threads simultaneously. A thread is a
lightweight sub-process, the smallest unit of processing. Multiprocessing and multithreading, both are
used to achieve multitasking.

Java provides Thread class to achieve thread programming. Thread class provides constructors and
methods to create and perform operations on a thread. Thread class extends Object class and
implements Runnable interface.

Program Output:

40
41
Q14. Write a Java Program to demonstrate the I/O streams handling & its
different forms.

Byte Streams
Java byte streams are used to perform input and output of 8-bit bytes.
Though there are many classes related to byte streams but the most
frequently used classes are, FileInputStream and FileOutputStream.
Following is an example which makes use of these two classes to copy an
input file into an output file −

Character Streams
Java Byte streams are used to perform input and output of 8-bit bytes, whereas Java Character streams
are used to perform input and output for 16-bit unicode. Though there are many classes related to
character streams but the most frequently used classes are, FileReader and FileWriter.

42
We can rewrite the above example, which makes the use of these two
classes to copy an input file (having unicode characters) into an output
file −

Standard Streams
All the programming languages provide support for standard I/O where
the user's program can take input from a keyboard and then produce an
output on the computer screen. If you are aware of C or C++ programming
languages, then you must be aware of three standard devices STDIN,
STDOUT and STDERR. Similarly, Java provides the following three
standard streams −
● Standard Input − This is used to feed the data to user's program and
usually a keyboard is used as standard input stream and represented
as System.in.
● Standard Output − This is used to output the data produced by the
user's program and usually a computer screen is used for standard
output stream and represented as System.out.

43
● Standard Error − This is used to output the error data produced by the
user's program and usually a computer screen is used for standard
error stream and represented as System.err.

Q15. Write a Java Program that shows the implementation of abstract class.

Program code:

Output:

44
Q16. Write a Java Program to demonstrate the concept of Applet programming &
loading an image into an applet.

Ans.

Applet is a special type of program that is embedded in the webpage to generate dynamic content. It
runs inside the browser and works at the client side.

Program Code:

Life Cycle of Java Applet

45
1. Applet is initialized.

2. Applet is started.

3. Applet is painted.

4. Applet is stopped.

5. Applet is destroyed.

For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle methods of
applet.

1. public void init(): is used to initialized the Applet. It is invoked only once.

2. public void start(): is invoked after the init() method or browser is maximized. It is used to
start the Applet.
3. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is
minimized.
4. public void destroy(): is used to destroy the Applet. It is invoked only once.

46
Q 17. Write a Java Program to demonstrate the concept of event-driven
programming in Java for different awt- components.

Program Code:

Program Output:

47
Q18. Write a Java Program to close the window of awt.

48
Program Output:

49
Q19. Write a Java Program to demonstrate the collection framework in Java( LinkedList &
ArrayList).

50
LinkedList

LinkedList implements the Collection interface. It uses a doubly linked list internally to store the
elements. It can store the duplicate elements. It maintains the insertion order and is not synchronized.
In LinkedList, the manipulation is fast because no shifting is required.

Consider the following example.

Program Output:

ArrayList

51
The ArrayList class implements the List interface. It uses a dynamic array to store the duplicate
element of different data types. The ArrayList class maintains the insertion order and is non-
synchronized. The elements stored in the ArrayList class can be randomly accessed. Consider the
following example.

Output:

52
Q20. Write the comparative analysis of different features in different version of
Java.

As of September 2021, Java 17 is the latest released Java version. It is also the next long-term support
version (LTS) after Java 11. Following graphic tells about the timeline of java versions.

Java versions before 9 simply had a different naming scheme. So, Java 8 can also be called 1.8,
Java 5 can be called 1.5, etc. This simply means Java 8. With the switch to time-based releases
with Java 9, the naming scheme also changed, and Java versions aren’t prefixed with 1.x
anymore.

1. JDK 1.1 (February 19, 1997)


Features:

53
● The idea of Inner Class

● JavaBeans

● JDBC

● RMI

● Reshaped AWT event model

● JIT (Just In Time) compiler: Used on Microsoft Windows stages, developed for JavaSoft by

Symantec

● Internationalization and Unicode support beginning from Taligent

2. J2SE Version 1.2 (December 8, 1998)


Features:

● Collections structure.

● Java String memory map for constants.

● JIT (Just In Time) compiler.

● Jar Signer for marking Java Archive (JAR) records.

● Policy Tool for allowing access to framework assets.

● Java Foundation Classes (JFC) which comprises of Swing 1.0, Drag and Drop, and Java 2D

class libraries.

54
3. J2SE Version 1.3 (May 8, 2000)
Features:

● Java Sound

● Jar Indexing

● Huge list of advancements for improving the Java area.

4. J2SE Version 1.4 (February 6, 2002)


Features:

● XML Processing

● Java Print Service

● Logging API

● Java Web Start

● JDBC 3.0 API

● Assertions

● API preferences

● IPv6 Support

5. J2SE Version 5.0 (September 30, 2004)

55
Features:

● Generics

● Enhanced for Loop

● Autoboxing/Unboxing

● Typesafe Enums

● Static Import

● Metadata (Annotations)

● Instrumentation

6. Java Version SE 6 (December 11, 2006)


Features:

● Scripting Language Support

● JDBC 4.0 API

● Java Compiler API

● Pluggable Annotations

● Java GSS, Kerberos and LDAP support

● Incorporated Web Services

● Many more improvements

56
7. Java Version SE 7 (July 28, 2011)
Features:

● Strings in switch Statement

● Type Inference for Generic Instance Creation

● Different Exception Handling

● Backing for Dynamic Languages

● Attempt with Resources

● Java NIO Package

● Binary Literals, underscore in literals

● Null Handling

8. Java Version SE 8 (March 18, 2014)


Features:

● Lambda Expressions

● Pipelines and Streams

● Date and Time API

● Default Methods

● Type Annotations

57
● Nashhorn JavaScript Engine

● Concurrent Accumulators

● Parallel operations

● TLS SNI

9. Java SE 9 (September 21, 2017)


Features:

● Modularization of the JDK under Project Jigsaw

● Given Money and Currency API

● Reconciliation with JavaFX

● Java usage of reactive streams

● More Concurrency Updates

10. Java SE 10 (March 20, 2018)


Features:

● Local Variable Type Inference

● Exploratory Java-Based JIT Compiler: This is the incorporation of the Graal dynamic compiler

for the Linux x64 stage.

58
● Time-sensitive Release Versioning

● Parallel Full GC for G1

● Garbage collector Interface

● Extra Unicode Language-Tag Extensions

● Root Certificates

● String Local Handshakes

● Remove the Native-Header Generation Tool – java

● Combine the JDK Forest into a Single Repository.

59

You might also like