Turn Desktop View Off
You are here:   HomeJava ProgrammingClasses in JavaCharacteristics of OOP in Java
by Dinesh Thakur Category: Classes

In this example, we compute the electricity bill of a particular customer. The class ElectricityBil1 contains field customerNo, name and units. It also contains methods setData () show () and billcalculate ().The statement,

ElectricityBill b = new ElectricityBill();

creates a ElectricityBill object in memory and assigns it to the reference variable b. Then the setData () method is invoked that initializes the fields custornerNo,name and units with 001, Dinesh Thakur and 280 values respectively.

The statement,

double billpay = b.billCalculate();

invokes the billCalculate ()method for the object b and stores the result obtained in the billpay variable. Finally, the bill details are displayed.

class Electricitybill

{
     private int customerNo;
     private String name;
     private int units;
     void setData(int num,String cname,int nounits )
     {
         customerNo = num;
         name = cname ;
         units=  nounits;
     }
     void show()
     {   
           System.out.println("Customer Number : " + customerNo);
           System.out.println("Customer Number : " + name);
           System.out.println("Units Consumed  : " + units);
     }
     double billcalculate ( )
     {
         double bill;
         if(units<100)
           bill = units * 1.20 ;
         else if(units <= 300)
            bill = 100 * 1.20 + (units - 100) * 2 ;
         else
            bill = 100 * 1.20 + 200 * 2 + (units - 300) * 3 ;
         return bill;
     }
  };
 
class ComputeElectricityBill
{
        public static void main(String[] args)
   {  
        Electricitybill b = new Electricitybill();
        b.setData(001,"Dinesh Thakur", 280);
        double billpay = b.billcalculate();
        b.show( );
        System.out.println("Bill to pay : " + billpay);
   }
}

Compute the Electricity Bill of a Person





Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article