DTDC, a courier company, charges for the courier based on the weight of the parcel. Define a class with the following specifications: class name:courierMember variables:name - name of the customer
Advertisements
प्रश्न
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 | |
थोडक्यात उत्तर
Advertisements
उत्तर
import java.util.*;
class DTDC
{
String name, address;
double weight, bill;
char type;
public 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 parcel in kilograms:");
weight=sc.nextDouble();
Sytem.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=(char)sc.next().charAt(0);
}
public void calculate()
{
if(weight>0)
{
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+=1500;
}
else
System.out.println("Invalid weight given");
}
public 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 arg[])
{
DTDC obj=new DTDC();
obj.accept();
obj.calculate();
obj.print();
}
}
Output:
Enter the name of the customer: Alok Kumar Singh.
Enter the weight of the parcel in kilograms: 55.
Enter the address of the recipient: New Delhi.
Enter the type: D for domestic and I for international: D
Name of the customer: Alok Kumar Singh
Weight of the parcel: 55.0 kilograms
Address of the recipient: New Delhi
Type of the parcel: D
Amount to be paid: 30000.0
shaalaa.com
Using Function Argument
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
