Cs3391 Object Oriented Programming Unit 1
Cs3391 Object Oriented Programming Unit 1
Ans. The variables in Java are allowed to get initialized at run time. Such. type of initialization is called
dynamic initialization.
For example
double a=5.0;
double b=20.0
double c=Math.sqrt((a+b));
Here the expression is evaluated at run time and its square root value is calculated and then the variable c is
initialized dynamically.
Q.3 What is the output of the main method in the given code ?
Ans. An object is an instance of a class. The objects represent the real world entity. The objects are used to
provide a practical basis for the real world. Each class is a collection of data and the functions that
manipulate the data. The data components of class are called data fields and the function components of the
class are called member functions or methods.
Ans. Classes bind together the relative data and methods in a cohesive unit. Due to this arrangement, only
certain methods are allowed to access the corresponding data. Secondly, if any modification is needed, then
the class can be viewed as one module and the changes made in one class does not spoil rest of the code.
Moreover, finding error from such a source code becomes simple.
Hence use of class is very important thing in OO technology.
Q.7 What is the difference between object and class?
Q.8 What is the difference between static and non static variables?
Ans. A static variable is shared among all instances of class, whereas a non static variable (also called as
instance variable) is specific to a single instance of that class.
Q.9 What is the difference between structure and class?
Ans. Each class is a collection of data and the functions that manipulate the data. The data components of
class are called data fields and the function components of the class are called member functions or methods.
For example
class Customer
int ID;
Customer() //Constructor
{}
Part B
Java program may contain many classes of which only one class defines the main method. A Java program
may contain one or more sections.
Documentation Section
Package Statement
Import Statements
Interface Statements
Class Definitions
Of the above Sections shown in the figure, the Main Method class is Essential part, Documentation Section
is a suggested part and all the other parts are optional.
Documentation Section
It Comprises a Set of comment lines giving the name of the program, the authorand other details.
Example:
/*
* Date: 31/08/2000
* Author: tim
*/
Package Statement
The first statement allowed in a Java file is a package statement. It declares the package name and informs
the compiler that the classes defined belong to this package.
Example :
package student;
package basepackage.subpackage.class;
Import Statements
The statement instructs the interpreter to load a class contained in a particular package.
Example :
import student.test;
Interface Statements
An interface is similar to classes which consist of group of method declaration. Like classes, interfaces
contain methods and variable. To link the interface to our program, the keyword implements is used.
Example:
Class Definitions
Data type is used to allocate sufficient memory space for the data. Data types specify the different sizes and
values that can be stored in the variable.
1. Primitive data types (Intrinsic or built-in types ) :- : The primitive data types include boolean,
char, byte, short, int, long, float and double.
2. Non-primitive data types (Derived or Reference Types): The non-primitive data types include
Classes, Interfaces, Strings and Arrays.
1. Primitive Types:
Primitive data types are those whose variables allow us to store only one value and neverallow
storing multiple values of same type. This is a data type whose variable can hold maximum one
value at a time.
There are eight primitive types in Java:
Integer Types:
1. int
2. short
3. lomg
4. byte
Floating-point Types:
5. float
6. double
Others:
7. char
8. Boolean
Integer Types:
The integer types are form numbers without fractional parts. Negative values are allowed.Java provides the
four integer types
Storage Default
Type Range Example
Requirement Value
-2,147,483,648(-2^31)
int a =
int 4 bytes to 0
2,147,483,647 (2^31-1) 100000, int b =
-200000
short s =
short 2 bytes -32,768 (-2^15) to 32,767 (2^15-1) 0
10000, short r =
-20000
-9,223,372,036,854,775,808 (-2^63)
long a =
long 8 bytes to 0L
100000L, int b =
9,223,372,036,854,775,808 (2^63-1)
-200000L
byte a = 100
byte 1 byte -128 (-2^7) to 127 (2^7-1) 0
, byte b = -
50
Floating-point Types:
The floating-point types denote numbers with fractional parts. Thetwo floating-pointtypes are
shown below
Storage Default
Type Range Example
Requirement Value
Approximately
float 4 bytes float f1 0.0f
±3.40282347E+38 =234.5f
F (6-7 significant decimal digits)
Approximately
double 8 bytes double d1 0.0d
±1.79769313486231570E+308
=
(15 significant decimal digits)
123.4
char:
char data type is a single 16-bit Unicode character.
Minimum value is '\u0000' (or 0).
Maximum value is '\uffff' (or 65,535 inclusive).
Char data type is used to store any character.
Example: char letterA ='A'
boolean:
boolean data type represents one bit of information.
There are only two possible values: true and false.
This data type is used for simple flags that track true/false conditions.
Default value is false.
Example: boolean one = true
A Variable is a named piece of memory that is used for storing data in java
Program.
A variable is an identifier used for storing a data value.
A Variable may take different values at different times during the
execution if the program, unlike the constants.
The variable's type determines what values it can hold
and what operations can beperformed on it.
Syntax to declare variables:
datatype identifier [=value][,identifier [ =value] …];
Initializing Variables:
After the declaration of a variable, it must be initialized by means of assignment
statement.
It is not possible to use the values of uninitialized variables.
Two ways to initialize a variable: 1. Initialize after declaration:
Syntax: Two ways to initialize a variable:
1. Initialize after declaration:
Syntax: variablename=value;
int months;
months=1;
class FindRemainer
{
public static void main(String arg[]) {int num=5,den=2;
int rem=num%den; System.out.println(―Remainder is ―+rem);
}
}
Output:
Remainder is 1
Java array.
Advantage of Array:
• Code Optimization: It makes the code optimized; we can retrieve or sort the data
easily.
• Random access: We can get any data located at any index position.
Disadvantage of Array:
Size Limit: We can store only fixed size of elements in the array. It doesn't growits
size at runtime.
Types of Array:
There are two types of array.
1. One-Dimensional Arrays
2. Multidimensional Arrays
1. One-Dimensional Array:
Creating an array:
Three steps to create an array:
1. Declaration of the array
2. Instantiation of the array
3. Initialization of arrays
1. Declaration of the array:
Declaration of array means the specification of array variable, data_type and
array_name.
Syntax to Declare an Array in java:
Example:
Definition:
Allocating memory spaces for the declared array in memory (RAM) is called as
Instantiation of an array.
Syntax:
arrayRefVar=new datatype[size];
Example: floppy=new int[10];
1. Initialization of arrays:Definition:
Storing the values in the array element is called as Initialization of arrays.
Example:
floppy[0]=20;
Example: (One-Dimensional Array)
class Array
{
public static void main(String[] args)
{
int month_days[];
month_days=new
int[12];
month_days[0]=3
1;
month_days[1]=2
8;
month_days[2]=3
1;
month_days[3]=3
0;
month_days[4]=3
1;
month_days[5]=3
0;
month_days[6]=3
1;
month_days[7]=3
1;
month_days[8]=3
0;
month_days[9]=3
1;
month_days[10]=
30;
month_days[11]=
31;
Output:
Example 2: Finding sum of the array elements and maximum from the array:
total += myList[i];
}
System.out.println("Total is " + total);
max = myList[0];
for (int i = 1; i < myList.length; i++)
{
if (myList[i] > max)
max = myList[i];
}
System.out.println("Max is " + max);
}
}
Output:
1.9
2.9
3.4
3.5
Total is 11.7
Max is 3.5
2. Multidimensional Arrays:
Multidimensional arrays are arrays of arrays. It is an array which uses more than
one index to access array elements. In multidimensional arrays, data is stored in
row and column based index (also known as matrix form).
Definition:
Uses of Multidimensional Arrays:
Used for table