JavaLab Expt 1 To 12 - 1677738240

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 15

Programming Practices (Java) CS 406 1

LAB MANUAL
Java (CS-406)

IV Sem (CSE)

CHAMELI DEVI GROUP OF


INSTITUTIONS, INDORE

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF COMPUTER SCIENCE AND ENGG.
Programming Practices (Java) CS 406 2

CHAMELI DEVI GROUP OF INSTITUTIONS


INDORE (M.P.)

Department of
Computer Science and Engineering

CERTIFICATE

This is to certify that Mr./Ms……………………………………………………………… with RGTU Enrollment


No. 0832CS……….………has satisfactorily completed the course of experiments in Programming Practices
(Java) CS-406 laboratory, as prescribed by Rajiv Gandhi Proudhyogiki Vishwavidhyalaya, Bhopal for IV
Semester of the Computer Science and Engineering Department during year 2022  23

Signature of
Faculty In-charge

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF COMPUTER SCIENCE AND ENGG.
Programming Practices (Java) CS 406 3

INDEX

Expt Name of the Experiment Date of Signature


. No. Conduction of Faculty-
in-Charge

1 Installation of J2SDK.

2 Write a program to show scope of variables.

3
Write a program to show concept of class in Java.

4
Write a program to show ‘type casting’ in Java.

5 Write a program to show exception handling in Java.

6
Write a program to show inheritance.

7 Write a program to show polymorphism.

8 Write a program to show access specifies (public, private, protected)


in Java.
9 Write a program to implement constructor in Java.

10 Write a program to show interfacing between two classes.

11 Write a program to add a class to a package.

12 Write a program to hide a class.

EXPT. No. – 1. Installation of J2SDK.

Aim: To understand the installation process of J2SDK.

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF COMPUTER SCIENCE AND ENGG.
Programming Practices (Java) CS 406 4

Theory:
Installation Instructions for J2SDK
Run the self-installing executable to unpack and install the J2SDK software bundle. As part of the J2SDK, this
installation includes the Java Plug-in and Java Web Start, as well as an option to include the public Java 2
Runtime Environment.

System Requirements
Software - Java 2 SDK Standard Edition, 1.4.2 is supported on i586 Intel and 100% compatible platforms
running Microsoft Windows. For a list of supported operating systems and desktop managers, see System
Configurations.

Setting the PATH variable


Set the PATH variable if you want to be able to conveniently run the Java 2 SDK executables (javac.exe,
java.exe, javadoc.exe, etc.) from any directory without having to type the full path of the command. If you don't
set the PATH variable, you need to specify the full path to the executable every time you run it, such as:

C:> \j2sdk1.4.2_\bin\javac MyClass.java

It's useful to set the PATH permanently so it will persist after rebooting.

To set the PATH permanently


Choose Start, Settings, Control Panel, and double-click System. On Microsoft Windows NT, select the
Environment tab; on Microsoft Windows 2000 select the advanced tab and then Environment Variables. Look
for "Path" in the User Variables and System Variables. If you're not sure where to add the path, add it to the right
end of the "Path" in the User Variables.

A typical value for PATH is: C:\j2sdk1.4.2_\bin


Capitalization doesn't matter. Click "Set", "OK" or "Apply".

Viva Questions:
1) What is SDK?
2) Define JRE.
3) Define JVM.
4) What are the different features of java?
5) What is the difference between Java and C++?
EXPT. No. – 2. Write a program to show scope of variables.

Aim: To understand the concept of local and global variables in Java.

Theory:

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF COMPUTER SCIENCE AND ENGG.
Programming Practices (Java) CS 406 5
Variables are a place where information can be stored while a program is running. The value can be changed at
any point in the program. To create a variable, we must give it a name and identify what type of information it
will store. We can also give an initial value to variable at the same time we create it.

There are three kinds of variables in Java:


1. Instance variables 2. Class variables 3. Local variables.

1. Instance variables, are used to define an object's attributes.


2. Class variables define the attributes of an entire class of objects and apply to all instances of it.
3. Local variables are used inside method definitions, or even smaller blocks of statements within a method.
They can be used only while the method or block is being executed by the Java interpreter and they cease to exist
afterward.

We can divide scope into:


1. Method body scope variable is accessible in method body only (local variables, parameters).
2. Class definition scope variable is accessible inside class definition (instance variables).

Viva Questions:
1) What is the difference between local and global variable?
2) Explain static variable.
3) What is instance variable?
4) What is the difference between constant and variable?
5) What is final variable?

EXPT. No. – 3. Write a program to show concept of class in Java.

Aim: To understand the concept of class in Java.

