KnowledgeBoat Logo
|

Computer Applications

Define a class ElectricityBill with the following specifications:

Data MembersPurpose
String nstores the name of the customer
int unitsstores the number of units consumed
double billstores the amount to be paid
Member MethodsPurpose
void accept()to store the name of the customer and number of units consumed
void calculate()to calculate the bill as per the tariff table given below
void print()to prints the details in the format given below

Tariff Table

Number of unitsRate per unit
First 100 units₹2.00
Next 200 units₹3.00
Above 300 units₹5.00

A surcharge of 2.5% charged if the number of units consumed is above 300 units.

Output:
Name of the customer: ___
Number of units consumed: _____
Bill amount: __

Write a main method to create an object of the class and call the above member methods.

Java

Java Classes

ICSE 2017

14 Likes

Answer

import java.util.Scanner;

public class ElectricityBill
{
    private String n;
    private int units;
    private double bill;
    
    public void accept() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter customer name: ");
        n = in.nextLine();
        System.out.print("Enter units consumed: ");
        units = in.nextInt();
    }
    
    public void calculate() {
        if (units <= 100)
            bill = units * 2;
        else if (units <= 300)
            bill = 200 + (units - 100) * 3;
        else {
            double amt = 200 + 600 + (units - 300) * 5;
            double surcharge = (amt * 2.5) / 100.0;
            bill = amt + surcharge;
        }
    }
    
    public 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(String args[]) {
        ElectricityBill obj = new ElectricityBill();
        obj.accept();
        obj.calculate();
        obj.print();
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Define a class ElectricityBill with the following specifications: Tariff Table A surcharge of 2.5% charged if the number of units consumed is above 300 units. Output: Name of the customer: ___ Number of units consumed: __ ___ Bill amount: __ Write a main method to create an object of the class and call the above member methods.

Answered By

3 Likes


Related Questions