Exercise 6        Name __________________        Score __/17

Modified: 

  1. (2) Define an array named vowels of type char initialized to contain the vowels a, e, i, o, u.
     
    char vowels[] = new char[5];       
    vowels[0] = 'a';
    vowels[1] = 'e';
    vowels[2] = 'i';
    vowels[3] = 'o';
    vowels[4] = 'u';
    char vowels[] = { 'a', 'e', 'i', 'o', 'u' };
  2. (2) Define an array named color of String initialized to contain the Strings "red", "white", "yellow" and "blue".
     
    String color[]=new String[4];
    color[0] = "red";
    color[1] = "white";
    color[2] = "yellow";
    color[3] = "blue";
    String color[] = {  "red", "white", "yellow", "blue" };

     

  3. (2) Diagram the following fixed size array declaration similar to at right for NumberDisplay:

    Use the TicketMachine definition on page 35.

    TicketMachine t1 = new TicketMachine(40);
    TicketMachine t2 = new TicketMachine(50);
    TicketMachine t3 = new TicketMachine(60);
    TicketMachine t4 = new TicketMachine(50);

    TicketMachine C [ ] = new TicketMachine[4];
    C[0]=t1;
    C[1]=t2;
    C[2]=t3;
    C[3]=t4; 
     

     

  4. (1) Does the following initialize array C equivalently to the Java code in Question 3?   __yes except for t1-t4____

    TicketMachine C[] = { new TicketMachine(40),
                                      new TicketMachine(50),
                                      new TicketMachine(60),
                                      new TicketMachine(50)
                                    };       
     
  5. (2) Give the Java for-statement to print the price of each TicketMachine variable of array C in Question 3 (use TicketMachine getPrice() method).

     
    for(int i=0; i<4; i++)
        System.out.println( C[i].getPrice() );

     

  6. (2) Give the Java for-statement to total the price of each TicketMachine variable of array C in Question 3 (use TicketMachine getPrice() method).

     
    int total=0;

    for(int i=0; i<4; i++)
        total = total + C[i].getPrice();

     

  7. (2) Write the Java method greatestPrice() that returns the greatest price of a TicketMachine variable of array C in Question 4 (use TicketMachine getPrice() method). The solution should work for any array of TicketMachines.

     
    public int greatest()
    {
        int gP = C[0].getPrice();

        for(int i=1; i<4; i++)
            if ( C[i].getPrice() > gP)
                    gP = C[i].getPrice();

        return gP;
    }


     

  8. (2) Give the Java for-statement to print the price of each TicketMachine variable of ArrayList D below (use TicketMachine getPrice() method).

    Use the TicketMachine definition below.

    TicketMachine t1 = new TicketMachine(40);
    TicketMachine t2 = new TicketMachine(50);
    TicketMachine t3 = new TicketMachine(60);

    ArrayList <TicketMachine> D = new ArrayList <TicketMachine>();
    D.add(t1);
    D.add(t2);
    D.add(t3); 
    D.add(new TicketMachine(50)); 

    for(int i=0; i < D.size(); i++)
        System.out.println( D.get(i).getPrice() );
  9. (2) Write the Java method greatestPrice() that returns the greatest price of any TicketMachine variable from ArrayList D in Question 8 (use TicketMachine getPrice() method). The solution should work for any size ArrayList collection of TicketMachines.
     
    public int greatestPrice()
    {
        int gP = D.get(0).getPrice();

        for(int i=1; i<D.size(); i++)
            if( D.get(i).getPrice() > gP)
                gP = D.get(i).getPrice();

        return gP;
    }