[go: up one dir, main page]

0% found this document useful (0 votes)
47 views9 pages

Introduction To Object Oriented Programming Concepts: Computer Applications

This document provides an overview of key concepts in object-oriented programming and Java, including objects and classes, principles of OOP (abstraction, inheritance, polymorphism, encapsulation), variables, data types, operators, packages, input/output, conditional statements, and mathematical methods. It defines objects as bundles of related variables and methods, and classes as blueprints that define common attributes and behaviors of objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views9 pages

Introduction To Object Oriented Programming Concepts: Computer Applications

This document provides an overview of key concepts in object-oriented programming and Java, including objects and classes, principles of OOP (abstraction, inheritance, polymorphism, encapsulation), variables, data types, operators, packages, input/output, conditional statements, and mathematical methods. It defines objects as bundles of related variables and methods, and classes as blueprints that define common attributes and behaviors of objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Class: 10 B

Computer Applications
Chapter 1 : Introduction to Object Oriented Programming Concepts
Theory Part

Salient Points of this chapter

1. Objects and Classes


2. Principles of Object Oriented Programming
3. Concept of Objects and classes
4. Classes Vs Objects
5. Values and Data Types
6. Tokens, Variables
7. Operators in Java
8. Java Packages
9. Input in Java
10. Mathematical Library Methods
11. Conditional Constructs in Java
12. Iterative Constructs
13. Continue and Break Statement
14. Programming Example
15. Test Yourself
Objects and Classes :
A software bunch or bundle of variables and related methods known as object. For example take one object like a
student of class 10B named Bijay. Bijay has some attributes which represent basic information about him like his
name, roll number, reg_No etc. And there are some functions that he performs like extra_curricular_ activity, study,
food_habit etc.
A class is a prototype of an object with common attributes or properties, operations or behaviour, relationships and
semantics.

Principles of Object Oriented Programming :


(a) Abstraction
(b) Inheritance
(c) Polymorphism
(d) Encapsulation
(a) Abstraction
Abstraction is one of an important feature of Object oriented programming language. Without getting involved
with the complexity of the entire system representing the essential features.
(b) Inheritance
The way by which one object acquires the properties of another object. One of the basic benefit is that
programming code once defined can be reused.

(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.

Concept of Objects and classes :


A software object manages its state either in one or more variables. Methods can be implemented with the behaviour
of a software object. A method is a function related with an object.

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.

Values and Data Types :


A set of alphabets, letters and some special characters which are valid in java language usually kwon as character set.
Programming languages are also use characters, words known as token and sentences known as statements. A
character set consists of Uppercase characters(A...Z), lowercase characters(a...z), digits (0....9)and special
characters.(+, _, ( ), { }, /, <, > etc.).
A character encoding informs the computer how to interpret Raw zeros and ones into real characters by pairing
numbers with characters. In such a way words and sentences in text are created from characters and these characters
are grouped into a character set. Different types of character encoding available.
A special set of characters that are used to signal an alternative interpretation of a series of characters known as
Escape sequence. In Java some available Escape sequences are :
\t – inserts a tab in the text
\b- inserts a back space in the text
\n - inserts a new line in the text
Like these so many other Escape sequences are there.

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 :

(a) Built in Packages :


Java.lang : contains language support classes.
Java.io : contains input/output support operation
Java.util : contains utility classes which implement data structures.
Java.applet : contains classes for creating Applets.
Java.awt : contains classes for implementation of graphical user interfaces.
Java.net: contains classes for supporting networking operations.
(b) User-defined Packages :
Packages defined by the user or programmer.

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.

Mathematical Library Methods :


Java Math class provides mathematical calculations.
pow( ) –return type double – returns power value of double type.
Sqrt( ) - return type double- returns square root of double type.
Ceil ( ) -return type double-return the double value greater than the argument value.
floor( ) - return type double - return the double value less than the argument value.
round( ) - return type double- rounds to the nearest integer.
abs( ) - return type double – returns absolute value of variable a.
max( )- return type double – returns maximum value of the two numeric values.
min( ) - return type double- returns minimum value of the two numeric values
random( ) - return type double- returns random positive real value between 0 to 1.
cbrt( ) - return type double – returns the cube root of double type value.
Conditional Constructs in Java :
Program control statements in java can be controlled into the following divisions : like selection iteration and jump.
1. Selection statements
Selective execution of statements by enabling decision.
(a) If statement :
If (test expression)
{
//statement-bl ;
}
// statement-out ;
If is a reserved word, test expression is a type of boolean value, if expression is true statement –bl will work otherwise if
expression is false then statement –bl will not work but statement-out gets executed.
Like this we can apply if-else statement, if-else-if statement, nested if statement, switch statement.
We will see one example of switch statement:
switch(expression)
{
case value 1:
//statement sequence ;
break;
case value 2:
//statement sequence ;
break;
..........
case value n:
//statement sequence ;
break;
default :
// default statement sequence ;
}
Here the expression also called case label must be of type byte, short, int or char and should be unique. The value of
expression will compare with of literal value in the case statements. If positive or result found then the following case
statement will execute. If none of the constants match the expression value then the default value will execute. Break
statement will terminate after positive execution.

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.

Two basic types of loops:


(a) Definite loop : loop will perform up to given limit.
(b) Indefinite loop : performs as long as the users want.
(i) Entry controlled : the test expression is evaluated before each iteration. Like for , while loop
(ii) Exit controlled : the test expression is evaluated after each iteration. Like do...while loop.

While loop : do...while loop: do for loop :


While(condition) { for (initialisation; condition; iteration)
{ //body of the loop {
//body of the loop } // body of the loop
} while (condition); }
Continue and Break Statement :
Break statement use to come out from the loop instantly and the continue statement in encountered inside a loop and
control directly jumps to the beginning of the loop for next iteration by skipping the execution of statements inside
loop’s body of the current iteration.

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

End of first session

You might also like