Theory:
Class- A class can be defined as a template/blueprint that describes the behavior/state that the object of its type
supports.

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF COMPUTER SCIENCE AND ENGG.
Programming Practices (Java) CS 406 6
Instance variables- It store the state of the object. An instance variable is a variable that is defined in a class, but
outside of a method. Each class would have its own copy of the variable. Every object has a state that is
determined by the values stored in the object. An object is said to have changed its state when one or more data
values stored in the object have been modified.

Class variable or static variable- It is defined in a class, but there is only one copy regardless of how many
objects are created from that class. It's common to define static final variables (constants) that can be used by all
methods, or by other classes.

Final Variable, Methods and Classes- In Java we can mark fields, methods and classes as final. Once marked
as final, these items cannot be changed. Variables defined in an interface are implicitly final. You can't change
value of a final variable (is a constant). A final class can't be extended i.e.; final class may not be subclasses.

Viva Questions:
1) What is final class?
2) What is static class?
3) Explain class variables.
4) Can a variable be local and static at the same time?
5) Can a class in Java be inherited from more than one class?

EXPT. No. – 4. Write a program to show ‘type casting’ in Java.

Aim: To understand the concept of type casting in Java.

Theory:
Type casting: Type casting refers to changing an entity of one data type into another. It means treating a
variable of one type as though it is another type.
Type casting is of two types:
1. Widening/Unboxing/Implicit typecasting: Converting from smaller data types into larger data types.
2. Narrowing/Boxing/Explicit typecasting: Converting from Larger data types into smaller data types.

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF COMPUTER SCIENCE AND ENGG.
Programming Practices (Java) CS 406 7
Casting in Java is safer than in C or other languages that allow arbitrary casting. Java only lets casts occur
when they make sense, such as a cast between a float and an int.
When we perform type casting as shown below from left to right, automatic conversion occurs.

Byte -> short ->int-> long -> float -> double


Viva Questions:
1) Explain type casting.
2) Explain the process of calling non-static method?
3)How can we use primitive data types as objects?
4) What is What is explicit type casting implicit type casting ?
5) ?

EXPT. No. – 5. Write a program to show exception handling in Java.

Aim: To understand the concept of run time errors.

Theory:
An exception (or exceptional event) is a problem that arises during the execution of a program. When an
exception occurs the normal flow of the program is disrupted and the program/application terminates
abnormally, which is not recommended, therefore, these exceptions are to be handled. An exception can occur
for many different reasons. Following are some scenarios where an exception occurs.
 A user has entered an invalid data.
 A file that needs to be opened cannot be found.

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF COMPUTER SCIENCE AND ENGG.
Programming Practices (Java) CS 406 8
 A network connection has been lost in the middle of communications or the JVM has run out of memory.

Checked exceptions − A checked exception is an exception that occurs at the compile time, these are also called
as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation; the
programmer should take care of (handle) these exceptions.

Unchecked exceptions − An unchecked exception is an exception that occurs at the time of execution. These are
also called as runtime exceptions. These include programming bugs, such as logic errors or improper use of an
API. Runtime exceptions are ignored at the time of compilation.

Viva Questions:
1) Explain exception.
2) Which types of exceptions are caught at compile time?
3) How can an exception be thrown manually by a programmer?
4) What happens if an exception is not handled in a program?
5) What is checked and unchecked exception?

EXPT. No. – 6. Write a program to show inheritance.

Aim: To understand the concept of inheritance in Java.

Theory:
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of parent object.
The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When
you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new
methods and fields also. Inheritance represents the IS-A relationship, also known as parent-child relationship.

Why use inheritance in java


 For Method Overriding (so runtime polymorphism can be achieved).
 For Code Reusability.

Viva Questions:
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF COMPUTER SCIENCE AND ENGG.
Programming Practices (Java) CS 406 9
1) Explain different types of inheritance.
2) What is ‘IS-A’ relationship?
3) What are the benefits of inheritance?
4) Explain super class and base class.
5) Can we override static methods of a class?

EXPT. No. – 7. Write a program to show polymorphism.

Aim: To understand the concept of polymorphism.

Theory:
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP
occurs when a parent class reference is used to refer to a child class object.

There are three distinct forms of Java polymorphism:


 Method overloading (Compile time polymorphism)
 Method overriding through inheritance (Run time polymorphism)
 Method overriding through the Java interface (Run time polymorphism)
Polymorphism allows a reference to denote objects of different types at different times during execution. A super
type reference exhibits polymorphic behavior, since it can denote objects of its subtypes.

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF COMPUTER SCIENCE AND ENGG.
Programming Practices (Java) CS 406 10

Viva Questions:

1) What is polymorphism?
2) How polymorphism implemented in Java?
3) What is interface?
4) What is difference between abstract class and interface?
5) What is marker interface?
EXPT. No. – 8. Write a program to show access specifies (public, private, protected) in Java.

Aim: To understand the concept of access specifiers in Java.

