DTDC, a courier company, charges for the courier based on the weight of the parcel. Define a class with the following specifications: class name: courier Member variables: name - name of the customer weight - weight of the parcel in kilograms address - address of the recipient bill - amount to be paid type - 'D' domestic, 'I' international Member methods: void accept() - to accept the details using the methods of the Scanner class only. void calculate() - to calculate the bill as per the following criteria: Weight in Kgs Rate per Kg First 5 Kgs Rs. 800 Next 5 Kgs Rs. 700 Above 10 Kgs Rs. 500 An additional amount of Rs. 1500 is charged if the type of the courier is I (International) void print() - To print the details void main() - to create an object of the class and invoke the methods
Topic: Class with slab-rate billing
Answer
import java.util.Scanner;
class courier { String name, address; double weight, bill; char type;
void accept() { Scanner sc = new Scanner(System.in); System.out.print("Enter the name of the customer : "); name = sc.nextLine(); System.out.print("Enter the weight of the parcel in kilograms : "); weight = sc.nextDouble(); sc.nextLine(); System.out.print("Enter the address of the recipient : "); address = sc.nextLine(); System.out.print("Enter the type: D for domestic and I for international : "); type = sc.next().charAt(0); }
void calculate() { if(weight <= 5) bill = 800 * weight; else if(weight <= 10) bill = 5 * 800 + 700 * (weight - 5); else bill = 5 * 800 + 5 * 700 + 500 * (weight - 10);
if(type == 'I') bill = bill + 1500; }
void print() { System.out.println("Name of the customer : " + name); System.out.println("Weight of the parcel : " + weight + " kilograms"); System.out.println("Address of the recipient : " + address); System.out.println("Type of the parcel : " + type); System.out.println("Amount to be paid : " + bill); }
public static void main(String args[]) { courier ob = new courier(); ob.accept(); ob.calculate(); ob.print(); } }
A slab-rate class question with one extra twist — the international surcharge.
The slabs are cumulative. A 55 kg parcel does not cost 55 x 500; the first 5 kg are charged at 800, the next 5 at 700, and only the remaining 45 at 500: 5 x 800 = 4000 5 x 700 = 3500 45 x 500 = 22500 total = 30000, matching the paper’s own sample output.
The Rs. 1500 surcharge is applied AFTER the slab calculation and only when type is 'I'. Write it as a separate if, not inside the slab chain, or you would have to repeat it three times.
Two input details earn marks: compare a char with single quotes (type == 'I', never "I"), and put sc.nextLine() after nextDouble() to clear the leftover newline — without it the address would come back empty.
name String - name of the customer weight double - weight of the parcel in kilograms address String - address of the recipient bill double - amount payable, computed slab-wise type char - 'D' for domestic, 'I' for international sc Scanner - object used to read input ob courier - object of the class used in main()