Page 215 - computer science (868) class 11
P. 215
void print()
{
System.out.println("Name of the customer\t\t: " + n);
System.out.println("Number of units consumed\t: " + units);
System.out.println("Bill amount\t\t\t: " + bill);
}
public static void main()
{
Scanner in = new Scanner(System.in);
String n;
int units;
System.out.print("Enter customer name: ");
n = in.nextLine();
System.out.print("Enter units consumed: ");
units = in.nextInt();
ElectricBill obj = new ElectricBill(n,units);
obj.calculate();
obj.print();
}
}
The output of the preceding program is as follows:
Enter customer name : Ram Kumar Sharma
Enter units consumed : 563
Name of the customer : Ram Kumar Sharma
Number of units consumed : 563
Bill amount : 2167.875
4. Copy Constructor: This constructor is used to initialise the data values of instance variables of one object to the
instance variables of another object of the same class.
Let us take an example:
class copyconstructor
{
int num1, num2, r;
copyconstructor(int n1, int n2)
{
num1=n1;
num2=n2;
}
void displaysum()
{
r=num1+num2;
System.out.println("Result "+r);
}
public static void main()
{
copyconstructor c_ob1=new copyconstructor(4,5);
copyconstructor c_ob2=c_ob1;
c_ob1.displaysum();
c_ob2.displaysum();
}
}
213
Methods and Constructors 213

