Exercise 2.52 After a ticket has been printed, could the value in the balance field ever be set to a negative value by subtracting price from it? Justify your answer.
if(balance >= price) {
// Simulate the printing of a 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();
// Simulate the printing of a 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();
// Update the total collected with the price.
total = total + price;
// Reduce the balance by the price.
balance = balance - price;
}
total = total + price;
// Reduce the balance by the price.
balance = balance - price;
}
No because the operation to subtract the value in price by the value in balance will only be executed if there is enough balance in the balance field. Whatever money is in balance; in order to perform the subtraction there has to be an amount greater than or equal to the price of the ticket. The result of the calculation will be whatever is remaining after the purchase of the ticket. If the ticket cost 50 and the balance is 50 the result will be 0. Whatever is subtracted from balance is never greater than the balance itself; therefore it will never be less than zero (0).
Exercise 2.54 Write an assignment statement that will store the result of multiplying two variables, price and discount, into a third variable, saving.
saving = price * discount;
Exercise 2.55 Write an assignment statement that will divide the value in total by the
value in count and store the result in mean.
value in count and store the result in mean.
mean = total / count;
Exercise 2.56 Write an if statement that will compare the value in price against the value in budget. If price is greater than budget, then print the message “Too expensive”; otherwise print the message “Just right”.
if (price > budget) {
System.out.println("Too expensive");
}
else {
System.out.println("Just right");
}
Exercise 2.57 Modify your answer to the previous exercise so that the message includes
the value of your budget if the price is too high.
the value of your budget if the price is too high.
if (price > budget) {
System.out.println("Too expensive, you only have: " + budget);
}
else {
System.out.println("Just right");
}
Exercise 2.58 Why does the following version of refundBalance not give the same results as the original?
public int refundBalance()
{
balance = 0;
return balance;
}
What tests can you run to demonstrate that it does not?
{
balance = 0;
return balance;
}
What tests can you run to demonstrate that it does not?
It gives a different result because it doesn't take the remaining value after the ticket purchase and store it into a local variable that will be the remaining value used to store the amount to be refunded. It reassign the balance to zero (0) and doesn't refund anything. The money is then lost and we can say that it steals money from the costumer if the costumer inserts more than the exact value of the ticket price. To see this flaw we call the getBalance method to see the remaining balance and then call the refundBalance method to see that the current balance wasn't returned as a refund to the user.
Exercise 2.59 What happens if you try to compile the TicketMachine class with the following version of refundBalance?
public int refundBalance()
{
return balance;
balance = 0;
}
What do you know about return statements that helps to explain why this version does not compile?
public int refundBalance()
{
return balance;
balance = 0;
}
What do you know about return statements that helps to explain why this version does not compile?
The compiler won't compile because there is an unreachable statement.
A return statement should be the last statement within the code block. Anything after return will be unreachable. The return statement tells the method to exit; therefore anything after it won't be reachable.
Exercise 2.60 What is wrong with the following version of the constructor of TicketMachine?
public TicketMachine(int cost)
{
int price = cost;
balance = 0;
total = 0;
}
Does this version compile? Create an object and then inspect its fields. Do you notice something wrong about the value of the price field in the inspector with this version? Can you explain why this is?
public TicketMachine(int cost)
{
int price = cost;
balance = 0;
total = 0;
}
Does this version compile? Create an object and then inspect its fields. Do you notice something wrong about the value of the price field in the inspector with this version? Can you explain why this is?
The program compiles but the value in cost is not stored in a field, it is stored in a local variable that only holds the value temporarily. A local variable last as long as a the execution of the method it belongs to lasts. If we call the getPrice method after creating an object from the TicketMachine class and we pass in a value through the cost parameter, that value will be zero(0) because the getPrice accessor returns the value stored in the price field and we haven't supply any value to it. The value we enter when we constructed the object was store into the price local variable and not the price field because A local variable of the same name as a field will prevent the field being accessed from within a constructor or method.
Exercise 2.61 Add a new method, emptyMachine, that is designed to simulate emptyingthe machine of money. It should reset total to be zero but also return the value that was stored in total before it was reset.
public int emptyMachine() {
int oldTotal;
oldTotal = total;
total = 0;
return oldTotal;
}
to be continued...
to be continued...
No comments:
Post a Comment