Nothing Special   »   [go: up one dir, main page]

Class:10 Subject: Computer Applications: LS 10. Constructors Important Points

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Class :10 Subject: Computer Applications

LS 10. Constructors
Important Points:
1. Constructor: A Constructor is a special method, which has the same name
as of the class name. A constructor, automatically gets invoked whenever an
object of the class is created and is used to initialize the data members.

2. The main features of Constructors are :

● A Constructor is a special method that has the same name as that of the
class name.
● A Constructor has no return type, not even void.

● A Constructor is automatically called, on object creation.

● We can have more than one Constructor in a class with different


arguments lists. (Constructor Overloading).
● If a constructor is not defined, then the compiler provides a default
constructor with null values.
● Constructors may have no parameters or may include parameters of
various types.

Declaration of a constructor:

Syntax : [Access specifier] <class name > ( Parameter list )


{
---- ----- -- --
}

Ex : public Student() // default constructor


public Student(int r, int m) // parameterised constructor

class Student
{
int rollno ;
int standard ;
public Student () // default constructor
{
rollno = 1;
standard = 9;
}
public Student (int R, int STD) // parameterised constructor
{
rollno = R;
standard = STD;
}

}
3. Need for Constructors:
The main purpose of a constructor is to
⮚ create an object of a class

⮚ initialize the data members and

⮚ to perform other complex tasks.

Note: The access specifier should be public, may be protected or default but never
private.
● Constructor in the creation of object of a class:

Syntax: < class name > <object name > = new Constructor ( ) ;

Ex: Student s1 = new Student ( );

4. Difference between Constructors and Functions/Methods:

Constructor Method

It is a block of code that initializes a newly It is a group of statements that can be called
created object. at any point in the program using its name to
perform a specific task.

It has the same name as the class name. It should have a different name than the
class name.

It has no return type It needs a valid return type if it returns a


value otherwise void

It is called implicitly at the time of object It is called explicitly by the programmer by


creation making a method call

If a constructor is not present, a default In the case of a method, no default method is


constructor is provided by Java provided.

5. Types of constructors:

a. Default Constructor (Non- Parameterized )


b. Parameterized Constructor

a) Default Constructor: They are also known as non – parameterized constructors,


which have no parameters and hence do not accept any arguments.
class Sample
{
int a ; float b; char ch;
Sample ( ) // default constructor
{
b = 2.5f ;
ch = ‘A’ ;
}
public void accept( )
{
a=100;
}
public void show( )
{
Sopln(“a= “+a);
Sopln(“b= “+b);
Sopln(“ch= “+ch);
}
}

b) Parameterized Constructor: A Parameterized constructor always accepts


arguments when an object of the class is created. It uses its arguments to initialize
the data members of the class for its new object.
class Sample
{
int a ;float b;char ch;
Sample (float x, char c ) // parameterised constructor
{
b = x;
ch = c;
}
public void accept( )
{
a=100;
}
public void show( )
{
Sopln(“a= “+a);
Sopln(“b= “+b);
Sopln(“ch= “+ch);
}
}

6. Constructor Overloading : The process of creating more than one constructor in


a class but differentiated either by their number of arguments or data types is called
Constructor Overloading .
Ex:
class Overload
{
int a;
double y,z;

public Overload( )
{
a=12;
}
public Overload ( double x)
{
y=x;
}
public void show( )
{
System.out.println(“a= “+a);
System.out.println(“y= “+y);
}
public void main ()
{
Overload obj1 = new Overload( ); // object using default constructor
Sopln(“OUTPUT 1”);
obj1 .show( );

Overload obj2 = new Overload( 5.2); // object using parameterised constructor


Sopln(“OUTPUT 2”);
obj2 .show( );
}
}

Program1:

Create a class Rectangle with data members:


length, breadth and height.
Use a parameterized constructor to initialize the object.
With the help of function surfacearea() and volume(), calculate and display
the surface area and volume of the cuboid.

class Rectangle {
double length;
double breadth;
double height;
Rectangle(double l, double b, double h)
{
length = l;
breadth = b;
height = h;
}
public void surfacearea() {
double sa = 2 * (length * breadth+ breadth * height + length * height);
System.out.println("Surface Area = " + sa);
}
public void volume() {
double vol = length * breadth * height;
System.out.println("Volume = " + vol);
}}

Program 2:
An electronics shop has announced a special discount on the purchase of
Laptops as given below:
Define a class Laptop described as follows:
Data members/instance variables:
1. name
2. price
3. dis
4. amt
Member Methods:
1. A parameterised constructor to initialize the data members
2. To accept the details (name of the customer and the price)
3. To compute the discount
Category Discount on Laptop
Up to ₹25,000 5.0%
₹25,001 - ₹50,000 7.5%
₹50,001 - ₹1,00,000 10.0%
More than ₹1,00,000 15.0%
4. To display the name, discount and amount to be paid after discount.
5. Write a main method to create an object of the class and call the
member methods.

import java.util.Scanner;
public class Laptop {
private String name;
private int price;
private double dis;
private double amt;
public Laptop(String s, int p) {
name = s;
price = p;
}
public void compute() {
if (price <= 25000)
dis = price * 0.05;
else if (price <= 50000)
dis = price * 0.075;
else if (price <= 100000)
dis = price * 0.1;
else
dis = price * 0.15;
amt = price - dis; }
public void display() {
System.out.println("Name: " + name+”\nprice”+price);
System.out.println("Discount: " + dis);
System.out.println("Amount to be paid: " + amt);
}
public static void main() {
Scanner in = new Scanner(System.in);
System.out.print("Enter Customer Name: ");
String str = in.next();
System.out.print("Enter Price: ");
int p = in.nextInt();
Laptop obj = new Laptop(str,p);
obj.compute();
obj.display();
Laptop obj1=new Laptop(“john”,40000);
obj1.compute();
obj1.display();
}
}

EXTRA PROGRAMS:

1. Create a class Rectangle to find the area of rectangle.

Instance variables-length and breadth


Functions:
1)Rectangle( )- a default constructor to assign 0 to length and breadth.
2)Rectangle( float l, float b)-to store value of l to length and b to breadth.
3)void findArea( )-to calculate the area of rectangle
Create a main method and invoke the above functions.

2. Write a program by using a class with the following specifications:


Class name — Calculate
Instance variables:

1. int num
2. int f
3. int rev
Member Methods:

1. Calculate(int n) — to initialize num with n, f and rev with 0 (zero)


2. int prime() — to return 1, if number is prime
3. int reverse() — to return reverse of the number
4. void display() — to check and print whether the number is a prime palindrome or not

3. Define a class named FruitJuice with the following description:

Data Members Purpose

int product_code stores the product code number

String flavour stores the flavour of the juice (e.g., orange, apple, etc.)

String pack_type stores the type of packaging (e.g., tera-pack, PET bottle, etc.)

int pack_size stores package size (e.g., 200 mL, 400 mL, etc.)

int product_price stores the price of the product

Member Purpose
Methods

FruitJuice() constructor to initialize integer data members to 0 and string data


members to ""

void input() to input and store the product code, flavour, pack type, pack size
and product price

void discount() to reduce the product price by 10

void display() to display the product code, flavour, pack type, pack size and
product price

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

You might also like