Question 6

Define a class taximeter having the following description:

Data members/instance variables

int taxino                :      to store taxi number

String name             :      to store passenger’s name

int km                     :      to store number of kilometres travelled

Member functions  :

taximeter()              :      constructor to initialize taxino to 0, name to”” and km to 0.

input()                     :      to store taxino,name,km

 calculate()               :      to calculate bill for a customer according to kilometers travelled 1st 5 Kilometer  Rs 25/- for every additional kilometer Rs 3/-

display()                 :      To display the details in the following format

Taxino    Name         Kilometres travelled       Bill amount

 Create an object in the main method and call all the above methods in it

public class TAXIMETER
{
    int taxino;
    String name;
    int km;
    public TAXIMETER()
    {
     taxino = 0;
     name ="";
     km=0;
    }
    public void input(int a, String b, int c)
    {
     taxino = a;
     name = b;
     km = c;   
    }
    public int calculate()
    {
        int amt=0;
        if(km<=5)
           amt=25;
        else
           amt = 25 + (km-5)*3;
        return amt;
    }
    public void display()
    {
        System.out.println(" Taxi number : "+taxino);
        System.out.println(" Name : "+name);
        System.out.println(" Kilometer travelled : "+km);
        System.out.println(" Bill amount : "+calculate());
    }
    public static void main()
    {
        TAXIMETER t1 = new TAXIMETER();
        t1.input(2013,"xxxx xxx",45);
        t1.display();
    }                  
}
This entry was posted in Term 1. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *