Anshul transport company charges for the parcels of its customers as per the following specifications given below: Class name : Atransport Member variables: String name – to store the name of the customer int w – to store the weight of the parcel in Kg int charge – to store the charge of 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 charge as per the weight of the parcel as per the following criteria. Weight in Kg Charge per Kg Upto 10 Kgs Rs.25 per Kg Next 20 Kgs Rs.20 per Kg Above 30 Kgs Rs.10 per Kg A surcharge of 5% is charged on the bill. void print ( ) – to print the name of the customer, weight of the parcel, total bill 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 methods.
Answers
import java.util.Scanner;
class Bill
{
String n;
double w, charge=0.0, tot_charge=0.0;
public void accept(String name, double weight)
{
n=name;
w=weight;
}
public void calculate()
{
if(w<=10)
{
charge=25*w;
tot_charge=charge+0.05*charge;
}
if(w>10 && w<=30)
{
charge=20*w;
tot_charge=charge+0.05*charge;
}
if(w>30)
{
charge=10*w;
tot_charge=charge+0.05*charge;
}
}
public void print()
{
System.out.println("Name");
System.out.println("\t\t");
System.out.println("Weight");
System.out.println("\t\t");
System.out.println("Bill amount");
System.out.println("\n");
System.out.println(n + "\t\t");
System.out.println(w + "\t\t");
System.out.println(tot_charge + "\n");
}
}
class Final_bill
{
public static void main(String[] args)
{
String s;
double wt;
Scanner sc=new Scanner(System.in);
System.out.println("Enetr the name of the customer: ");
s=sc.nextLine();
System.out.println("Enetr the weight of the parcel: ");
wt=sc.nextDouble();
Bill bill_obj=new Bill();
bill_obj.accept(s, wt);
bill_obj.calculate();
bill_obj.print();
}
}
Answer:
Here is your code
Coded By Saptarshi Halder of DBB.
import java.util.*;
public class Atransport{// class begins
int w,charge;
String name;
void accept(){// method accept starts
Scanner ob=new Scanner(System.in);
System.out.println("Enter the name of customer");
name=ob.nextLine();
System.out.println("Enter the weight of parcel");
w=ob.nextInt();
}// method accept ends
void calculate()
{// method calculate starts
if(w<=10)
charge=w*25;
else if(w<=30)
charge=w*25 +(w-10)*20;
else
charge=10*25+20*20+(w-30)*10;
charge=charge+(5/100)*charge;
} // method calculate ends
void print() // method print begins
{
System.out.println("Name\t weight\t Bill Amount");
System.out.println(name+"\t"+w+"\t"+charge);
} // method print ends
public static void main(String args[])
{// method main starts
Atransport obj=new Atransport();
obj.accept();
obj.calculate();
obj.print();
}// method main ends
}