Class name : ETLimited
Member variables : String cname – to store the name of the customer
int wt – to store the weight of the parcels in kilogram (kg)
double charges – to store the charges for the parcel
Member functions : void accept ( ) – to accept the name of the customer, weight of the parcel
from the user (using Scanner class)
void calculate ( ) – to calculate the charges as per the weight of the parcel
as per the following criteria.
Weight in kg Charges per kg (in ₹)
Up to 20 kgs ₹ 50 per kg
Next 30 kgs ₹ 30 per kg
Above 50 kgs ₹ 20 per kg
A surcharge of 5 % is charged on the bill.
void display ( ) – to print the name of the customer, weight of the parcel,
total bill amount inclusive of surcharge in a tabular form
in the following format :
Name Weight Bill amount
------- -------- -------------
Define a class with the above mentioned specifications, create the main method, create an object
and invoke the member functions.
import java.util.*;
class ETlimited
{
int wt;
String cname;
Double charges;
Scanner in=new Scanner(System.in);
void accept()
{
System.out.println("Enter the name of the customer");
cname=in.nextLine();
System.out.println("Enter the weight of the parcels in kilogram (kg)");
wt=in.nextInt();
}
void calculate()
{
if(wt<=20)
charges=((5*50.0*wt/100)+50.0*wt);
if(wt>20 && wt<=30)
charges=((wt-20)*30.0+20*50)+(((wt-20)*30.0+20*50)*0.05);
if(wt>50)
charges=(((wt-50)*20.0+30.0*30+20.0*50)+(((wt-50)*20.0+30.0*30+20.0*50)*0.05));
}
void display()
{
System.out.println("Name"+"\t"+"\t"+"Weight"+"\t"+"\t"+"Bill Amount");
System.out.println(cname+"\t"+"\t"+wt+"\t"+"\t"+charges);
}
}
No comments:
Post a Comment