Exercise 9        Name __________________        Score __/59

Modified: 

    Do Question 8 first, it should help in the following.

  1. (3) Define:
    1. an array variable named tmA that can hold references for a TicketMachine object,

      TicketMachine tmA [];
       
    2. an ArrayList variable named tmAL that can hold references for a TicketMachine object,

      ArrayList tmAL;
       
    3. an HashMap variable named tmHM that can hold references for a TicketMachine object.


      HashMap tmHM;


       
  2. (6) Define a constructor with no parameters for the class named Lottery that initializes each variable defined in Question 1 to three different TicketMachines with a price of 2000 cents.


    public Lottery () {
        tmA = new TicketMachine[3];
        for (int i=0; i<3; i++)
            tmA[i]=new TicketMachine(2000);

        tmAL = new ArrayList();
        for (int i=0; i<3; i++)
            tmAL.add( new TicketMachine(2000) );
         
        tmHM = new HashMap();
        tmHM.put("zero", new TicketMachine(2000) );
        tmHM.put("two", new TicketMachine(2000) );
        tmHM.put("three", new TicketMachine(2000) );
    }
     
  3. (3) Complete the following constructor to initialize tmA to n TictketMachines with a price of 1000 cents.

        public Lottery ( int n ) {
            tmA = new TicketMachine[n];
            for (int i=0; i<n; i++)
                tmA[i]=new TicketMachine(1000);
        }
     
  4. (3) Complete the following constructor to initialize tmAL to n TictketMachines with a random price between 1000 and 3000 cents.

        public Lottery ( int n ) {
            Random randomPrice = new Random();
            tmAL = new ArrayList();
            for (int i=0; i<n; i++)
                tmAL.add(new TicketMachine(randomPrice.nextInt(2001)+1000));
        }
     
  5. (3) Define a Lottery method highestPrice that returns the highest price of any TicketMachine referenced by tmAL.

        public int highestPrice() {

            int highest = ((TicketMachine)tmAL.get(0)).getPrice();

            for (int i=0; i<tmAL.size(); i++)
                if ( ((TicketMachine)tmAL.get(i)).getPrice() > highest )
                    highest = ((TicketMachine)tmAL.get(i)).getPrice();

            return highest;
         }

     

  6. (3) Define a Lottery method highestPrice that returns the highest price of any TicketMachine referenced by tmA.

        public int highestPrice() {

            int highest = tmA[0].getPrice();

            for (int i=0; i<tmA.length; i++)
                if ( tmA.[i].getPrice() > highest )
                    highest = tmA.[i].getPrice();

            return highest;
         }

  1. public class A {
  2.  private String S;
     
  3.  public A (String theS)
  4. {
  5.       S = theS;
  6.  }
     
  7.  public void print ()
  8.  {
  9.       System.out.println( S );
  10.  }
  11. }
  1. public class B extends A {
  2.  private double D;
     
  3.  public B (double theD, String S)
  4.  {
  5.       super(S);
  6.       D = theD;
  7.  }
     
  8.  public void print ()
  9.  {
  10.       super.print( );
  11.       System.out.println( D );
  12.  }
  13. }
  1. public class Seven {
  2.   public Seven()
  3.   {
  4.   }
     
  5.   public void printA( A anA )
  6.   {
  7.       anA.print();
  8.   }
     
  9.   public void printB( B aB )
  10.   {
  11.       aB.print();
  12.   }
  13. }
  1. (13) Trace the lines of execution for the following that are valid or indicate those that are invalid:
    1. A a7 = new A("red");                3-6

    2. a7.print();                               7-10

    3. B b7 = new B(15.0, "white");     14-16, 3-6, 17-18

    4. b7.print();                               19-21, 7-10, 22-23

    5. Seven seven7 = new Seven();    26-28

    6. seven7.printA( a7 );                  29-31, 7-10, 32

    7. seven7.printB( b7 );                  33-35, 19-21, 7-10, 22-23, 36

    8. seven7.printA( b7 );                  29-31, 19-21, 7-10, 22-23, 32

    9. seven7.printB( a7 );                  Invalid, cannot pass a supertype to a subtype.

    10. A j = b7;                                 Valid, no lines executed.

    11. B k = a7;                                 Invalid, cannot assign a supertype to a subtype.

    12. B k = (B) a7;                            Valid, no lines executed.

    13. seven7.printA( j );                    29-31, 19-21, 7-10, 22-23, 32

    14. seven7.printB( k );                    Run time error, k references a supertype a7 from Question l.
       

  2. (16) What is the result of the following fragments?

  1. for (int i=0; i<3; i++)                        i = 0 i = 1 i = 2
    {
        System.out.print(" i =");
        System.out.print( i );
    }

     
  2. for (int i=0; i<3; i++)                        compile error because i is not defined
        System.out.print(" i =");
    System.out.print( i );

     
  3. public boolean test() {                       false
        int x = 3;
        if (x == 4)
            return true;
        return false;
    }

     
  4. public boolean test() {                       false
        int x = 3;
        return x == 4;
    }
     
  1. int [] a = new int[ 4 ];                      
    a[0] = 30;
    a[1] = 40;
    a[2] = 25;
    a[3] = 16;
    int x = a[0];

    for (int i=0; i<a.length; i++)
        if ( a[i] > x )
            x = a[ i ];

    System.out.println( x );                    40

     
  2. TicketMachine [] a = new TicketMachine[ 4 ];       
    a[0] = new TicketMachine(30);
    a[1] = new TicketMachine(40);
    a[2] = new TicketMachine(25);
    a[3] = new TicketMachine(16);
    TicketMachine x = a[ 0 ];

    for (int i=0; i<a.length; i++)
        if ( a[i].getPrice() > x.getPrice() )
            x = a[ i ];
    System.out.println( x.getPrice() );        40

     
  3. TicketMachine [] a = new TicketMachine[ 4 ];       
    a[0] = new TicketMachine(30);
    a[1] = new TicketMachine(40);
    a[2] = new TicketMachine(25);
    a[3] = new TicketMachine(16);

    int x = a[ 0 ].getPrice();

    for (int i=0; i<a.length; i++)
        if ( a[i].getPrice() > x )
            x = a[ i ].getPrice();

    System.out.println( x );                    40

     
  4. ArrayList a = new ArrayList(); 
    a.add( new TicketMachine(30));
    a.add( new TicketMachine(40));
    a.add( new TicketMachine(25));
    a.add( new TicketMachine(16));

    int x = ((TicketMachine) a.get(0)).getPrice();

    for (int i=0; i<a.size(); i++)
        if ( ((TicketMachine) a.get( i )).getPrice() > x )
            x = ((TicketMachine) a.get( i )).getPrice();

    System.out.println( x );                   40
  1. (8) Diagram the variable a in 8e-h.