Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Object Oriented Programming Language I

SECE OOP BY | YOHANNES RETA


Student Version

Lab Manual Intermediate Level


Syllabus OOP
Object Oriented Programming

 Declaration, operator…..
 This. Keyword
 Scanners
 Modifiers
 Loops (Iteration)
 Encapsulation
 Constructor
 Method
 Derived Class Constructor
 Static and Public
 Arrays
 Void Method
 Class and Object
 Value Returning-Method
 Inner Class
 The Return Keyword
 Inheritance
 Interfaces
 Abstraction
 File Handling
 Polymorphism
 ArrayList, Linkedlist
 Exception Handling
 Math Class, Time Class
 Pointers
prepared by| YOHAN.R
Lab 1
OOP
Variable And Declaration

Tasks
❑ Display Variable (txt, Number)
❑ Assign & change a Value
❑ Variable Declaration
❑ Using Concatenation

A Variable Must be Specify A Data Type


(Eg, int, float, char, boolean, String)

prepared by| YOHAN.R


❑ Type Casting
Understanding
Lab 2
❑ Float & Double OOP
❑ Operators
By Y.R

1 4

Type casting is when you assign a value of


one primitive data type to another type.
2
Arithmetic Operator Comparison Operators

3
Logical Operators

prepared by| YOHAN.R


Lab 2.1
OOP
Conditions and If Statements By Y.R

