Chapter 2 Class As Basis of All Computation
Chapter 2 Class As Basis of All Computation
Class X
COMPUTER APPLICATION
Q.5. Distinguish between User-defined data type and Primitive data type.
Ans.
User-Defined data type Primitive data type
1. These data types are created 1. These are built-in data types
by the user which are provided by the
system.
2. They are available in 2. These data types are
accordance to their visibility available with the system.
modes throughout the
program.
Q.7. Explain access specifiers. Or name and explain various categories of access
specifiers.
Ans. The Access specifies are the terms used to specify the extent of usage of the class
member in the program. They are also termed as visibility modes which indicate
the program area where class member is visible.
Three categories are public, private and protected.
Public : class member specified as public can be used outside the visibility of the
class.(every where in the program)
Private: class member specified as private can be used only within the scope of a
class.
Protected: Protected members are used in the class as private members which can
only be used within the class but can be applied to another sub class during
inheritance.
The following table shows the accessibility of all access specifiers in java.
Here,
Yes= denotes access is allowed.
No= denotes access is not allowed.
Package = denotes project in java.
Q.13. Why can't every class be termed as user defined data type?
Ans. The classes that contain public static void main(String args[]) method are not
considered as user defined data type. Only the classes that don't contain this method
are called user defined data type. The presence of public static void main(String args[])
method in a class, converts it into a Java application so it is not considered as a user
defined data type.
Variables that are declared inside a class without using the keyword 'static' and outside
any member methods are termed instance variables. Each object of the class gets its
own copy of instance variables. For example, in the below class:
class Cuboid {
private double height;
private double width;
private double depth;
private double volume;
Programs
Question 1
Write a class program to accept two numbers as instance variables. Use the
following functions for the given purposes:
Class Name — Calculate
void inputdata() — to input both the values
void calculate() — to find sum and difference
void outputdata() — to print sum and difference of both the numbers
Use a main method to call the functions.
import java.util.Scanner;
Question 2
Write a class program with the following specifications:
Class Name — Triplet
Data Members — int a, b, c
Member Methods:
void getdata() — to accept three numbers
void findprint() — to check and display whether the numbers are Pythagorean
Triplets or not.
import java.util.Scanner;
Question 3
Define a class Employee having the following description:
Question 4
Define a class Discount having the following description:
Question 5
Define a class Telephone having the following description:
Output: