CHAPTER_CONSTRUCTOR_CLASS_X
CHAPTER_CONSTRUCTOR_CLASS_X
CHAPTER_CONSTRUCTOR_CLASS_X
CLASS – X
A member function having the same name as that of the class name is called constructor.
Question 2
Question 3
Question 4
Question 5
Question 6
Question 7
Question 8
The compiler supplies a special constructor in a class that does not have any constructor.
True
Question 2
Question 3
Question 4
Question 5
Question 6
Question 7
Question 8
A constructor is a member method that is written with the same name as the class name
and is used to initialize the data members or instance variables. It is invoked at the time of
creating any object of the class. For example:
Question 2
1. Parameterised constructor
2. Non-parameterised constructor
Question 3
A constructor is used to initialize the objects of the class with a legal initial value.
Question 4
A constructor used to initialize the instance variables of an object by copying the initial
values of the instance variables from another object is known as Copy Constructor.
The process of using a number of constructors with the same name but different types of
parameters is known as Constructor overloading.
Question 5
Constructors are special member methods of the class. Objects are non-primitive data
types so they are passed by reference and not by value to constructors. If objects were
passed by value to a constructor then to copy the objects from actual arguments to formal
arguments, Java would again invoke the constructor. This would lead to an endless circular
loop of constructor calls.
Question 6
Constructor Method
It is a block of code that initializes a It is a group of statements that can be called at any point
newly created object. in the program using its name to perform a specific task.
It has the same name as class name. It should have a different name than class name.
It needs a valid return type if it returns a value otherwise
It has no return type
void
It is called implicitly at the time of It is called explicitly by the programmer by making a
object creation method call
If a constructor is not present, a default
In case of a method, no default method is provided.
constructor is provided by Java
It may or may not be inherited depending upon its access
It is not inherited by subclasses.
specifier.
Question 7
Question 8
The first statement abc p = new abc(); is calling a non-parameterised constructor to create
and initialize an object p of class abc. The second statement abc p = new abc(5,7,9); is
calling a parameterised constructor which accepts three arguments to create and initialize an
object p of class abc.
Question 9
class Area
{
int l, b;
Area(int x, int y) {
l = x;
b = y;
}
}
Question 10
Create a class Rectangle with data members length, breadth and height. Use a parameterised
constructor to initialize the object. With the help of function surfacearea() and volume(),
calculate and display the surface area and volume of the rectangle.
Answer
class Rectangle {
double length;
double breadth;
double height;
Question 11
If we don't assign an object created by the new operator to a variable then it is known as a
temporary instance or an anonymous object. If we want to use an object only once, then we
can use it as a temporary instance. The below example illustrates this:
class Sum {
int x;
int y;
In the main method, the object of class Sum is not assigned to any variable. We use it just
to print the sum of 3 and 5 after which it is destroyed.
Question 12
Create a class Right_Triangle having data members as the perpendicular and base of a right-
angled triangle(int p, int b). Use a parameterised constructor to initialize the object. With the help
of function area( ) and diagonal( ), calculate and display the area and diagonal of the right angled
triangle.
Answer
class Right_Triangle {
int p;
int b;
1. int a
2. int b
Member functions:
import java.util.Scanner;
int hcf = x;
int lcm = (a * b) / hcf;
Output
Question 2
An electronics shop has announced a special discount on the purchase of Laptops as given
below:
1. name
2. price
3. dis
4. amt
Member Methods:
import java.util.Scanner;
Output
Question 3
Instance variables:
1. int num
2. int f
3. int rev
Member Methods:
import java.util.Scanner;
public Calculate(int n) {
num = n;
f = 0;
rev = 0;
}
f = 1;
if (num == 0 || num == 1)
f = 0;
else
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
f = 0;
break;
}
}
return f;
}
int t = num;
while (t != 0) {
int digit = t % 10;
rev = rev * 10 + digit;
t /= 10;
}
return rev;
}
Output
Question 4
Member Methods:
Write a main method to create an object of the class and call the above member methods.
import java.util.Scanner;
public Arrange(String s) {
str = s;
i = "";
p = s.length();
ch = 0;
}
Output
Question 5
Data members:
1. String str
Member functions:
import java.util.Scanner;
public Stringop() {
str = null;
}
Output
Question 6
Write a program by using a class to find the population of the country at the end of each year
from 2001 to 2007. The Class has the following specifications:
Member Methods:
import java.util.Scanner;
public class Population
{
private float p;
private float r;
Output
Question 7
Write a program in Java to find the roots of a quadratic equation ax2+bx+c=0 with the following
specifications:
Data Members — float a,b,c,d (a,b,c are the co-efficients & d is the discriminant), r1 and r2 are
the roots of the equation.
Member Methods:
If d < 0 then print "Roots not possible" otherwise find and print:
r1 = (-b + √d) / 2a
r2 = (-b - √d) / 2a
import java.util.Scanner;
Output
Question 8
public FruitJuice() {
product_code = 0;
flavour = "";
pack_type = "";
pack_size = 0;
product_price = 0;
}
Output
Question 9
The basic salary of employees is undergoing a revision. Define a class called Grade_Revision
with the following specifications:
Write the main method to create an object of the class and call all the member methods.
import java.util.Scanner;
public Grade_Revision() {
name = "";
bas = 0;
expn = 0;
inc = 0.0;
nbas = 0.0;
}
Output
Question 10
Define a class called Student to check whether a student is eligible for taking admission in class
XI with the following specifications:
Write the main method to create an object of the class and call all the member methods.
import java.util.Scanner;
return course;
}
Output
Question 11
Define a class Bill that calculates the telephone bill of a consumer with the following
description:
Create an object in the main() method and invoke the above functions to perform the desired
task.
import java.util.Scanner;
public Bill() {
bno = 0;
name = "";
call = 0;
amt = 0.0;
}
Output
Question 12
Write a main method to create an object of the class and call the above member methods.
import java.util.Scanner;
public BookFair() {
bname = "";
price = 0.0;
}
price -= disc;
}
Output