Q2A. Write a function called travelCost, that returns the price to be paid by a
ID: 3819574 • Letter: Q
Question
Q2A. Write a function called travelCost, that returns the price to be paid by a customer purchasing tickets. The function takes 3 input arguments:
ticketPrice, the cost of a single ticket, before the 13% Ontario Harmonized Sales Tax(HST).
passengers, the number of passengers for this purchase;
hstExempt: if true, this indicates this fare purchase should not be charged the 13% HST.
For examples, a silent purchasing tickets on behalf of three HST-exempt passengers paying $40.45 per fare would owe $121.35 (ie 3*$40.45); if the purchase was not HST-exempt, the purchaser would pay $137.13 (ie. 3*$40.45*1.13).
Explanation / Answer
//Java code to calculate travelCost
import java.text.DecimalFormat;
public static double travelCost(double ticketPrice , int passengers , boolean hasExempt) {
DecimalFormat df = new DecimalFormat("#.##");
if(hasExempt) {
// if hasExempt is true , the code will not impose HST
return Double.valueOf(df.format(ticketPrice*passengers));
}
return Double.valueOf(df.format(ticketPrice*passengers*1.13)) ;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.