Name ___________________________________________                                                                                       Q1 ____ of  30

          Last, First Middle    [Please print legibly]                                                                        

                                                                                                                                                                                                Q2 ____ of  40

ID # ____________________________________________                                                   

                                                                                                Q3____ of  30

Signature ________________________________________

TOTAL ______ of  100

Section (circle one): 

1

M 1:25-2:15

A.J.Byrd

2

M 2:30-3:20

Jay Henniger

3

M 3:35-4:25

Jay Henniger

4

T 10:10-11:00

Oren Kurland

5

T 2:30-3:20

Joy Zhang

6

T 3:35-4:25

Joy Zhang

7

T 1:25-2:15

Oren Kurland

 

Instructions. This is a 1.5-hour, closed book exam.  There are 3 questions worth a total of 100 points.  Be sure that your packet contains all 3 questions and the pages are in order. Write all answers in this packet.  Answers will be graded on whether they are correct, and on whether they are legible and written in a clear style.  Comment your code where appropriate, but not excessively.  Small syntactic and punctuation errors will not be heavily penalized.  Concentrate on problem solving and presenting your solutions clearly.

 

You must use the code templates as provided, and may not alter or delete the code provided.  You may not write outside of the boxes and blanks, and you may not alter the structure of the given code templates.  Correct solutions may have empty boxes.

 

Don’t be alarmed if you don’t have to write much code in a box or blank for an answer, or if you need much less than 1.5 hours to complete the exam.  You may leave after a half hour, but not before.

 

 

 

 

 


Question 1 (30 points). 

Trace the sequence of assignments to variables made by the program segment given below when it is run with input data

2, 13

0, 0

-1, 2

and also show the output on the lines provided.  Repeated assignments of the same value to a variable should be repeated in the table.  You may not need all of the table columns or output lines that have been provided.

Assume that variable in has been declared such that expression in.readInt() inputs one integer value.   

int x, y, z;

 

x = in.readInt();

y = in.readInt();

while ( x != -1 )

{

      System.out.print(x + ”**” + y + ”=”);

      z = 1;

      while ( y > 0 )

      {

            if ( y % 2 == 0 )

                  {

y = y / 2;

x = x * x;

                  }

            else

                  {

                        y = y – 1;

                        z = z * x;

                  }

      }          

      System.out.println(z);

 

x = in.readInt();

y = in.readInt();

}

 

time à  

x

0

 

 

2

 

 

 

 

 

4

 

16

 

 

 

256

 

 

0

 

 

-1

y

 

0

 

 

13

 

12

 

6

 

3

 

2

 

1

 

0

 

 

0

 

 

z

 

 

0

 

 

1

 

2

 

 

 

 

 

32

 

 

 

8192

 

 

1

 

            continued à           

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Output:

2**13=8192 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

0**0=1  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Question 2 (40 points). 
Complete the program segment according to the following input/output specification.  Do not write any while-statements other than the two provided in the answer blank.  Declare any additional variables you use.  It is OK to leave a box empty.

Input. The input consists of a non-negative integer (call it n) followed by data for n bank accounts.  The data for each bank account consists of an integer initial balance followed by zero or more transaction, terminated by a 0.  A transaction is a non-zero integer, where a positive value is a deposit, and a negative value is a withdrawal.  Here is some sample input consisting of data for three bank accounts:

3

90 100 -82 200 0

75 0

150 100 0

Output.  The output for each bank account should all be on one line and should begin with the initial balance. If there are no transactions, output “no transactions”, otherwise output the total deposits, total withdrawals, and final balance. For the sample input given above, the output would be:

Initial: 90 Deposits: 300 Withdrawals: 82 Final: 308

Initial: 75 no transactions

Initial: 150 Deposits: 100 Withdrawals: 0 Final: 250

 

(Continued on next page.)

TokenReader in = new TokenReader(System.in);

int n;                // Number of accounts.

int initial;          // Initial balance of current account.

int deposits;         // Total deposits of current account so far.

int withdrawals;      // Total withdrawals of current account so far.

int transaction;      // Current transaction.

 

int j = 1;            // Current account number.

n = in.readInt();

     

 

while ( ___________ j <= n _____________________ )

{

 

initial = in.readInt();

deposits = 0;

withdrawals = 0;

System.out.print( ”Initial: ” + initial );

transaction = in.readInt();

 

 

   while ( ________ transaction != 0 ______________ )

   {

 

if ( transaction > 0 )

    deposits = deposits + transaction;

else

    withdrawals = withdrawals – transaction;

 

transaction = in.readInt();

     

   }

 

if (deposits + withdrawals == 0)

    System.out.println( ”no transactions” );

else {

    System.out.println(

      ” Deposits: ”  +  deposits +

      ” Withdrawals: ”  +  withdrawals  +

      ” Final: ”  +  (initial + deposits - withdrawals)

        );

     }

 

j = j + 1;

 

}

 

 


 

Question 3 (30 points).  Implement class Event according to the following specification:

 

The objects of class Event model happenings in the real world.  Each event is said to “happen” at the moment the corresponding Event object is constructed during the execution of a program.

 

before( Event e1, Event e2 )

Given two Event objects e1 and e2, invoking before(e1,e2)returns the value true if e1 happened (i.e., was constructed) before e2, and false otherwise.

 

iCameBefore( Event e2 )

Given two Event objects e1 and e2, invoking the iCameBefore method of object e1 on the object e2 returns the value true if e1 happened (i.e., was constructed) before e2, and false otherwise.

 

Here is some sample client code that uses the class Event that you are to write.

 

class TrivialApplication

{

      public static void main(String args[])

      {

            Event one   = new Event();

            Event two   = new Event();

            Event three = new Event();

            System.out.println( Event.before(one, two) );

            System.out.println( Event.before(two, one) );

            System.out.println( Event.before(two, three) );

            System.out.println( Event.before(two, two) );

            System.out.println( one.iCameBefore(two) );

            System.out.println( two.iCameBefore(one) );

            System.out.println( two.iCameBefore(three) );

            System.out.println( two.iCameBefore(two) );

      }

}

 

The output of this sample client application would be:

true

false

true

false

true

false

true

false

 

Your code should follow the precepts of good object-oriented programming, e.g., the principle of information hiding.

Note that there is no way to enquire about the actual time that an event occurred; it is only possible to ask about the relative orders of events.  Thus, it is sufficient to associate an event number (rather than a time) with each Event object.

Note also that the outcome of an invocation of before or iCameBefore depends only on the relative order in which the events in question occurred, and not on any other factor.

 

(Continued on next page.)
class
Event

{

// class variable declaration(s)

 

private static int nextId = 1;

 

 

// instance variable declaration(s)

 

private int id;

 

 

// constructor definition(s)

 

Event()

{

   id = nextId;

   nextId = nextId + 1;

}

 

 

// class method definition(s)

 

static Boolean before( Event e1, Event e2 )

{

   return e1.id < e2.id;

}

 

 

// instance method definition(s)

 

Boolean iCameBefore( Event e2 )

{

   return id < e2.id;

}

 

}