Professional Documents
Culture Documents
Aim: Write a program on Basic Programming constructs like branching and looping.
1. A] Write a program to check if a number is even or odd and also to find the sum of the
individual digits of the same number.
1.B] Write a program to generate Fibonacci Series upto 10 numbers.
Objective: To apply basic programming constructs like Branching and Looping for solving
arithmetic problems.
Theory:
1. A)Branching- It also called decision-making statements they evaluate the Boolean
expression and control the program flow depending upon the result of the condition
provided.
B)Looping- It is a sequence of instruction s that is continually repeated until a certain
condition is reached.
2. The branching statements allow us to make a decision to which a block of statements
will be executes or not is depend on the Boolean expression true or false, whereas the
Looping statements are used to do some statements repeatedly util given condion is
true.
3. Types of Branching statements (write two line explanation for each type)
a. Simple if-To specify a block of code to be executed, if a specified condition is true.
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 1
b. If...Else-The Java if-else statement also tests the condition. It executes the if block if
condition is true otherwise else block is executed.
d. The Else If ladder-The if-else-if ladder statement executes one condition from
multiple statements.
e. The Switch Statement-The switch statement to select one of many code blocks to
be executed.
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 1
b. Do … while-The do/while loop is a variant of the while loop. This loop will
execute the code block once, before checking if the condition is true, then it will
repeat the loop as long as the condition is true.
c. For loop-We can initialize the variable, check condition and increment/decrement
value.
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 1
d. Nested For loop-If we have a for loop inside the another loop, it is known as
nested for loop. The inner loop executes completely whenever outer loop
executes.
i. Syntax
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 1
Algorithm:
Algorithm of Program 1:
STEP 1: START
STEP 2: Take input as number
STEP 3: Read number as N
STEP 4: Check if : N%2 == 0 then
output N is an Even number.
Else output N is a ODD number.
STEP 5: Store the modulus of N in any variable
STEP 6: Sum the remainder
STEP 7: Divide N with 10
STEP 8: Repeat STEP 2 while N>0
STEP 9: Output total sum of the remainder
STEP 10: EXIT
STEP 1: START
STEP 2: Take input Number from user as N
STEP 3: Put 0 and 1 value in two variables
STEP 4: Subtract 2 form N
STEP 5: Print 0 and 1
STEP 6: Add 1st and 2nd number, then shift 2nd to 1st and sum to 2nd
STEP 7: repeat STEP 6 for N times
STEP 8: Output the sum
STEP 9: EXIT
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 1
Implementation:
1. Program 1
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 1
2. Output:
3. Program 2
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 1
4. Output:
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 1
Conclusion: The Branching and Looping statements are very essential in many programming
languages. Also there are many programs that they utilize those statements like 1)To find n number
of sum 2)To find largest & smallest number, 3)Pattern printing etc.
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 2
Objective: To use the facility of java to read data from the keyboard for any program.
Theory:
1. Scanner class -The Java Scanner class is used to collect user input. Scanner is part of the
java.util package, so it can be imported without downloading any external libraries.
Scanner reads text from standard input and returns it to a program.
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 2
2. BufferedReader class – Java BufferedReader class is used to read the text from a
character-based input stream. It can be used to read data line by line by readLine()
method.
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 2
Implementation:
1. Program
Objective:To apply the concept of class and object to solve a real life problem like calculating
percentage of students.
Theory:
1. Classes -A class is a user defined blueprint or prototype from which objects are
created. It represents the set of properties or methods that are common to all objects of
one type.
Objects-An object is an instance of a class. A class is a template or blueprint from which
objects are created. So, an object is the instance(result) of a class.
2. Method- A method is a block of code or collection of statements or a set of code grouped
together to perform a certain task or operation.
Types of Methods:
There are two types of methods in Java:
1. Predefined Method
2. User-defined Method
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 3
Implementation:
Program :
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 3
Output:
Conclusion: Yes, we can execute a java program without a main method by using a static block.
Static block in Java is a group of statements that gets executed only once when the class is loaded
into the memory by Java ClassLoader, It is also known as a static initialization block. Static
initialization block is going directly into the stack memory.
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 4
Objective: To implement the concept of Method overloading and Constructor Overloading for
calculating area and getting information about the students.
Theory:
1. Method Overloading-If a class has multiple methods having same name but different in
parameters, it is known as Method Overloading.
2. Constructor Overloading-Constructor overloading in Java is a technique of having more
than one constructor with different parameter lists. They are arranged in a way that each
constructor performs a different task.
3. Method Overloading syntax:
Implementation:
1. Program
a)Method Overloading:
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 4
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 4
2. Output:
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 4
b)Constructor Overloading:
Program:
Output:
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 4
Conclusion:
Method overloading is a feature of Java in which a class has more than one method of the
same name and their parameters are different .
a) If the programmer has created a method in Java to perform some action, but later as part of the
requirement the programmer wants the same method to act on different parameters, then the coder
can simply use the feature of method overloading in Java. This will allow the programmer to
create the methods of performing similar functions with the same name so that they do not have to
remember the names later.
Constructor overloading in java is a technique of having more than one constructor with different
parameter lists.
a) As construction overloading enables the creation of the object of a specific class in several ways,
it is most commonly used in Java programs based on the requirement of the programmer. With the
use of constructor overloading, objects can be initialized with different data types .
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 5
Objective: To implement the concept of packages and subpackages to perform addition and
printing the statement.
Theory:
1. Packages-A java package is a group of similar types of classes, interfaces and sub-
packages.
2. Types of Packages-
1.built-in package
2. user-defined package.
3. Syntax for creating User-defined packages:
Write the package name at the top of every source file (classes, interface, enumeration, and
annotations).
Remember that there must be only one package statement in each source file.
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 5
Implementation:
1.Program
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 5
2.Output:
Conclusion: Java package is used to categorize the classes and interfaces so that they can be
easily maintained. Java package provides access protection. Java package removes naming
collision.
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 6
Theory:
1. An array is a data structure, which can store a fixed-size collection of elements of the
same data type. An array is used to store a collection of data, but it is often more useful to
think of an array as a collection of variables of the same type
2. Syntax for creation and declaration of Array
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 6
3. Strings in Java are Objects that are backed internally by a char array. Since arrays are
immutable(cannot grow), Strings are immutable as well. Whenever a change to a String
is made, an entirely new String is created. Also string is basically an object that
represents sequence of char values. An array of characters works same as Java string.
4. Syntax for creation and declaration of String
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 6
Implementation:
1. Program
a)
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 6
2.Output
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 6
b)Program
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 6
Output:
Conclusion:
2D array- The simplest of the multi-dimensional array is a two-dimensional array. A simple
definition of 2D arrays is: A 2D array is an array of one-dimensional arrays .In Java, a two-
dimensional array is stored in the form of rows and columns and is represented in the form of a
matrix.
String Function uses: String f are used in computer programming languages to manipulate
a string or query information about a string (some do both).
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 7
Theory:
1. Java StringBuffer class is used to create mutable (modifiable) String objects. The
StringBuffer class in Java is the same as String class except it is mutable i.e. it can be
changed.
2. StringBuffer Methods : apppend(), insert(), replace(), delete(), reverse(), capacity(),
length(), charAt(), etc.
3. Vector is like the dynamic array which can grow or shrink its size. Unlike array, we
can store n-number of elements in it as there is no size limit. It is a part of Java Collection
framework since Java 1.2. It is found in the java.util package and implements the List
interface, so we can use all the methods of List interface here.
4. Vector Methods: add(), contains(), isEmpty(), remove(), removeElement(), size(),
get(), delete(), capacity(), clear(), etc.
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 7
Implementation:
1. Program 1
2. Output 1
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 7
3. Program 2
4. Output 2
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 7
Conclusion: The significant performance difference between String & StringBuffer classes is that
StringBuffer is faster than String when performing simple concatenations. In String manipulation
code, character strings are routinely concatenated. Like an array, vector contains components that
can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed
to accommodate adding and removing items after the Vector has been created. Thus, both of them
have an upper edge over String and array, hence they’re useful in development
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 8
Theory:
1. Inheritance is the procedure in which one class inherits the attributes and methods of
another class. The class whose properties and methods are inherited is known as the
Parent class.
2. Syntax for single-level inheritance and multilevel-inheritance
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 8
Implementation:
1. Program
a)
Output
a)
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 8
1.Program
b)
Output
b)
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 8
Conclusion: When a class extends a class, which extends anther class then this is called multilevel
inheritance. For example class C extends class B and class B extends class A then this type of
inheritance is known as multilevel inheritance. Thus, we achieved multilevel inheritance.
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 9
Theory:
1. Interfaces -An interface in Java is a blueprint of a class. It has static constants and abstract
methods. The interface in Java is a mechanism to achieve abstraction. There can be only
abstract methods in the Java interface, not method body. It is used to achieve abstraction and
multiple inheritance in Java.
2. Use of Interfaces-There are mainly three reasons to use interface. They are given below.
Implementation:
1. Program
2.Output
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 9
Conclusion: It is the mechanism in java by which one class is allowed to inherit the
features(fields and methods) of another class. 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).
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 10
Theory:
1. Errors and Exception – In java, both Errors and Exceptions are the subclasses of
java.lang.Throwable class. Error refers to an illegal operation performed by the user which
results in the abnormal working of the program. Programming errors often remain undetected
until the program is compiled or executed. Some of the errors inhibit the program from
getting compiled or executed. Thus errors should be removed before compiling and
executing. It is of three types: Compile-time ,Run-time, Logical Whereas exceptions in java
refer to an unwanted or unexpected event, which occurs during the execution of a program i.e
at run time, that disrupts the normal flow of the program’s instructions
2. 2.Try-catch block and Multiple catch block syntax
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 10
Implementation:
1. Program
2. Output
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 10
Conclusion: Multiple catch block
A try block can be followed by one or more catch blocks. Each catch block must contain a different
exception handler. So, if you have to perform different tasks at the occurrence of different
exceptions, use java multi-catch block.
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 11
Theory:
1. User Defined Exceptions-User Defined Exception or custom exception is creating your
own exception class and throws that exception using ‘throw’ keyword. This can be done
by extending the class Exception. There is no need to override any of the above methods
available in the Exception class, in your derived class .
Implementation:
1. Program
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 11
2. Output
Conclusion: In the given program “MyExpception” is the User-Defined exception. The Catch
block is assigned to catch this exception. In simple word in the program it gives a condition is its
true then, it throws the user-defined exception i.e. MyException.
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 12
Theory:
Output
a)
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 12
b) Program
Output
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 12
Conclusion: In this Programs we have used public void run() Method which is used to
perform action for a thread.
Starting a thread:
The start() method of Thread class is used to start a newly created thread.
It performs the following tasks: of India | List of Prime Minister of India (1947-2020)
Theory:
1. Applet is a special type of program that is embedded in the webpage to generate the
dynamic content. It runs inside the browser and works at client side.
2. public void paint(Graphics g): is used to paint the Applet. It provides Graphics class
object that can be used for drawing oval, rectangle, arc etc
3. Syntax of Commonly used and implemented methods
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 13
Implementation:
1. Program
2. Output
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 13
Conclusion: Graphics class provides many methods to draw or fill different shapes, some of
those are drawString(), drawRect(), drawArc(), drawLine(), fillRect(), fillArc(), etc.
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 14
Theory:
1. Applet Life Cycle-
The applet life cycle can be defined as the process of how the object is created, started,
stopped, and destroyed during the entire execution of its application.
HTML CODE-
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 14
2. Output
Theory:
1. Introduction Java Swing-Java Swing is a part of Java Foundation Classes (JFC) that
is used to create window-based applications. It is built on the top of AWT (Abstract
Windowing Toolkit) API and entirely written in java.
2. Various components in java Swing- Unlike AWT, Java Swing provides platform-
independent and lightweight components. The javax.swing package provides classes for
java swing API such as Jbutton , JTextField, JTextArea, JRadioButton, JCheckbox,
JMenu, JColorChooser etc.
i. Jbutton- JButton class is used to create a push-button on the UI. The
button can contain some display text or image. It generates an event
when clicked and double-clicked. A JButton can be implemented in the
application by calling one of its constructors. Ii.
ii. Jlabel- JLabel class is used to render a read-only text label or images on
the UI. It does not generate any event.
iii. JTextField JTextField renders an editable single-line text box. A user can
input non-formatted text in the box. To initialize the text field, call its
constructor and pass an optional integer parameter to it. This parameter
sets the width of the box measured by the number of columns. It does not
limit the number of characters that can be input in the box.
iv. JTextArea JTextArea class renders a multi-line text box. Similar to the
JTextField, a user can input non-formatted text in the field. The
constructor for JTextArea also expects two integer parameters which
define the height and width of the text-area in columns. It does not restrict
the number of characters that the user can input in the text-area.
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 15
Implementation:
1. Program
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 15
2. Output
Department of Artificial Intelligence & Data Science
Subject: Object Oriented Programming with Java
Experiment No. 15
Conclusion: The page created takes information. The object of JLabel class is used to display
read only text like “ Name”. The JTextFeild class is used to take input from the user. Checkboxes
are created using JCheckBox class which helps the user to select one or many alternatives from
either male or female . Button is created using Jbutton to submit the informaton. The display of
textfield is due to the implementation of ActionListener.