InterviewSolution
| 1. |
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 : A surcharge of 2.5% charged if the number of units consumed is above 300 units. Write a main method to create an object of the class and call the above member methods. |
|
Answer» import java.io.*; class ElectricBill { String n; int units; double bill ; void accept() throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.print(“Name of the customer = “); n = reader.readLine( ); System.out.print( “Number of units consumed = “); String un = reader. readLine( ); units=Integer .parselnt(un); } void calculate!) { if(units < = 100) bill = 2.00*units; if(units< =300) bill = 3.00*units; if (units > 300) bill = 5.00*units; } void print( ) { System.out.println(“Naine of the customer:” +n); System.out.println(“Number of units consumed:” +units); System.out.println(“Bill amount:” + bill); } public static void main(String argsQ) throws IO Exception { ElectricBill eb = new ElectricBill( ); eb. accept!); eb.calculate(); } } |
|