You Already Know the java supports the usual logical conditions from mathematics Like >, <, <=, >=, ==, !=
if Statement Syntax = if (condition) { // block of code to be executed if the condition is true }
2 3
1

4
Ex

Ex

#Shorthand if-else (Ternary Operator)


Syntax = Variable (Condition) ? Expression True: Expression False;
int time = 12;
String result=(time<6)? Morning" . "Afternoon";
prepared by| YOHAN.R
System.out.println(result);
Lab 2.2
OOP
Switch Statements
By Y.R

Instead of writing many if..else statements, you can use the switch statements 1
1. Switch Evaluate once
2. The value of expression is compared with the value of each case
3. If there is a match the associated block of code is Executed.
4. The break and default keyword is optional.

When Java reaches a break keyword, it breaks out of the switch block.
The default keyword specifies some code to run if there is no case match: prepared by| YOHAN.R
Lab Quiz
OOP

By Y.R

Quiz (5-marks)

1. Create a class “Lab2” and a variable named Animal and Assign a Value “Elephant” to it ___ ___=_____:
2. Fill the missing parts to create 3 variables and display the output of the sum | int =5 y= 6 z=60
3. Fill the Related Data types and Display The output
4. Write to Display the ff Output = Salary = 2500 3
5. Nested If Declare & assign value 20, 60 age and weight , respectively
and if greater than that display The ff output
“You are Eligible to Donate a blood”

prepared by| YOHAN.R


Lab 3
OOP
Loops (Iterations) By Y.R

Loops : Used To Iterates Program Several Times.


save time, reduce errors, and they make code more readable.
1 2

Do-While : Execute Then Check The Condition.


While : Check The conditions then Execute.

For : will only print even values between 0 and 10:


Lab Quiz 2
OOP

By Y.R

1. Display The FF Output Pyramid output Using For each loop. 1


2. Write a Program that Declare int id and String Name , Create With a Default
Constructor and Display 0, null Values

prepared by| YOHAN.R


Lab 3 OOP

By Y.R

 A Class & Multiple Class


 An Object & Multiple Object
 Constructor (Default & Parametrized)

prepared by| YOHAN.R


OOP

class in java By Y.R

A class is a group of objects that has common properties. It is a template or blueprint from which objects are created.

Instance variable in Java


A class in java can contain
❑ data member A variable that is created inside the class but outside the method, is
❑ Method known as instance variable.Instance variable doesn't get memory at
❑ Constructor compile time.It gets memory at runtime when object(instance) is
❑ Block created. That is why, it is known as instance variable
❑ class and interface
Components of Java Classes
1.Modifiers: A class can be public or has default access (Refer this for details).
2.Class keyword: class keyword is used to create a class.
3.Class name: The name should begin with an initial letter (capitalized by convention).
4.Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the
keyword extends.
A class can only extend (subclass) one parent.
5.Interfaces(if any): A comma-separated list of interfaces implemented by the class,
if any, preceded by the keyword implements. A class can implement more than one interface.
6.Body: The class body is surrounded by braces, { }.

Using Multiple Class: one class has all Attributes & Method. The other hold the main method (Code to be Executed)
prepared by| YOHAN.R
object in java Lab 3.1
OOP

By Y.R

1
Object is an instance of a class. Class is a template or blueprint
from which objects are created. So object is the instance(result) of a
class.
An object has three characteristics
state: represents data (value) of an object.
behavior: represents the behavior (functionality) of an object
such as deposit, withdraw etc.
identity: Object identity is typically implemented via a unique ID. Multiple Object
The value of the ID is not visible to the external user. But,it is used 2
internally by the JVM to identify each object uniquely.

Class Object Attribute Method


Volvo Weight Drive
Car Toyota Color Break
Vitz
Declaring Objects (Also called instantiating a class)

For Example: Pen is an object. Its name is Reynolds, color is white etc.
known as its state. It is used to write, so writing is its behavior. prepared by| YOHAN.R
Lab 3.2
OOP
Multiple Class By Y.R

| Tips
You can create multiple objects of one class. You can also create an object of a class and access it in another class.
This is often used for better organization of classes (one class has all the attributes and methods,
while the other class holds the main() method (code to be executed)).

| Create Project“ Address”


1 Contact.class 2 ContactTest.class

|Remember that the name of the java file should match the class name. In this example,
we have created two files in the same directory/folder:

•MyClass.java
•OtherClass.java
prepared by| YOHAN.R
OOP
constructor in java By Y.R

D/c B/n Constructor And Method in Java


Constructor Method
Constructor is used to initialize the state of an object. Method is used to expose behaviour of an object.

Constructor must not have return type. Method must have return
(no-arg type.
constructor)

Constructor is invoked implicitly. Method is invoked explicitly.


The java compiler provides a default constructor if
Method is not provided by compiler in any case.
you don't have any constructor.
Method name may or may not be same as class
Constructor name must be same as the class name.
name.

prepared by| YOHAN.R


| Rules

Lab 3.3
OOP
Constructor Constructor Name Must be.
Constructor Must’ve no Explicit Return Type.
By Y.R

Constructor: a special Type of method used to initialize The objects.

| Default-Constructor | Parametrized-Constructor
1 2

If there is No Constructor Compiler Automatically


Create a default Constructor.
| Purpose
C 0 & NULL Value Depending on Type.
Provide a d/t Value to the distinct of objects.
You can have as many parameters as you want:
prepared by| YOHAN.R
Lab 4 OOP

By Y.R

 Arrays
 Packages/Scanner/Input
 Methods
 Modifiers (Private , Public)
 Method Call, Void, Return Keyword
 Value Returning Method

Tips Running a File For A Specific Project

prepared by| YOHAN.R


Java Arrays OOP

By Y.R

1
Arrays are used to store multiple values in a single variable,
instead of declaring separate variables for each value.

Declare
Access

3 Multidimensional Arrays Change

Length

2 Loop Through an Array


For each Loop

To access the elements of the myNumbers array,


specify two indexes: one for the array, and one for the
element inside that array.
This example accesses the third element (2) in the second
array (1) of myNumbers: prepared by| YOHAN.R
Lab 4
OOP
Packages/ Import Class /Input (Scanner) By Y.R

Packages Used to group related class. Think of it as a folder in a file directory. We use Packages are divided into two categories:
packages to avoid name conflicts, and to write a better maintainable code ➢ Built-in Packages (packages from the Java API)
➢ User-defined Packages (create your own packages).

1. Built-in Packages
The library is divided into packages and classes. Meaning you can either
import a single class
To use a class or a package from the library, you need to use the import keyword:

Scanner class, which is used to get user input, :

2. User-defined Packages
To create your own package, you need to understand that Java uses a file
system directory to store them. Just like folders on your computer:

The Scanner class of the java.util package is used to read input data from different sources like input streams, files, etc. Let's take an example.
prepared by| YOHAN.R
Lab 4
OOP
Input Types (Scanner class) By Y.R

2
1 Read a Line of Text Using Scanner

prepared by| YOHAN.R


Advantage of Method JAVA

Method in java ❑ Code Reusability OOP

Lab 4.1
❑ Code optimization

a method is like function i.e. used to expose behavior of an object.


Declare with class and they are Used to Perform Certain Action.

Method Call Call a Method

prepared by| YOHAN.R


Method in java Static vs. Public JAVA
a method is like function i.e. used to expose behavior of an object. You will often see Java programs that have OOP
Declare with class and they are Used to Perform Certain Action. either Static or Public Attributes and Methods.
Lab 4.2
Method Call Private Access Modifier - The access level is only inside a class. The access level is everywhere
Public Access Modifier It Specify how to access classes, methods, and fields. ➢ Inside a class
Static Non - Access Modifier - you can access fields/methods using the ➢ Outside a class
class name. ➢ Inside the package
Example:- System.out > out is a static field of System ➢ Outside the package
1

Call a Method

prepared by| YOHAN.R


Method in java
JAVA
Static and Public Method OOP

Common Static and Public Method Example Access Modifier

For a Class We can For Attribute Method, &


Use Constructor You can use

Public Public

Default
Private

Default

Protected

Non - Access Modifier

For a Class We can For Attribute Method,


Use either You can use

Final Final, Static

Abstract Abstract,
Transient

Synchronized

An abstract method belongs to an abstract class, Volatile


and it does not have a body. The body is provided by the subclass: we will see, it in the next class
I. VOID METHOD
OOP

By Y.R

prepared by| YOHAN.R


OOP

By Y.R

// OUTPUT

//  Passing a parameter

In this case you will get an error, b/c as you can see
This method Doesn’t Take any Parameter.

prepared by| YOHAN.R


II. VALUE RETURNING METHOD
OOP

By Y.R

//Another way of writing this code.

prepared by| YOHAN.R


III. THE RETURNING KEYWORD
OOP

By Y.R

// Not compatible
// It will never execute
// am returning an integer but the type of method is void >> this will
give an error

//Another way of writing this code. // the type of this method is returning an integer and we are
returning a string are not compatible,>> this will give us an error

prepared by| YOHAN.R


Lab 5 & 6 OOP

By Y.R

 Encapsulation
 Abstraction
 Inheritance
 Polymorphism
 Project

prepared by| YOHAN.R


OOP
Encapsulation By Y.R

Encapsulation
The meaning of Encapsulation, is to make sure that "sensitive"
data is hidden from users. To achieve this, you must:
o declare class variables/attributes as private
o provide public get and set methods to access and update the value of a private
variable .

Get and Set: You learned from the previous chapter


that private variables can only be accessed within the same class
(an outside class has no access to it).

However, it is possible to access them if we provide public get and set methods.
The get method returns the variable value, and
the set method sets the value.

Why We use Encapsulation?


o Better control of class attributes and methods
o Class attributes can be made read-only (if you only use the get
method), or write-only (if you only use the set method)
o Flexible: the programmer can change one part of the code without
affecting other parts
o Increased security of data
prepared by| YOHAN.R
OOP
Object Oriented Programming By Y.R

Inheritance Types of Inheritance


Is one of the core Concepts of OOP.
is possible to inherit attributes and methods from one class to
another. We group the "inheritance concept“
into two categories:

•subclass (child) - the class that inherits from another class


•superclass (parent) - the class being inherited from

Why use inheritance in java


• For Method Overriding (so runtime polymorphism can be
achieved).
• For Code Reusability.
Note: Multiple inheritance is not supported in java through class.
Inheritance represents the IS-A relationship, also known as
Why multiple inheritance is not supported in java?
parent-child relationship.
To reduce the complexity and simplify the language, multiple inheritance is
not supported in java.

prepared by| YOHAN.R


OOP
Object Oriented Programming
Polymorphism
means "many forms", and it occurs when we have many classes that are related to
each other by inheritance.
Like we specified in the previous chapter; Inheritance lets us inherit attributes and
methods from another class. Polymorphism uses those methods to perform different
tasks. This allows us to perform a single action in different ways.
For example, think of a superclass called Animal that has a method called
animalSound(). Subclasses of Animals could be Pigs, Cats, Dogs, Birds - And they also
have their own implementation of an animal sound (the pig oinks, and the cat meows,
etc.):

prepared by| YOHAN.R


OOP
Object Oriented Programming By Y.R

THANK YOU!!
OOP Lab Cheat sheet by Yohan.R

You might also like