Class:10 Subject: Computer Applications: LS 10. Constructors Important Points
Class:10 Subject: Computer Applications: LS 10. Constructors Important Points
Class:10 Subject: Computer Applications: LS 10. Constructors Important Points
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.
● 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.
Declaration of a 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
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 ( ) ;
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.
5. Types of constructors:
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( );
Program1:
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. int num
2. int f
3. int rev
Member Methods:
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.)
Member Purpose
Methods
void input() to input and store the product code, flavour, pack type, pack size
and product price
void display() to display the product code, flavour, pack type, pack size and
product price
**************