1. (8) What
is the final value of balance in each case?
a.
balance
= 12;
balance = balance * 3 + 14 /
5;
_________________
b.
balance
= 13;
limit = 4;
if (balance>= limit+5)
balance = balance +
limit;
else
balance = balance +
5;
_________________
c.
balance
= 8;
balance = (balance + 5) / 12;
_________________
d.
balance
= 8;
limit = 13;
balance = (balance + 5) %
limit;
_________________
2. (10) What
is the result of the following when the int type price
is 54?
e. "# "
+ price + " cents."
_________________
f. "price"+"5"
_________________
g. price+5
_________________
h. “abcdefg".length()
_________________
i. “abcdefg".substring(3,5).length()
_________________
3.
(12) What is the following when limit = 4?
a. !false && (false || true)
_________________
b. limit*2 > 5 && 6*2
>= 5
_________________
c. boolean correct = false;
!correct && (false || true)
_________________
d. if ( limit < 5 && 6*2
>= 5 )
System.out.println(
"Done");
else
System.out.println(
"NOT Done");
_________________
For n=0, 1, 2, 3, 4, 5, 6, ...
What are the possible results of:
4. (6) Circle the type of
method call for each of the following:
5. (42) Use the TicketMachine definition on the last page in answering the
following questions:
TicketMachine tm = new TicketMachine( 50 );
tm.insertMoney( 20 );
tm.insertMoney( 40 );
tm.printDetails();
TicketMachine tm = new TicketMachine(
50 );
tm.insertMoney( 20 );
tm.insertMoney( 40 );
tm.printDetails();
tm.refundBalance();
total ________________ balance
____________________
6. (18) For
the class Subway below:
public class Subway
{
}
public class TicketMachine
{
private int
price;
private int
balance;
private int
total;
public TicketMachine(int ticketCost)
{
price = ticketCost;
balance = 0;
total = 0;
}
public void insertMoney(int amount)
{
if(amount > 0)
balance += amount;
else
System.out.println("Use
a positive amount: " + amount);
}
public void printTicket()
{
if(balance >= price) {
System.out.println("##################");
System.out.println("#
The BlueJ Line");
System.out.println("#
Ticket");
System.out.println("#
" + price + " cents.");
System.out.println("##################");
System.out.println();
total += price;
balance -= price;
}
else {
System.out.println("You
must insert at least: " +
(price -
balance) + " more cents.");
}
}
public int refundBalance()
{
int amountToRefund;
amountToRefund
= balance;
balance = 0;
return amountToRefund;
}
}