04A – CSKC
Sign In

Search





Q&A #7253 siteicon  

Question:

Define a class ElectricBill with the following specifications:

class : ElectricBill
Instance variables / data member:
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:
Number of units Rate per unit
First 100 units Rs.2.00
Next 200 units Rs.3.00
Above 300 units Rs.5.00
A surcharge of 2.5% charged if the number of units consumed is above 300 units.
void print ( ) – To print the details as follows:
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.

Answer:

import java.util.Scanner;

public class ElectricBill {

private String n;
private int units;
private double bill;

public void accept() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter name: ");
n = scanner.next();
System.out.print("Enter units: ");
units = scanner.nextInt();
}

public void calculate() {
if (units <= 100) { bill = units * 2; } else if (units > 100 && units <= 300) {
bill = 100 * 2 + (units - 100) * 3;
} else {
bill = 100 * 2 + 200 * 3 + (units - 300) * 5;
double surcharge = bill * 2.5 / 100;
bill = bill + surcharge;
}
}

public void print() {
System.out.println("Name of the customer: " + n);
System.out.println("Number of units consumed: " + units);
System.out.println("Bill amount: " + bill);
}

public static void main(String[] args) {
ElectricBill electricBill = new ElectricBill();
electricBill.accept();
electricBill.calculate();
electricBill.print();
}
}

Sample output
Enter name: Sai
Enter units: 50
Name of the customer: Sai
Number of units consumed: 50
Bill amount: 100.0


Share

C. Sir| Created: 15-Apr-2019 | Updated: 11-Jul-2026|ICSE2017









×
ssignment Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning Display Using IO Manipulation User Defined Data Types Enumerated Types Preprocessor Directives And Macros Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constructor Concepts Default Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Mult-Level Inheritance Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Stacks Queues Introductory Sessions Beginning to Program Tokens Keywords Identifiers Literals Data Types Variables and Constants Operators (Arithmetic-Assignment-Unary-Relational-Logical) Expressions Statements and Formula Based Calculations Class Object Creation and Access Wrapper Classes Constructor Basics Conversion and Checking Functions Math Functions Function building and calling Simple If and If-Else Selection Switch Based Selections Basic Loop Types Array Basics Array Search Array Sorting Character Handling String Basics String Class Functions Types of Errors Try Catch Introductory Sessions Beginning to Program Introductory Sessions Beginning to Program Introductory Sessions Beginning to Program Data Types Variables and Constants Introductory Sessions Beginning to Program Date and Time Handling Regular Expressions Command Line Arguments Concurrent Execution Making Executable File Python Introduction (Features Python Introduction (Features, Installation, Modes) Handling Unicode Internet Interactions Imaging and Compression Special Data File Handling Python Basics Cryptography Blockchain and Cryptocurrency Generative AI keywords, identifiers, variables Beginning To Program Tokens, Keywords, Identifiers Variables and Expressions Data Types Operators Concept Learning Examples Operators Solved Practical Examples elational nary)