Exercise 3        Name __________________        Score __/20

  1. (4) Give the object diagram for the following fixed size array declarations similar to a right for: int [] hours = int[24];
    1. int [ ] A;
      A = new int [ 4 ];

    2. boolean [ ] B;
      B = new boolean [ 3 ];

       

     public class NumberDisplay
     {
     private int limit;
     private int value;
      public NumberDisplay(int rollOverLimit)
     {
         limit = rollOverLimit;
         value = 0;
     }
      public int getValue()
     {
         return value;
     }
      public void setValue(int replacementValue)
     {
         if((replacementValue >= 0) && (replacementValue < limit))
              value = replacementValue;
     }
      public void increment()
     {
         value = (value + 1) % limit;
     }
     }

     

  2. (3) Give the object diagram for the following fixed size array declaration similar to that of the TicketMachine at right.

    Use the NumberDisplay definition.

    NumberDisplay [ ] nd;
    nd = new NumberDisplay [ 2 ];

  3. (4) From Question 1 add the following assignments to the object diagram:
    1. A
      • variable 0 the value -2
      • variable 1 the value -3
      • variable 2 the value -7
      • variable 3 the value -5
    2. B
      1. variable 0 the value true
      2. variable 1 the value false
      3. variable 2 the value true
         
  4. (3) From Question 3, what is:
     
    1. for (int i = 0; i < 4; i++)
          System.out.println(A [ i ] );
       
    2. for (int i = 3; i > 0; i--)
          System.out.println(A [ i ] );
       
    3. for (int i = 0; i < 4; i++)
          System.out.println(B [ i ] );
       
    4. int sum = 0;
      for (int i = 0; i < 4; i++)
          sum = A [ i ] + sum;
      System.out.println( sum );
       
    5. boolean sum = true;
      for (int i = 0; i < 3; i++)
          sum = B [ i ] && sum;
      System.out.println( sum );
       
  5. (5) Add to the object diagram of Question 2 the results of the following similar to that at right for the TicketMachine example in Chapter 4 notes:

    NumberDisplay [ ] nd;
    nd = new NumberDisplay [ 2 ];

    nd[ 0 ] = new NumberDisplay( 10 );
    nd[ 1 ] = new NumberDisplay( 24 );

    nd[ 0 ].increment();
    nd[ 1 ].setValue( 20 );

  6. (1) Continuing Question 5, what is:

    nd[ 1 ].getDisplayValue( );