KnowledgeBoat Logo

Computer Applications

Define a class Bill that calculates the telephone bill of a consumer with the following description:

Data MembersPurpose
int bnobill number
String namename of consumer
int callno. of calls consumed in a month
double amtbill amount to be paid by the person
Member MethodsPurpose
Bill()constructor to initialize data members with default initial value
Bill(…)parameterised constructor to accept billno, name and no. of calls consumed
Calculate()to calculate the monthly telephone bill for a consumer as per the table given below
Display()to display the details
Units consumedRate
First 100 calls₹0.60 / call
Next 100 calls₹0.80 / call
Next 100 calls₹1.20 / call
Above 300 calls₹1.50 / call

Fixed monthly rental applicable to all consumers: ₹125

Create an object in the main() method and invoke the above functions to perform the desired task.

Java

Java Constructors

91 Likes

Answer

import java.util.Scanner;

public class Bill
{
    private int bno;
    private String name;
    private int call;
    private double amt;
    
    public Bill() {
        bno = 0;
        name = "";
        call = 0;
        amt = 0.0;
    }
    
    public Bill(int bno, String name, int call) {
        this.bno = bno;
        this.name = name;
        this.call = call;
    }
    
    public void calculate() {
        double charge;
        if (call <= 100)
            charge = call * 0.6;
        else if (call <= 200)
            charge = 60 + ((call - 100) * 0.8);
        else if (call <= 300)
            charge = 60 + 80 + ((call - 200) * 1.2);
        else
            charge = 60 + 80 + 120 + ((call - 300) * 1.5);
            
        amt = charge + 125;
    }
    
    public void display() {
        System.out.println("Bill No: " + bno);
        System.out.println("Name: " + name);
        System.out.println("Calls: " + call);
        System.out.println("Amount Payable: " + amt);
    }
    
    public static void main(String args[]) {
        
        Scanner in = new Scanner(System.in);
        System.out.print("Enter Name: ");
        String custName = in.nextLine();
        System.out.print("Enter Bill Number: ");
        int billNum = in.nextInt();
        System.out.print("Enter Calls: ");
        int numCalls = in.nextInt();
        
        Bill obj = new Bill(billNum, custName, numCalls);
        obj.calculate();
        obj.display();
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Define a class Bill that calculates the telephone bill of a consumer with the following description: Fixed monthly rental applicable to all consumers: ₹125 Create an object in the main() method and invoke the above functions to perform the desired task.

Answered By

37 Likes


Related Questions