Introduction To Object Oriented Programming Concepts: Computer Applications
Introduction To Object Oriented Programming Concepts: Computer Applications
Computer Applications
Chapter 1 : Introduction to Object Oriented Programming Concepts
Theory Part
(c) Polymorphism
The way of an interface to do different things based on the data of different types it accepts and provide the
proper expected output accordingly. Same message should respond through two or more classes in different
ways.
(d) Encapsulation
A wrapper which prevents the data and the code from being accessed by any other code defined outside.
Classes Vs Objects :
A class is a prototype or blueprint which defines variables and methods common to all object of a certain type.
A class determines the state and behaviour of all the objects of its type. A class is called object factory because by
using a type of class different objects can be created with similar characteristics. A class is not itself the object it
describes. A class is an abstract type where as objects are the instance of the type.
A method or function is a subprogram that works on data and often returns a value.
Tokens :
A set of predefined words and symbols available in the programming languages. In Java programming language they
are called tokens. Java tokens are the smallest units of a java program. Five types of tokens are there.
1. Reserved Keywords : Word that have special predefined purpose in a java program.
2. Identifiers : Represent names which can be assigned to variables, methods and classes to identify them
uniquely.
3. Literals or Constants : Refer to a fixed unchanged value during the execution of the program. Java supports
different types of constants. Like Integer( Decimal, Octal, Hexadecimal), Real, Character, String, Boolean.
4. Operators : The mathematical symbols used to perform mathematical and logical operations among two or
more variables or literals named operands. There are different types of operators available in Java as
Arithmetic operators, Relational operators, Logical operators, Bitwise operators, Assignment operators etc.
5. Separators : Symbols that help define the structure of a program. The separators used in Java are Parenthesis
(), Braces {}, Brackets [], Semicolon ;, Colon :, Comma , and Period.
Variables :
In memory a variable is a name reserved a specified area allocation. The value of the variable can change and the
name of the variable is defined by the naming rules of identifiers.
There are three types of variables in Java:
Local variable : Which is declared inside a method.
Instance variable : Which is declared inside a class and outside of any method.
Static variable : Must be explicitly declared as static.
“final” keyword – When we define a variable as final the value of the variable remains constant throughout the
program. When we define a class as final it cannot be inherited by any sub class even if we use ‘extend’ keyword.
Parameter : The value that can be passed to a method in Java.
Data Types : The set of possible values a variable can hold.
Two types of data types :
Primitive : This is also known as fundamental data types. These are predefined data types provided by a programming
language.
Non-Primitive : The data types made up of the combinations and sequences of multiple variables of primitive data
types. For example : Arrays, String etc.
Operators in Java :
Special symbols that perform specific operations on one, two or more operands known as operator. Operands can be
constant or variable.
Types of operators :
Unary operators : Operators needed only one operand to perform different types of operations. Like +, - , ++ , --,
! etc.
Binary operators: When two operands are required. Like + , - , * , / , %
Ternary operators : Operators required three operands. The ternary operator “ ? : ”.
Types of Operators :
Arithmetic Operators (+ , -, * , /)
Relational Operators ( > , < , >= , <=)
Logical operators (AND, OR , NOT)
Increment , decrement operators (++ , --)
Shorthand Operators (?)
Java Packages :
A mechanism in java to encapsulate a group of classes, sub packages and interface. From the package we can reuse
existing classes.
Uses of packages :
1. Preventing naming conflict.
2. Making search and usage of classes, interfaces etc.
3. Protected and default have access control of package level.
4. We can take Packages as example of data hiding or encapsulation.
Types of Packages :
Input in Java :
Input output operation of a java program performs through streams. A stream is an abstraction that either brings or
sends information.
InputStream OutputStream
Java
Source 101010 101010 Source
Application
1 1
Read Write
Streams
In Java System.in is used as console input and System.out refers to the standard output stream.
Iterative Constructs :
A set of instructions that are repeatedly executed until a termination condition will occur known as loop.
Loops can be declared as nested.
Return statement : it is a type of jump statement usually used at the end of a function or method to terminate it with
or without return value. The returned value depends on declared type of function.
Programming Example :
#1. Write a program in BlueJ environment to input two numbers and swap each number without using third variable.
Class name : Swap
Instance variables : int a, int b.
Methods : void input() – to input two numbers.
Void display() – to display result.
Input using bufferedreader statement.
Output :
Steps :
1. Importing built in package java.io in this program
2. Declare class
3. Declare body of the class by using opening curly brace.
4. Declare global or instance variables.
5. Declare method with throwing of IOException.
6. Declare body of the method by using opening curly brace.
7. Declare buffer statement.
8. Input two numbers using readLine function and convert it into integer by Integer.parseInt .
9. Display the original set of numbers.
10. Calculate the swapping without using third variable.
11. Close body of the method by using closing curly brace.
12. Declare display method.
13. Declare body of the method by using opening curly brace.
14. Display swapped result.
15. Close body of the method by using closing curly brace.
16. Terminate body of the class by using closing curly brace.
#2. Write a program in BlueJ environment to accept a name and print in line wise. Like :
R
RA
RAM
RAMA
RAMAY
RAMAYA
RAMAYAN
Output :
Steps :
1. Declare class
2. Declare body of the class by using opening curly brace.
3. Declare instance variables.
4. Declare method with parameter.
5. Declare body of the function by using opening curly brace.
6. Initialise local variable to instance or global variable.
7. Calculate the length of the string.
8. With the help of nested for loop extracting character with charat function and display line wise.
9. Close body of the method by using closing curly brace.
10. Declare main method with static.
11. Declare object of the class.
12. Call method or function with the help of object and pass string value.
13. Close body of the method by using closing curly brace.
14. Close body of the class by using closing curly brace.
Test Yourself :
1. What is an object ?
2. Write four principles of OOP and explain.
3. What is a class ?
4. What are methods ?
5. What is data type ?
6. What are literals in Java ?
7. What is nested loop ?
8. What is ternary operator ?
9. Give one example of nested for loop.
10. Explain “class is called object factory”.
11. Declare an object of a class.
12. Differentiate exit control and exit control loop.
13. Write a program to print the following output :
*
**
***
****
*****
14. Write a program that takes total marks and obtained marks of a student and print the grade A++, A+, A,
B+, B, C, D according to following slab.
A++ > 90
A+ > 75
A > 60
B+ > 50
B > 45
C > 35
D < 35
15. The data structure for the byke object is given below. Write what you think can be the methods for the
object.
Colour
Weight
Model
Year
Fuel_Type