Page 267 - ComputerScience_Class_11
P. 267
19 {
20 double amt = 200 + 600 + (units - 300) * 5;
21 double surcharge = (amt * 2.5) / 100.0;
22 bill = amt + surcharge;
23 }
24 }
25 void print()
26 {
27 System.out.println("Name of the customer\t\t: " + n);
28 System.out.println("Number of units consumed\t: " + units);
29 System.out.println("Bill amount\t\t\t: " + bill);
30 }
31 public static void main(String[] args)
32 {
33 Scanner in = new Scanner(System.in);
34 String n;
35 int units;
36 System.out.print("Enter customer name: ");
37 n = in.nextLine();
38 System.out.print("Enter units consumed: ");
39 units = in.nextInt();
40 ElectricBill obj = new ElectricBill(n,units);
41 obj.calculate();
42 obj.print();
43 }
44 }
The output of the preceding program is as follows:
BlueJ: Terminal Window - Java
Options
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.
Methods and Constructors 265

