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,
    2. an ArrayList variable named tmAL that can hold references for a TicketMachine object,
    3. a HashMap variable named tmHM with a String key and a TicketMachine value.






       
  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. Three TicketMachines for tmA, three ticketMachines for tmAL, and three TicketMachines for tmHM.











     
  3. (3) Complete the following constructor to initialize tmA to n TictketMachines with a price of 1000 cents.

        public Lottery ( int n )






     
  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 )





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



     

     

     

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




     





 

  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. (14) Trace the lines of execution for the following that are valid or indicate those that are invalid:
    1. A a7 = new A("red");

    2. a7.print();

    3. B b7 = new B(15.0, "white");

    4. b7.print();

    5. Seven seven7 = new Seven();

    6. seven7.printA( a7 );

    7. seven7.printB( b7 );

    8. seven7.printA( b7 );

    9. seven7.printB( a7 );

    10. A j = b7;

    11. B k = a7;

    12. B k = (B) a7;

    13. seven7.printA( j );

    14. seven7.printB( k );
       

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

    1. for (int i=0; i<3; i++)
      {
          System.out.print(" i =");
          System.out.print( i );
      }
       
    2. for (int i=0; i<3; i++)
          System.out.print(" i =");
          System.out.print( i );
       
    3. public boolean test() {
          int x = 3;
          if (x == 4)
              return true;
          return false;
      }
       
    4. public boolean test() {
          int x = 3;
          return x == 4;
      }
       
    5. 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 );
       
    6. 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() );
       
    7. 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 );
       
    8. ArrayList<TicketMachine> a = new ArrayList<TicketMachine>();
      a.add( new TicketMachine(30));
      a.add( new TicketMachine(40));
      a.add( new TicketMachine(25));
      a.add( new TicketMachine(16));
      int x = a.get(0).getPrice();

      for (int i=0; i<a.size(); i++)
          if ( a.get( i ).getPrice() > x )
              x =a.get( i ).getPrice();
      System.out.println( x );
       
  1. (8) Diagram the variable a in 8e-h.
    1.  
    2.  
    3.