Theory:
Java provides a number of access modifiers to help you set the level of access you want for classes as well as the
fields, methods and constructors in your classes. A member has package or default accessibility when no
accessibility modifier is specified. Types of access specifiers:
1) Public
2) Private
3) Protected
4) Default (with in directory or same package)

1) Public access modifier: Fields, methods and constructors declared public (least restrictive) within a
public class are visible to any class in the Java program, whether these classes are in the same package or
in another package.

2) Private access modifier: The private (most restrictive) fields or methods cannot be used for classes and
Interfaces. It also cannot be used for fields and methods within an interface. Fields, methods or
constructors declared private are strictly controlled, which means they cannot be accesses by anywhere
outside the enclosing class.

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF COMPUTER SCIENCE AND ENGG.
Programming Practices (Java) CS 406 11
3) Protected access modifier: The protected fields or methods cannot be used for classes and Interfaces. It
also cannot be used for fields and methods within an interface. Fields, methods and constructors declared
protected in a superclass can be accessed only by subclasses in other packages. Classes in the same
package can also access protected fields, methods and constructors as well, even if they are not a subclass
of the protected member's class.

4) Default (Package) access modifier: Java provides a default specifier which is used when no access
modifier is present. Any class, field, method or constructor that has no declared access modifier is
accessible only by classes in the same package (with in directory). The default modifier is not used for
fields and methods within an interface.

Viva Questions:

1) What are the different types of access specifiers?


2) Why runnable interface is used in Java?
3) Explain uses of private access modifier.
4) How can we make copy of a Java object?
5) Explain default access modifier.
EXPT. No. – 9. Write a program to implement constructor in Java.

Aim: To understand the concept of constructor in Java.

Theory:
Constructor: A Java constructor has the same name as the name of the class to which it belongs. Constructor's
syntax does not include a return type, since constructors never return a value.

Constructors may include parameters of various types. When the constructor is invoked using the new operator,
the types must match those that are specified in the constructor definition. Java provides a default constructor
which takes no arguments and performs no special actions or initializations, when no explicit constructors are
provided.

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF COMPUTER SCIENCE AND ENGG.
Programming Practices (Java) CS 406 12

Viva Questions:

1) Explain constructor.
2) What are the different types of constructor?
3) What is the difference between constructor and destructor?
4) What is finalize method?
5) Define garbage collection.
EXPT. No. – 10. Write a program to show interfacing between two classes.

Aim: To understand the concept of interface between two classes.

Theory:
Interface looks like a class but it is not a class. An interface can have methods and variables just like the class but
the methods declared in interface are by default abstract (only method signature, no body). Also, the variables
declared in an interface are public, static & final by default.

As mentioned above they are used for full abstraction. Since methods in interfaces do not have body, they have
to be implemented by the class before you can access them. The class that implements interface must implement
all the methods of that interface. Also, java programming language does not allow you to extend more than one
class, however you can implement more than one interface in your class.

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF COMPUTER SCIENCE AND ENGG.
Programming Practices (Java) CS 406 13

Viva Questions:

1) Define an interface?
2) Can an interface be extended by another interface in Java?
3) Explain comparable interfaces.
4) Explain abstraction.
5) What is abstract class?
EXPT. No. – 11. Write a program to add a class to a package.

Aim: To understand the concept of package in Java.

Theory:
In order to add Java classes to packages, we can do two things:

1. Put the Java source file inside a directory matching the Java package.
2. Declare that class as part of the package.

Just create a source root directory, and inside that, create directories for each package and sub package
recursively. Put the class files into the directory matching the package you want to add it to.

When you have put your Java source file into the correct directory (matching the package the class should belong
to), you have to declare inside that class file, that it belongs to that Java package.

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF COMPUTER SCIENCE AND ENGG.
Programming Practices (Java) CS 406 14

Viva Questions:

1) Define packages in Java.


2) How to create a package in Java?
3) What are the advantages of packages?
4) What is the base class of all classes?
5) Which package is always imported by default?
EXPT. No. – 12. Write a program to hide a class.

Aim: To understand the concept hiding classes in Java.

Theory:
When we import a package within a program, only the classes declared as public in that package will be made
accessible within this program. In other words, the classes not declared as public in that package will not be
accessible within this program. We shall profitably make use of the above fact. Sometimes, we may wish that
certain classes in a package should not be made accessible to the importing program. In such cases, we need not
declare those classes as public. When we do so, those classes will be hidden from being accessed by the
importing class.

Viva Questions:
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF COMPUTER SCIENCE AND ENGG.
Programming Practices (Java) CS 406 15

1) Explain class hiding concept in Java.


2) What is static import?
3) Can we import same package/class twice?
4) Compare Java IO and NIO packages.
5) Should package be the first statement in a Java class?

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF COMPUTER SCIENCE AND ENGG.

You might also like