C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Class & Object Based Program - Java Programs

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
Up to 10 kg Rs. 25 per kg
Next 20 kg Rs. 20 per kg
Above 30 kg 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 the class with the above mentioned specifications, create a main() method, create an object and invoke the member methods.

import java.util.*; class ATransport{ String name; int w; int charge; public void accept(){ Scanner sc = new Scanner(System.in); System.out.print("Name: "); name = sc.nextLine(); System.out.print("Weight of the parcel in kg: "); w = sc.nextInt(); } public void calculate(){ if(w <= 10) charge = w * 25; else if(w <= 30) charge = 250 + (w - 10) * 20; else charge = 450 + (w - 30) * 10; charge += (int)(5.0 / 100 * charge); } public void print(){ System.out.println("Name\tWeight\tBill Amount"); System.out.println(name + "\t" + w + "\t" + charge); } public static void main(String args[]){ ATransport obj = new ATransport(); obj.accept(); obj.calculate(); obj.print(); } }

Output

 
 OUTPUT 1: 
Name: Anand Rao
Weight of the parcel in kg: 11
Name    Weight  Bill Amount
Anand Rao   11  283 

 OUTPUT 2: 
Name: Ajay Singh
Weight of the parcel in kg: 21
Name    Weight  Bill Amount
Ajay Singh  21  493