Professional Documents
Culture Documents
Lab Main 3rd
Lab Main 3rd
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
1 4
3
Logical Operators
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
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”
By Y.R
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.
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.
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)).
|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
Constructor must not have return type. Method must have return
(no-arg type.
constructor)
Lab 3.3
OOP
Constructor Constructor Name Must be.
Constructor Must’ve no Explicit Return Type.
By Y.R
| Default-Constructor | Parametrized-Constructor
1 2
By Y.R
Arrays
Packages/Scanner/Input
Methods
Modifiers (Private , Public)
Method Call, Void, Return Keyword
Value Returning Method
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
Length
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:
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
Lab 4.1
❑ Code optimization
Call a Method
Public Public
Default
Private
Default
Protected
Abstract Abstract,
Transient
Synchronized
By Y.R
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.
By Y.R
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
By Y.R
Encapsulation
Abstraction
Inheritance
Polymorphism
Project
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 .
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.
THANK YOU!!
OOP Lab Cheat sheet by Yohan.R