Lab Manual OOP-1
Lab Manual OOP-1
COURSE DESCRIPTION
Course Title: Object Oriented Programming
Pre-requisite: none
2. Java: How to Program, 5/e, Deitel and Deitel, Prentice Hall, 0131016210/
0131202367 International Edition.
5 Task 5 Function/Method
6 Task 6 Constructor/Destructor
7 Task 7 Abstraction
8 Task 8 Encapsulation
9 Task 9 Polymorphism
10 Task Inheritance
10
12 Task Overloading
12
13 Task Overriding
13
Final Exam
Installing Java Extension Pack in VS
Code:
Install VS Code:
Download and install Visual Studio Code from https://code.visualstudio.com/.
Open VS Code:
Open VS Code on your computer.
Install Java Extension Pack:
Go to the Extensions view by clicking on the Extensions icon in the Activity
Bar on the side of the window (or use Ctrl+Shift+X).
Search for "Java Extension Pack" and install it.
Install Java Development Kit (JDK):
Ensure that you have a JDK installed on your system.
Set the JAVA_HOME environment variable to point to the JDK installation
directory.
Create a Java Project:
Open the Command Palette (Ctrl+Shift+P) and type "Java: Create Java
Project."
Follow the prompts to create a new Java project.
Write a Basic Java Program:
Open the created project.
Inside the src folder, create a new Java file (e.g., HelloWorld.java).
Write a simple "Hello World" program.
SOURCE CODE:
OUTPUT :
Hello World
ffjjjf
}
OUTPUT:
1010010100010001000.111100111011011011011
1010010100010001000.1111001110110
11011011
This Java program is similar to the previous one, but instead of using
System.out.print(), it uses System.out.println() for each variable. The println
method appends a newline character after printing, so each value will be on a new
line
SOURCE CODE:
10
100
10
1000
Dividing each by 8 gives the size in bytes. The program then prints these sizes with
appropriate labels.
SOURCE CODE :
SOURCE CODE:
Enter the value of a:
10
20
30
SOURCE CODE:
Enter the value of a:
10
20
200
Enter the value of a:
10
20
SOURCE CODE :
:
Enter the value of a:
10
20
10
SOURCE CODE :
10
20
0
Enter the value of a:
10
Sum: 13
Difference: 7
Product: 30
Quotient: 3
1. Assignment Operators:
= (Assignment): Assigns the value on the right to the variable on the left.
+=, -=, *=, /=, %= (Compound Assignment): Performs the operation and
assigns the result to the left operand.
SOURCE CODE :
Output 10
OUTPUT : 2
OUTPUT :
The value of X is 10
OUTPUT = 2
Source code :
Output = 30
Output =10
Output =200
Output = 2
Output = 0
AIRTHMATIC OPERATOR :
Output 10 .
Source code
s Output : 200
Output 2 .
Output : 0 .
2. Relational Operators:
== (Equal to): Checks if two values are equal.
!= (Not equal to): Checks if two values are not equal.
> (Greater than): Checks if the left operand is greater than the right
operand.
< (Less than): Checks if the left operand is less than the right operand.
>= (Greater than or equal to): Checks if the left operand is greater than
or equal to the right operand.
<= (Less than or equal to): Checks if the left operand is less than or equal
to the right operand.
Output : The a is greater
OUTPUT =
3. Logical Operators:
&& (Logical AND): Returns true if both conditions are true.
|| (Logical OR): Returns true if at least one of the conditions is true.
! (Logical NOT): Returns true if the condition is false, and vice versa
Output =
Output = login
Output = logout
Output = 9
TERNARY OPERATOR :
OUTPUT = b is greater
OUTPUT =
STRING:
In Java, a String is a class that represents a sequence of characters. It is one of the
built-in classes in Java, and it is widely used to manipulate and represent textual
data. Strings in Java are immutable, meaning that their values cannot be changed
after they are created. This immutability property makes strings safe for use in
various applications, such as multithreading.
Output= humna
Output = humnaallauddin
Output = Enter a string Humna
Humna
Output =
enter string
Hello, Java!
12
. The trim() method removes those spaces, and the program then prints the
modified string,
Output = enter
string
Hello, Java!
Hello, Java!
Hello, Java!
hello, java!
Output= enter string
Hello, Java!
HELLO, JAVA!
Java Programming
1
Output =
FUNCTION / METHOD
returnType: The data type of the value the method returns. It can be a
primitive type, an object, or void if the method does not return anything.
methodName: The name of the method, which is used to call it.
parameterType1, parameterType2, ...: The data types of the parameters
that the method accepts (if any).
parameterName1, parameterName2, ...: The names of the parameters
used within the method body.
Method body: The block of code that defines the behavior of the method.
return returnValue;: The return statement is used to return a value from
the method. The data type of the value must match the method's declared
return type.
main method: The main method is the entry point of a Java program. It is
called when the program starts executing
Output:
Square of 5 is: 25
Output =
Output = 10
Ambiguous Bcz ssme name
Output = Value of x: 46
Value of y: 46
GLOBAL VARIABLE :
Output = The value of x after the loop: 56
ARRAY:
Output = 20
Output = 20
Output =
3
4
10
11
12
13
14
15
Default Constructor:
Automatically generated if no constructor is defined.
Takes no parameters.
Initializes the object with default values.
Parameterized Constructor:
Takes parameters to initialize the object with specific values.
Output = Make: Toyota
Model: Camry
Year: 2022
Overloading Constructor :
10
12
DESTRUCTORS:
A destructor is another special method or function that is called when an object
goes out of scope or is explicitly deleted. It is used to release resources, such as
memory, that were allocated for the object during its lifetime. Destructors have the
same name as the class preceded by a tilde (~), and they do not take any
parameters.
ABSTRACTION :
bstraction is one of the four fundamental Object-Oriented Programming
(OOP) principles, along with encapsulation, inheritance, and polymorphism.
Abstraction refers to the concept of hiding the complex implementation
details and showing only the essential features of an object. In Java,
abstraction can be achieved through abstract classes and interfaces.
Abstract Classes:
An abstract class is a class that cannot be instantiated on its own and
may contain abstract methods.
Abstract methods are declared without providing an implementation in
the abstract class.
Abstract classes can also have concrete methods (methods with an
implementation).
To declare an abstract class, you use the abstract keyword.
Source code :
Output = running safely
Output =
Welcome to Campus Management Solution Portal
The UserName is :Zaid
Inheritence :
Inheritance is a fundamental concept in object-oriented programming
(OOP) that allows a class (subclass/child class) to inherit properties and
behaviors from another class (superclass/parent class). In Java, inheritance is
implemented using the extends keyword.
Key Points:
Syntax:
Types of Inheritance:
Java supports both single and multilevel inheritance.
Single Inheritance: A class can inherit from only one superclass.
Multilevel Inheritance: A class can inherit from a superclass, and
another class can inherit from the derived class.
Polymorphism
Polymorphism in Java is a fundamental concept in object-oriented
programming (OOP) that allows objects to be treated as instances of their
parent class rather than their actual class. Polymorphism can be achieved
through method overloading and method overriding.
Output =
Example code :
Overloading:
Explanation:
Overriding :
Explanation:
THE END