Professional Documents
Culture Documents
Audit Course:
Java Programming
SY BTECH (2022-
23)
Course Champion
– Mr. S.A. Khandekar
Mrs. Sneha Pokharkar
Vision & Mission of MIT Academy of Engineering
&
Vision, Mission, PEOs, PO & PSO of E&TC Engg.
Vision & Mission of MIT Academy of Engineering
Vision
To develop MITAOE into a new-age learning center with an excellent ambiance for
academics and research conjugated with a vibrant environment for honing the extra and
curricular skills of all its stakeholders, to enable them to solve real-world problems and
bring a positive change in the society.
Mission
To leave no stone unturned in our endeavor to ensure that every alumnus looks back at us
and says MITAOE has not merely taught me, it has educated me.
To facilitate inclusive growth of all its student community and enabling them to be
leaders of tomorrow.
CO.2 Apply the concepts of classes, methods & inheritance to write Java L3
program.
Implement package , exception handling in Java.
CO.3 L3
32 10/04/23
Exception handling mechanism in Java
6.
This is to certify that, Mr. Atharv Mahesh Musale Roll No. 250 of SY B.Tech. (ETX/ENTC) has
completed the term work in the subject of Java Programming in the School of Electrical Engineering
of MIT Academy of Engineering,Alandi (D), Pune- 412105 during the academic year 2022-2023.
Rubrics for Assessment
Experiment No. : 1
Date : 17/02/2023
Write a program in Java which would involve creating a simple java program. Create class, have
data and method. Have a main class, create object and call the methods.
Objective :
The objective of this assignment is to learn classes, objects and methods in java.
Theory:
● Object - Objects have states and behaviors. Example: A dog has states - color, name, breed
as well as behaviors -wagging, barking, eating. An object is an instance of a class.
● Class - A class can be defined as a template/blue print that describes the
behaviors/states that object of its type support.
Objects in Java:
Let us now look deep into what are objects. If we consider the real-world we can find many objects
around us, Cars, Dogs, Humans, etc. All these objects have a state and behavior.
If we consider a dog, then its state is - name, breed, color, and the behavior is - barking, wagging,
running
If you compare the software object with a real world object, they have very similar characteristics.
Software objects also have a state and behavior. A software object's state is stored in fields and
behavior is shown via methods.
So in software development, methods operate on the internal state of an object and the object-to-
object communication is done via methods.
Classes in Java:
public class
Dog{ String
breed; int age;
S. Y. E&TC Programming in JAVA
String color;
void barking(){
}
void hungry(){
}
void sleeping(){
}
}
● Local variables: Variables defined inside methods, constructors or blocks are called local
variables. The variable will be declared and initialized within the method and the variable
will be destroyed when the method has completed.
● Instance variables: Instance variables are variables within a class but outside any method.
These variables are instantiated when the class is loaded. Instance variables can be accessed
from inside any method, constructor or blocks of that particular class.
● Class variables: Class variables are variables declared with in a class, outside any method,
with the static keyword.
A class can have any number of methods to access the value of various kinds of methods. In the
above example, barking(), hungry() and sleeping() are methods.
Constructors:
When discussing about classes, one of the most important sub topic would be constructors. Every
class has a constructor. If we do not explicitly write a constructor for a class the Java compiler
builds a default constructor for that class.
Each time a new object is created, at least one constructor will be invoked. The main rule of
constructors is that they should have the same name as the class. A class can have more than one
constructor.
S. Y. E&TC Programming in JAVA
Input:
Output:
FAQ:
References:
Conclusion:
1) Class serves as a blueprint for objects, encapsulating both data and behavior. Also class to store
information and methods to perform specific actions or computations on that data.By creating a
main class, we were able to instantiate objects of the defined class using the new keyword.
2) This allowed us to create specific instances of the class and access its methods to manipulate the
data or perform operations.By calling the methods on the object within the main class, we were
able to execute the desired functionality of the program. This demonstrated how objects can
interact with methods to perform tasks and process data.
Programming in Java
Experiment No. : 2
Date : 28/02/2023
Aim of Experiment:
Write some simple programs using methods in Java such as
i) To find factorial of number.
ii) To display first 50 prime numbers.
iii) To find sum and average of N numbers.
Objective :
The objective of this assignment is to learn simple programming concepts & exercise simple problem
statements based on methods.
Theory:
Factorial of any number is the product of an integer and all the integers below it for example
factorial of 4 is
4! = 4 * 3 * 2 * 1 = 24.
But 6 can be divided evenly by 1, 2, 3 and 6 so it is NOT a prime number (it is a composite
number).
Recursion in Java
Recursion in java is a process in which a method calls itself continuously. A method in java that calls
itself is called recursive method.
Syntax:
1. returntype methodname(){
2. //code to be executed
3. methodname();//calling same
method 4. }
Java Recursion Example 1: Infinite times
1. public class RecursionExample1 {
2. static void p(){
3. System.out.println("hello");
4. p();
5. }
6.
7. public static void main(String[] args) {
8. p();
9. }
10. }
Output:
hello
hello
...
java.lang.StackOverflowError
Input:
Output:
FAQ:
References:
Conclusion:
1) Methods facilitate code reuse. Once defined, a method can be called multiple times from
different parts of the program, eliminating the need to duplicate code and promoting efficient and
clean coding practices.
2) Allows methods to interact with other parts of the program, exchange data, and perform operations,
enhancing the program's functionality.
Programming in Java
Experiment No. : 3
Date : 09/03/2023
Aim of Experiment:
Write a program in Java to implement constructor and destructor by passing parameters in different
ways.
Objective:
The objective of this assignment is to learn the concepts of constructor and destructor.
Theory:
Introduction to Constructor and Destructor in Java
A constructor is something that initializes objects, and destructors are to destroy that initialization.
Java has automatic garbage collection, which used the mark and sweep’s algorithm.
class Employee {
Employee() {
}
}
If you see in the above example, we have not given any return type like int or void to the method,
which has the same name as a class name.
It is mainly used to initialize the object. When we are creating an object of a class, at that time,
constructor gets invoked.
1. Default Constructor
2. Parameterized Constructor
.
1. Default Constructor
This is the one type of constructor. By default, without any parameters, this constructor takes place.
This constructor does not have any parameters in it.
Example:
class Abc{
Abc( ){
System.out.println("This is the example of default constructor.");
}
}
2. Parameterized Constructor
As the name suggests, the parameterized constructor has some parameters or arguments at the time of
initializing the object.
Example:
import java.util.*;
class Square{
int width,height;
Square( int a , int b){
width = a;
height = b;
}
int area( ){
return width * height;
}
}
class Cal{
public static void main(String[] args){
{
Square s1 = new Square(10,20);
int area_of_sqaure = s1.area();
System.out.println("The area of square is:" + area_of_sqaure);
}
}
}
Destructor
Before start talking about the destructor, let me tell you there is no destructor in java. Destructor is in
C++ programming language. If we are talking about java, then java has a feature called automatic
garbage collector. Which free the dynamically allocated memory when there is no use. This concept is
very important, and you can explore more about this garbage collection in java.
Java uses the garb collection technique for memory allocation automatically. There is no need to
explicit use of destructors like C++. For allocating memory in java, we do not have a malloc function
like in C programming. The new operator in java does the same process of allocating memory.
new keyword allocates memory space for an object on heap memory. At the time of program
execution, a new keyword allocates some memory space for the object. End-user need to worry about
this as the program handles memory allocation. At the time when the object used in programs done
with the work, the memory used for the object is utilized for another task. This process of utilizing
memory efficiently is the job of garbage collection in java.
As we know, there is no destructor in java as it has finalize() method to do so. The following are some
of the key points to be noted.
Finalize() Methods
Finalize method is work like a destructor and opposite of a constructor, as we have seen
earlier.
Generally, the finalize method is used to remove the object.
For using this method, we have to define this method in java explicitly.
The finalize method starts working after garbage collection done with its work.
This simply means that after freeing memory space by deallocating memory space from
objects, there are chances that memory utilization still there with other things like fonts etc.,
to delete that memory space or to frees up that space, we make use of finalize() method.
Errors observed:
The constructor is undefined
Input:
Output:
FAQ:
Why do we need constructor and destructor in Java?
What are the different types of constructors?
References:
Conclusion:
1) Constructors are special methods used to initialize objects. They are invoked when an object is
created and are responsible for setting the initial state of the object. Constructors have the same
name as the class and do not have a return type.
2) On the other hand, Java does not have explicit destructors like some other languages. Instead, it
relies on automatic garbage collection. When an object is no longer reachable and no longer
referenced by any variables, the Java Virtual Machine (JVM) automatically reclaims the memory
occupied by that object.
Programming in Java
Experiment No. : 4
Date : 21/03/2023
Aim of Experiment:
Write a program in Java to implement function/operator overloading.
Objective:
The objective of this assignment is to learn the concepts of polymorphism.
Theory:
Syntax
Overloaded functions have the same name but different types of arguments or parameters assigned to
them. They can be used to calculate mathematical or logical operations within the number of assigned
variables in the method. The syntax of the overloaded function can be given below, where there are up
to N number of variables assigned.
Example #1
In coding example 1, we will see overloaded main(), which is used to show different outputs in the
panel and show how the overloaded main() is used in programming languages and how the calling of
the different functions produces different outputs, respectively.
Code:
When the main() is called, there is output which is shown significantly in the output panel as shown
below. First, the print statement of the main() is called and printed. Then, by distinguishing the number
of arguments, the program separates the two overloaded functions which are present in the piece of
code. The main() calls the first overloaded function, while the first overloaded function calls the
second overloaded function.
Example #2
In the second coding example, we are going to see functions that will perform multiplication but which
will have a different number of arguments assigned to them. This will help people in developing the
different functions which are meant for dealing with different arguments.
Code:
class Adder
{
static int multiply(int a,int b)
{
return a*b;
}
static int multiply(int a,int b,int c)
{
return a*b*c;}
}
}
class TestOverloading1
{
public static void main(String[] args)
{
System.out.println(Adder.multiply(110,110));
System.out.println(Adder.multiply(110,110,110));
}
}
Input:
Output:
FAQ:
What are the two types of function overloading in Java?
Can we overload a static method?
References:
Conclusion:
1) Overloading enhances code readability and reusability, as it allows programmers to use intuitive
and consistent naming conventions. By choosing the appropriate overloaded function or operator
based on the arguments provided, Java enables the creation of more expressive and efficient
programs.
Programming in Java
Experiment No. : 5
Date : 30/03/23
Aim of Experiment:
Write a program in Java to create a player class. Inherit the child classes from parent classes
Objective:
The objective of this assignment is to learn the concepts of inheritance in Java.
Theory:
Inheritance can be defined as the process where one object acquires the properties of another. With the
use of inheritance the information is made manageable in a hierarchical order.
When we talk about inheritance, the most commonly used keyword would be extends and implements.
These words would determine whether one object IS-A type of another. By using these keywords we
can make one object acquire the properties of another object.
IS-A Relationship:
IS-A is a way of saying : This object is a type of that object. Let us see how the extends keyword is
used to achieve inheritance.
public class Animal{
}
Input:
// Parent class
class Player {
protected Str ng name;
protected nt age;
// Ch ld class 1
class Cr cketPlayer extends Player {
pr vate Str ng team;
// Ch ld class 2
class FootballPlayer extends Player {
pr vate Str ng club;
Output:
FAQ:
What is is-a-relationship and has-a-relationship?
What is extends, interface?
References:
Experiment No. : 6
Date : 10/04/23
Aim of Experiment:
Write a Java program file handling using try catch block for exceptional handling.
Objective:
To study try and catch for exception handling.
Theory:
The exception handling in java is one of the powerful mechanism to handle the runtime errors so that
normal flow of the application can be maintained. In this page, we will learn about java exception, its
type and the difference between checked and unchecked exceptions.
What is an exception :
In java, exception is an event that disrupts the normal flow of the program. It is an object which is
thrown at runtime.
Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL,
Remote etc.
1. Checked Exception
2. Unchecked Exception
3. Error
The classes that extend Throwable class except RuntimeException and Error are known as checked
exceptions e.g.IOException, SQLException etc. Checked exceptions are checked at compile-time.
2) Unchecked Exception
The classes that extend RuntimeException are known as unchecked exceptions e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked
exceptions are not checked at compile-time rather they are checked at runtime.
3) Error
1. try
2. catch
3. finally
4. throw
5. throws
Java try block is used to enclose the code that might throw an exception. It must be used within the
method.
try{
//code that may throw exception
}finally{}
Input:
Output:
FAQ:
What is exception handling?
How to use try catch block?
When finally() method is called?
What is the difference between final, finally and finalize commands?
References:
Experiment No. : 7
Date : 21/04/2023
Aim of Experiment:
Write a Java program which imports user defined package and uses members of the classes contained
in the package.
Objective :
The objective of this assignment is to learn concept of packages in Java.
Theory:
A java package is a group of similar types of classes, interfaces and sub-packages. Package in java
can be categorized in two form, built-in package and user-defined package. There are many built-in
packages such as java, lang, awt, javax, swing, net, io, util, sql etc. Here, we will have the detailed
learning of creating and using user-defined packages.
1. //save as Simple.java
2. package mypack;
3. public class Simple{
4. public static void main(String args[]){
5. System.out.println("Welcome to package");
6. }
7. }
How to compile java package
If you are not using any IDE, you need to follow the syntax given below:
For example
1. javac -d . Simple.java
The -d switch specifies the destination where to put the generated class file. You can use any directory
name like /home (in case of Linux), d:/abc (in case of windows) etc. If you want to keep the package
within the same directory, you can use . (dot).
You need to use fully qualified name e.g. mypack.Simple etc to run the class.
There are three ways to access the package from outside the package.import package.*;
1. import package.classname;
2. fully qualified name
3. Input:
Output:
FAQ:
How package is used?
What is way to access to package?
What is sub-package?
What is the importance of protected and default access specifier in relation to package.
References:
Conclusion:
1) A package is a way to organize related classes, interfaces, and other resources. It provides a
mechanism to group classes into logical units, making it easier to manage and organize large
codebases.
2) Packages help avoid naming conflicts by providing a unique namespace for classes. They also
promote code reusability and modularity by allowing classes to be encapsulated and accessed only
within the package or by classes explicitly granted access through modifiers like public or
protected.
Programming in Java
MIT Academy of Engineering, Alandi, Pune 412105
Department of Electronics & Telecommunication Engineering
Experiment No. : 8
Date : 09/05/23
Aim of Experiment:
Write a program in java to implement interface using suitable example.
Objective :
The objective of this assignment is to learn the concept of interface and its implementation.
Theory:
An interface is a reference type in Java. It is similar to class. It is a collection of abstract
methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
Along with abstract methods, an interface may also contain constants, default methods, static methods,
and nested types. Method bodies exist only for default methods and static methods.
Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of
an object. And an interface contains behaviors that a class implements.
Unless the class that implements the interface is abstract, all the methods of the interface need to be
defined in the class.
An interface is written in a file with a .java extension, with the name of the interface matching the name of
the file.
Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that
matches the package name.
An interface cannot contain instance fields. The only fields that can appear in an interface must be declared
both static and final.
Declaring Interfaces
The interface keyword is used to declare an interface. Here is a simple example to declare an
interface −
Example
Following is an example of an interface −
An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface.
Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
Example
/* File name : Animal.java
*/ interface Animal {
public void eat();
public void
travel();
Implementing Interfaces
When a class implements an interface, you can think of the class as signing a contract, agreeing to
perform the specific behaviors of the interface. If a class does not perform all the behaviors of the
interface, the class must declare itself as abstract.
A class uses the implements keyword to implement an interface. The implements keyword appears in
the class declaration following the extends portion of the declaration.
Example
/* File name : MammalInt.java */
public class MammalInt implements Animal {
public void eat() {
System.out.println("Mammal eats");
}
public void travel() {
System.out.println("Mammal travels");
}
public int noOfLegs() {
return 0;
}
public static void main(String args[]) {
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
Output
Mammal eats
Mammal
travels
When overriding methods defined in interfaces, there are several rules to be followed −
Checked exceptions should not be declared on implementation methods other than the ones declared by the
interface method or subclasses of those declared by the interface method.
The signature of the interface method and the same return type or subtype should be maintained when
overriding the methods.
An implementation class itself can be abstract and if so, interface methods need not be implemented.
A class can extend only one class, but implement many interfaces.
An interface can extend another interface, in a similar way as a class can extend another class.
Extending Interfaces
An interface can extend another interface in the same way that a class can extend another class.
The extends keyword is used to extend an interface, and the child interface inherits the methods of
the parent interface.
Example
// Filename: Sports.java
public interface Sports
{
public void setHomeTeam(String name);
public void setVisitingTeam(String name);
}
// Filename: Football.java
public interface Football extends Sports {
public void homeTeamScored(int points);
public void visitingTeamScored(int points);
public void endOfQuarter(int quarter);
}
// Filename: Hockey.java
public interface Hockey extends Sports {
public void homeGoalScored();
public void visitingGoalScored();
public void endOfPeriod(int period);
public void overtimePeriod(int ot);
The Hockey interface has four methods, but it inherits two from Sports; thus, a class that implements
Hockey needs to implement all six methods. Similarly, a class that implements Football needs to
define the three methods from Football and the two methods from Sports.
The extends keyword is used once, and the parent interfaces are declared in a comma-separated list.
For example, if the Hockey interface extended both Sports and Event, it would be declared as −
Example
public interface Hockey extends Sports, Event
Input:
Output:
Application:
Abstraction and Polymorphism
API Design
Multiple Inheritance of Type
FAQ:
What is the advantage of implementing interface?
How to implement multiple inheritance using
interface
References:
Conclusion: