Chapter 4 Unit 2: Objects and Classes

PrevNextBack

Chapter 4 Unit 2: Objects and Classes

Contents

Fill in the blanks

Question 1

Class is a blueprint of an object.

Question 2

A real word objects contains characteristics and behaviours.

Question 3

A software method is referred to as a behaviour of real world object.

Question 4

Class acts as a machine producing objects of similar kinds hence, called Object factory.

Question 5

The data members of an object are also called characteristics of an object.

Write whether the following statements are True/False

Question 1

Class is a template to create different objects.
True

Question 2

Initialization is also termed as instantiation.
False

Question 3

Objects of same class may have different attributes.
True

Question 4

An abstract class may contain sub-classes.
True

Question 5

Class can be used create a data type of user's choice.
True

Write three best suitable attributes of the following classes

Question 1

Class Medicine

Answer

  1. Name
  2. Batch Number
  3. Expiry Date

Question 2

Class School

Answer

  1. Name
  2. Address
  3. Board

Question 3

Class TVset

Answer

  1. Company
  2. Model Number
  3. Price

Question 4

Class Market

Answer

  1. Name
  2. Address
  3. Type

Question 5

Class Biodata

Answer

  1. Name
  2. Age
  3. Qualification

Question 6

Class Laptop

Answer

  1. Company
  2. Processor
  3. RAM

Answer the following

Question 1

What is the purpose of 'new' operator?

Answer

The purpose of new operator is to instantiate an object of the class by dynamically allocating memory for it.

Question 2

In what way a class and an object are interrelated?

Answer

A Class is used to create various Objects that have different characteristics and common behaviours. Each object follows all the features defined within a class. Hence, class is also referred to as a blue print or prototype of an object.

Question 3

Write a statement to create an object MP4 belonging to class 'Digital'.

Answer

Digital MP4 = new Digital();

Question 4

What do you mean by the following statement?
Employee staff = new Employee();

Answer

This statement creates a new object of class Employee. The newly created object is assigned to a variable named staff which is of Employee type. The object can be accessed using staff variable.

Question 5

Why is class referred to as object factory?

Answer

A class has the complete description of the data elements the object will contain, the methods the object can do, the way these data elements and methods can be accessed. A class can create objects of itself with different characteristics and common behaviour just like a factory can produce similar items based on a particular design. Hence, class is also referred to as 'Object Factory'.

Question 6

An object is referred as an instance of a class. Explain.

Answer

A class can create objects of itself with different characteristics and common behaviour. So, we can say that an Object represents a specific state of the class. For these reasons, an Object is called an Instance of a Class.

Question 7

What do you mean by Abstract class?

Answer

A class declared using the abstract keyword is called an Abstract Class. We cannot instantiate objects of Abstract Class. An Abstract Class can contain abstract methods (methods without body) and concrete methods (normal methods with body). The sub-class inheriting the abstract class needs to provide the definition of abstract methods of the super-class otherwise sub-class also needs to be abstract.

Question 8

In what way is a class helpful in creating a user defined data type?

Answer

We may need to use a group of primitive types so that a number of values can be operated simultaneously by using a single interface. To do so, user may define a class with a specific name that serves as a specific data type. This is the reason, a class is referred to as user defined data type.

Question 9

What do you mean by instantiating a class?

Answer

Instantiating a class means creating an object of the class. A class is instantiated with the help of new operator that allocates space in dynamic memory for the storage of an object.

Question 10

Design a class 'Time' with the following specifications:

class Time

Data members/Instance variables
int hour, min, sec

Member Methods:
void get_time( ) : to accept a time in hour, minute and second.
void show_time( ) : to display the time in terms of hour, minute and second.

Write a main method to create an object of class 'Time' and call the member methods.

Answer

import java.util.Scanner;

class Time {
    int hour; 
    int min; 
    int sec;

    void get_time() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter hours: ");
        hour = in.nextInt();
        System.out.print("Enter minutes: ");
        min = in.nextInt();
        System.out.print("Enter seconds: ");
        sec = in.nextInt();
    }

    void show_time() {
        System.out.println(hour + ":" + min + ":" + sec);
    }

    public static void main(String args[]) {
        Time obj = new Time();
        obj.get_time();
        obj.show_time();
    }
}

Question 11

Design a class 'Rectangle' with the following specifications:

class Rectangle

Data members/Instance variables
int length, breadth, area, perimeter

Member methods:
void input( ) : to accept length and breadth of a rectangle.
void calculate( ) : to calculate area and perimeter of rectangle.
void display( ) : to print area and perimeter of rectangle.

Write a main method to create an object of class 'Rectangle' and call the member methods.

Answer

import java.util.Scanner;

class Rectangle {
    int length; 
    int breadth; 
    int area; 
    int perimeter;

    void input() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter length of rectangle: ");
        length = in.nextInt();
        System.out.print("Enter breadth of rectangle: ");
        breadth = in.nextInt();
    }

    void calculate() {
        area = length * breadth;
        perimeter = 2 * (length + breadth);
    }

    void display() {
        System.out.println("Area of Rectangle = " + area);
        System.out.println("Perimeter of Rectangle = " + perimeter);
    }

    public static void main(String args[]) {
        Rectangle obj = new Rectangle();
        obj.input();
        obj.calculate();
        obj.display();
    }
}

Question 12

Write a class template 'Calculator' with the following specifications:

class Calculator

Data members/Instance variables
int a,b,c

Member Methods:
void readdata( ) : to accept the values of a and b.
void add( ) : to add a and b and place the result in c.
void sub( ) : to subtract b from a and place the result in c. Display the result.
void mul( ) : to multiply a and b and place the result in c. Display the result.
void div( ) : to divide a by b and place the result in c. Display the result.

Write a main method to create an object of class 'Calculator' and call the member methods to enable the task.

Answer

import java.util.Scanner;

class Calculator {
    int a;
    int b;
    int c;

    void readdata() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a: ");
        a = in.nextInt();
        System.out.print("Enter b: ");
        b = in.nextInt();
    }

    void add() {
        c = a + b;
        System.out.println("Addition Result = " + c);
    }

    void sub() {
        c = a - b;
        System.out.println("Subtraction Result = " + c);

    }

    void mul() {
        c = a * b;
        System.out.println("Multiplication Result = " + c);

    }

    void div() {
        c = a / b;
        System.out.println("Division Result = " + c);

    }

    public static void main(String args[]) {
        Calculator obj = new Calculator();
        obj.readdata();
        obj.add();
        obj.sub();
        obj.mul();
        obj.div();
    }
}