Computer Applications
Define a class ElectricityBill with the following specifications:
| Data Members | Purpose |
|---|---|
| String n | stores the name of the customer |
| int units | stores the number of units consumed |
| double bill | stores the amount to be paid |
| Member Methods | Purpose |
|---|---|
| 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 units | Rate 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

Answered By
3 Likes
Related Questions
Write a program by using class with the following specifications:
Class name — Characters
Data Members:
- String str — To store the string
Member Methods:
- void input (String st) — to assign st to str
- void check_print() — to check and print the following:
(i) number of letters
(ii) number of digits
(iii) number of uppercase characters
(iv) number of lowercase characters
(v) number of special characters
Define a class Student with the following specifications:
Class name : Student
Data Members Purpose String name To store the name of the student int eng To store marks in English int hn To store marks in Hindi int mts To store marks in Maths double total To store total marks double avg To store average marks Member Methods Purpose void accept() To input marks in English, Hindi and Maths void compute() To calculate total marks and average of 3 subjects void display() To show all the details viz. name, marks, total and average Write a program to create an object and invoke the above methods.
Define a class called ParkingLot with the following description:
Class name : ParkingLot
Data Members Purpose int vno To store the vehicle number int hours To store the number of hours the vehicle is parked in the parking lot double bill To store the bill amount Member Methods Purpose void input( ) To input the vno and hours void calculate( ) To compute the parking charge at the rate ₹3 for the first hour or the part thereof and ₹1.50 for each additional hour or part thereof. void display() To display the detail Write a main method to create an object of the class and call the above methods.
Design a class RailwayTicket with following description:
Class name : RailwayTicket
Data Members Purpose String name To store the name of the customer String coach To store the type of coach customer wants to travel long mob no To store customer's mobile number int amt To store basic amount of ticket int totalamt To store the amount to be paid after updating the original amount Member Methods Purpose void accept() To take input for name, coach, mobile number and amount void update() To update the amount as per the coach selected (extra amount to be added in the amount as per the table below) void display() To display all details of a customer such as name, coach, total amount and mobile number Type of Coaches Amount First_AC ₹700 Second_AC ₹500 Third_AC ₹250 Sleeper None Write a main method to create an object of the class and call the above member methods.