Test 1 Name _______________   Password _______________

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:

  1. n % 2           _____________
  2. n % limit      _____________

4. (6) Circle the type of method call for each of the following:

  1. student.printDisplay();                          External                  Internal       
  2. hour.setValue();                                   External                  Internal
  3. setValue(minute);                                 External                  Internal

5. (42) Use the TicketMachine definition on the last page in answering the following questions:

  1. (4) List the fields of a TicketMachine instance.

    ________________________________________________________

  2. (2) List the local variables of the TicketMachine methods.

    ________________________________________________________

  3. (4) List the formal parameters of the TicketMachine methods and constructors.

    ________________________________________________________

  4. (2) Is insertMoney a mutator or accessor method?

    ________________________________________________________

  5. (6) Draw the object diagram after the execution of the following:

TicketMachine tm = new TicketMachine( 50 );

tm.insertMoney( 20 );
tm.insertMoney( 40 );
 
tm.printTicket();











  1. (6) What is the value of total and balance after the execution of the following:


TicketMachine tm = new TicketMachine( 50 );

tm.insertMoney( 20 );
tm.insertMoney( 40 );

tm.printTicket();

tm.refundBalance();

total ________________  balance ____________________

  1. (3) What value is returned by the tm.refundBalance() statement above?

    _____________________________________________________


  2. (5) Define another TicketMachine constructor that initializes fields price and total to the values of two actual parameters to the constructor. For example:

                new TicketMachine( 100, 200 );

             would create a TicketMachine, setting the price and total to 100 and 200 respectively.











  1. (5) Define a TicketMachine emptyMachine method that returns the value of total and sets total to zero.












  1. (5) Define a TicketMachine raisePrice mutator method that adds the value of the formal parameter to the price but only when the resulting price is greater than zero. The formal parameter is of type int.








 

6. (18) For the class Subway below:

  1. Add three fields of class TicketMachine.

  2. Draw the class diagram.

  3. Define a Subway constructor with no parameters that initializes one of the TicketMachine fields to a TicketMachine with price of 50 cents, the second to a TicketMachine with price of 40 cents, and the third to a TicketMachine with price of 75 cents.

  4. Define the method emptyAllMachines with no parameter that returns the sum of the total in all three TicketMachines. Hint: Use an external call to your emptyMachine method.

 

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 = 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 = total + price;
            balance = 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;
    }
}