Define a class Tour with the following description:
- Members are : TCode of type String, NoofAdults of type integer, NoofKids of type integer, Kilometers of type integer, TotalFare of type float.
- Member functions :
- A constructor to assign initial values as follows:
- TCode with word “NULL”, Other members initialize with 0.
- A function AssignFare() which calculates and assigns the value of the data member TotalFare as follows:
- For each Adult
Fare (Rs) | For Kilometers |
500 | >=1000 |
300 | <1000 and >=500 |
200 | <500 |
- For each kid the above Fare will be 50% of the fare mentioned in the above table.
- A function EnterTour() to input the value of the data members TCode, NoofAdults, NoofKids and Kilometers; and invoke the AssignFare() function.
- A function ShowTour() which displays the content of all the data members for a Tour.
Solution
import java.util.*; class Tour { String TCode; int NoofAdults; int NoofKids; int Kilometers; float TotalFare; //define a constructor Tour () { TCode=null; NoofAdults=0; NoofKids=0; Kilometers=0; TotalFare=0.0f; } public void AssignFare(){ if(Kilometers>=1000) { TotalFare=NoofAdults*500+NoofKids*250; } if(Kilometers>=500 && Kilometers<1000) { TotalFare=NoofAdults*300+NoofKids*150; } if(Kilometers<500) { TotalFare=NoofAdults*200+NoofKids*100; } } public void EnterTour(){ Scanner sc = new Scanner(System.in); System.out.println("Enter Travel Code : "); TCode=sc.next(); System.out.println("Enter Number of Adults : "); NoofAdults=sc.nextInt(); System.out.println("Enter Number of Kids : "); NoofKids=sc.nextInt(); System.out.println("Enter Distance in Kilometers : "); Kilometers=sc.nextInt(); AssignFare(); } public void ShowTour(){ System.out.println("Travel Code : "+TCode); System.out.println("No of Adults : "+NoofAdults); System.out.println("No of Kids : "+NoofKids); System.out.println("Dist in Kms : "+Kilometers); System.out.println("Total Fare : "+TotalFare); } } class Travel { public static void main(String args[]) { Tour ob=new Tour(); ob.EnterTour(); ob.ShowTour(); } }
Save the above Program with “Travel.Java” then compile it with the following commands :
javac Travel.java //to compile the code java Travel // to run the code
Output is
Enter Travel Code : 101 Enter Number of Adults : 4 Enter Number of Kids : 3 Enter Distance in Kilometers : 2400 Travel Code : 101 No of Adults : 4 No of Kids : 3 Dist in Kms : 2400 Total Fare : 2750.0