Define a class ElectricBill() with the following descriptions: Class - ElectricBill , Instance varaibles : String n - to store the name of the customer, int units - to store the number of units consumed, double bill - to store the amount to be paid. Member methods: void accept() - to accept the name of the customer and number of units consumed , void calculate() - to calculate the bill as per the following tariff: First 100 units - rate per unit = rs. 2.00, Next 200 units - rate per unit = rs. 3.00; Above 300 units - rate per unit = 5.00.A surcharge of 2.5 % charged if the number of units is above 300 units. Write a main() method to create an object of the class and call the above member methods.

 THE FOLLOWING PROGRAM SEGMENT SHOWS HOW TO DO THE ABOVE OPERATIONS:

import java.util.*;

class ElectricBill

{

    String s;

        int units;

        double bill;double surcharge = 0.0;

    void accept()

    {

    Scanner ob = new Scanner (System.in);

    System.out.println("Enter the name of the customer");

    s = ob.nextLine();

     System.out.println("Enter the number of units consumed");

     units = ob.nextInt(); 

    }

    void calculate()

    {

        if(units<= 100)

        {

            bill = units*2.00;

        }

         if(units <= 300)

        {

            bill = units*3.00;

        }

        if(units > 300)

        {

            bill = units * 5.00;

            surcharge = (2.5/100)*bill;

        }

    }

    void print()

    {

         System.out.println("Name of the customer " + s );

          System.out.println("Number of units consumed " + units);

           System.out.println("The bill amount = " + bill);

           System.out.println("surcharge is = "+surcharge);

        }

        public static void main (String args[])

        {

            ElectricBill obj = new ElectricBill();

            obj.accept();

            obj.calculate();

            obj.print();

        }

    }

The above program is compiled and is without any error.

Hope you guys found it helpful.

Please share it with your friends and comment down in the comment section below.

Follow https://askrisfor.blogspot.com/ for more such updates.

Visit us again.

Thank you!!

Comments

Popular Posts