Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 2.62 Rewrite the printTicket method so that it declares a local variabl

ID: 3750126 • Letter: E

Question

Exercise 2.62 Rewrite the printTicket method so that it declares a local variable, amountLeftToPay. This should then be initialized to contain the difference between price and balance. Rewrite the test in the conditional statement to check the value of amountLeftToPay. If its value is less than or al to zero, a ticket should be printed; otherwise, an error message should be printed stating the amount left to pay. Test your version to ensure that it behaves in exactly the same way as the original version. Make sure that you call the method more than once, when the machine is in different states, so that both parts of the conditional statement will be executed on separate occasions.

Explanation / Answer

Java Updated Method:

public void printTicket()
{
   //Variable to hold amount left
   double amountLeftToPay;
  
   //Difference between price and balance
   amountLeftToPay = price - balance;
  
   //Checking condition
   if(amountLeftToPay <= 0)
   {
       //Printing ticket
       System.out.println("###################");
       System.out.println("# The BlueJ Line");
       System.out.println("# Ticket");
       System.out.println("# " + price + " cents.");
       System.out.println("###################");
       System.out.println("");
   }
   else
   {
       System.out.println(" Sorry!!! You are only left with " + amountLeftToPay + " ");
   }
}