Chapter 2

Chapter 2

I. Fill in the blanks:

1. A ___________________is also considered as an object factory.

2. The ___________of a class differs on various characteristics.

3. The object of a ____________is represented through the attributes.

4. The term ______________is used for creating various objects.

5. Class is a ______________of the objects.

6. ______________keyword indicates an operator for dynamic allocation of an object.

7. Objects are also termed as class tags or ________________

8. Different objects of a class have common ______________

Ans.

1. class                                                                              

2. Objects

3. class                                                                              

4. ‘Object Factory’     

5. blueprint /prototype

6. new

7. ‘Instances of a class’

8. attributes

II. Answer the following questions:

1. Define Object with an example.

Ans.

An object is a unique entity that contains data and functions together in an Object Oriented Programming language. Object is also called as an ‘Instance of a class’ and objects are the basic elements of Object Oriented System. Moreover, a set of related objects may exchange data and information to interact with each other.

Example:

Consider the real-world object like a Book which has states/characteristics called Pages, Chapters etc and Behaviours such as readNotes( ) and wordSearch( ). Thus, a real world object is a combination of Characteristics of the Book and its Behaviours.

Consider the software object , ‘Student Details’

  • It has data members such as name, roll number which are similar to the states /characteristics of a real world object like a book (e.g. Chapters, Pages etc)
  • It also has functions  like AcceptMarks( ) and DisplayResult( ) which are similar to the behaviour of a real world object.

 (e.g. readNotes ( ))

2. Mention five states(characteristics) and two methods for the following classes:

a) class Employee       b) class Bank

c) class School            d) class Book

e) class Park               f) class Medicine

g) class Computer       h) class Camera

Ans.

ClassBehaviour or State (5)Methods (2)
a) class Employee Emp_id, Name, Dob, Join_date, SalarygenerateSalarySlip( ) , printFoodPass( )
b) class BankCust_id, Account_no, Name, Dob, AddressmonthlyStatement( ), printPassBook( )
c) class SchoolRoll_no, Name, Class, Aadhar, AddressacceptDetails( ), printDetails( )
d) class BookPage_num, Chapters, Category, ISBN, AuthoracceptData( ), printData( )
e) class ParkPark_Name, Incharge_Name, Location, Size, ChargescalculateCharges( ), printEntrySlip( )
f) class MedicineName, Category, Expiry_date, Batch_num, CompanyprintLabels( ), printSupplierDetails( )
g) class ComputerCategory, Brand, Model, Cost, Features,inputStockDetails( ), generateBill( )
h) class CameraBrand, Size, Model, Cost, FeaturesinputStockDetails( ), calculateDiscount( ), printBill( )

3. What is an Object? Give five examples of real world objects.

Ans.

  An object is a unique identity having some characteristics and behaviours. An object is also defined as an ‘Instance of a class’ because it possesses instance variables and member methods defined within the class. For example, if we define a class called ‘Fruits’ then the different types of fruits (Mango, Banana, Apple, Pear,Cherry etc) can be referred to as the ‘Objects’ of the ‘class Fruits’.

4. How will you define a software object?

Ans. A software object is created while writing a Java program. It is a unique combination of data and functions. Software objects can interact with each other and can pass information to each other through behaviours or functions (Known as Message Passing). The characteristics of real- world objects are referred to as data members and the behaviour of real-world objects are referred to as member functions of a software object.

5. Class and Objects are inter-related. Explain.

Ans. Class is used to create various objects which possess same characteristics and behaviour defined within it. So, we can say that class is the blue print or prototype of an object. Thus, each object follows all the features which are defined within the class.

For example, if ‘Car’ is the class with the characteristics colour, model etc then ‘Swift Dzire’ and ‘VW polo’ are the objects of the class ‘Car’.

6. A class is also referred to as ‘Object Factory’. Comment.

Ans. Generally, the term factory is used to refer to the place where similar kinds of objects are created from a prototype. Similarly, in Java, objects are created by using all the information (data members, member methods) available in the class. Since a class is used to create many objects containing common attributes and behaviour it is called as an ‘Object factory’.

7. What does the following statement mean?

     Employee staff=new Employee( );

Ans. The above statement means that an object called ‘Staff’ has been created using the prototype of the ‘Employee’ class. The ‘new’ keyword is used to instantiate the class by allocating memory for the created object.

8. Why is an Object called an ‘Instance of a class’? Explain.

     Ans. An object that is created using a class is said to be an instance of that class. The variables that the object contains are called Instance variables and the methods that the object contains are called instance variables. Hence an Object is called as an ‘Instance of a class’.

9. Why is a class known as a composite data type?

Ans. A class is a tool to create user defined data type. The class name becomes the data type for the instances used in the program. Class is a data type that includes various predefined data types (int, float, double etc) within it. Hence, the class is said to be a composite data type.

10. Write a statement to create an Object called ‘Keyboard’ of the class ‘Computer’.

Ans. Computer Keyboard = new Computer( );

11.Consider a real world object as ‘Cricket Ball’. Now mention two behaviours and methods each by taking the ‘Cricket Ball’ as a software object.

Ans.  Cricket Ball

          Behaviours  : colour, type

          Methods      : checkCricketBallStock( ), checkFootballStock( )

12. Refer a class structure as shown below:

Class MySchool

{

Name

Address

Principal’s name

AcceptData( );

PrintData( );

}

With reference to the above class declaration, indicate whether the following statements are true/false.

i) Acceptdata( ) is the characteristic of the class.  

ii) Address is behaviour of the class.

iii) AcceptData( ) and PrintData( ) are the common behaviour of the objects of class ‘Our School’

iv) Behaviours are the medium of inter-object communication.

v) Creating multiple objects of class ‘School’ is not possible.

Ans.

i)  false

ii) true

iii)false

iv) true

v) false

13. You want to create a class ‘Football’. Choose the elements to be used as characteristics and behaviour from the list given below:

Ball, Goalkeeper, Making a goal, Defender, Forward player, Passing ball, Referee, Hitting Corner, Making Fault.

Ans.

Characteristics:Behaviours      :  
BallMaking a goal
GoalkeeperPassing ball
DefenderHitting Corner
Forward playerMaking Fault
Referee 

14. Write a program by using a class ‘Picnic’ without any data members but having only functions as per the specifications given below:

Class name : Picnic

void display1( ):To print venue, place and reporting time

void display2( ):To print the number of students, name of the teacher accompanying and bus number.

Write a main class to create an object of the class ‘Picnic’ and call the functions display1( ) and display2( ).

Ans.

class Picnic{

void display1( )

{

Systen.out.println(“Venue   :”+ “Cubbon Park” );

Systen.out.println(“Place     :”+ “Bangalore” );

Systen.out.println(“Reporting Time:”+ “8am” );

}

void display2( )

{

Systen.out.println(“Number of Students    :”+ “100” );

Systen.out.println(“Name of the teacher    :”+ “Mrs.Archer” );

System.out.println(“Bus Number               :”+ “28”);

}

public static void main()

{

Picnic obj = new Picnic( );

obj.display1( );

obj.display2( );

}  

} //end of the class

*****************