0% found this document useful (0 votes)
35 views

Cs3391 Object Oriented Programming Unit 1

Uploaded by

narmathaapcse
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)
35 views

Cs3391 Object Oriented Programming Unit 1

Uploaded by

narmathaapcse
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/ 12

CS3391 OBJECT ORIENTED PROGRAMMING

UNIT I INTRODUCTION TO OOP AND JAVA FUNDAMENTALS

Part A Two Marks Questions with Answers

Q.1 Mention some of the separators used in Java programming.

Ans. : Some of the separators that are used in Java are

Q.2 How dynamic initialization of variables is achieved in java ?

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 ?

public static void main(String[] args)


{
screen.write(sum());
}
static int sum()
{
int A=12;
int B=13;
return =A+B;
}
Ans. It will generate error at the statement return = A+B as "Illegal start of expression".

Q.4 What are the features of Java ?

1) Java is simple to implement.


2) Java is platform independent.
3) It is an object oriented programming language.
4) It is a robust programming language.
5) Java is designed for distributed system.
Q.5 Define objects and classes in Java.

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.

Q.6 Why are classes important in OO technology?

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?

Q.10 Define class. Give example.

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;

String Name; //Data Field

Customer() //Constructor

{}

double withdraw_money() //method {...}

Part B

1.) Explain and detail about structure of java?

JAVA SOURCE FILE - STRUCTURE – COMPILATION THE JAVA SOURCE FILE:


THE JAVA SOURCE FILE:
A Java source file is a plain text file containing Java source code and having .java extension. The .java
extension means that the file is the Java source file. Java source code file contains source code for a class,
interface, enumeration, or annotation type. There are some rules associated to Java source file.
Java Program Structure:

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

main Method Calss

Main Method Definition

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.

Comments help in Maintaining the Program.

Example:

/*

* Title: Conversion of Degrees *

Aim: To convert Celsius to Fahrenheit and vice versa

* 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:

public class xx extends Applet implements ActionListener

Class Definitions

A Java Program can have any number of class declarations.

The number of classes depends on the complexity of the program.

2.) Explain and detail about data type?

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.

Data types in Java are of two types:

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

3.) Explain and details about variable in java?

 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] …];

 Example of Variable names:

int average=0.0, height, total height;

 Rules followed for variable names


1. A variable name must begin with a letter and must be a sequence of letter or
digits.
2. They must not begin with digits.
3. Uppercase and lowercase variables are not the same.
a. Example: Total and total are two variables which are distinct.
4. It should not be a keyword.
5. Whitespace is not allowed.
6. Variable names can be of any length.

 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;

2. Declare and initialize on the same line:


Syntax:

int months;
months=1;

 Dynamic Initialization of a Variable:


Java allows variables to be initialized dynamically using any valid expression at the
timethe variable is declared.

Example: Program that computes the remainder of the division operation:

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

4.) Explain and detail about Array?


Definition:
An array is a collection of similar type of elements which has contiguous memory location.
Java array is an object which contains elements of a similar data type. Additionally, The elements of
an array are stored in a contiguous memory location.
It is a data structure where we store similar elements. We can store only a fixed set of elements in a

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:

Definition: One-dimensional array is an array in which the elements are stored


in onevariable name by using only one subscript.

 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:

int[] floppy; (or) int []floppy (or) int floppy[];

2. Instantiation of the array:

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.

arrayRefVar[index value]=constant or value;

Syntax to initialize values to array element:

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;

System.out.println(“April has ”+month_days[3]+ “ days.”);


}

Output:

April has 30 days.

Example 2: Finding sum of the array elements and maximum from the array:

public class TestArray


{
public static void main(String[] args)
{
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements

for (double element: myList)


{
System.out.println(element);
}

// Summing all elementsdouble total = 0;

for (int i = 0; i < myList.length; i++)


{

total += myList[i];
}
System.out.println("Total is " + total);

// Finding the largest elementdouble

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

 Used for more complex arrangements


Syntax to Declare Multidimensional Array in java:

1. dataType[][] arrayRefVar; (or)


2. dataType [][]arrayRefVar; (or)
3. dataType arrayRefVar[][]; (or)
4. dataType []arrayRefVar[];

You might also like