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.


     
  2. (2) Define an array named color of String initialized to contain the Strings "red", "white", "yellow" and "blue".



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

    Use the TicketMachine definition at end of exercise.

      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 fixed array C equivalently to the Java code in Question 1?   ________

    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 fixed array C in Question 4 (use TicketMachine getPrice() method).







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






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






     
  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)); 

     

  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 class TicketMachine
     {
     private int price;
     private int balance;
     private int total;
     public TicketMachine(int ticketCost)
     {
           price = ticketCost;
           balance = 0;
           total = 0;
     }
     public int getPrice()
     {
           return price;
     }
     public int getBalance()
     {
           return balance;
    }
     public void insertMoney(int amount)
     {
           balance += amount;
     }
     public void printTicket()
     {
           System.out.println("##################");
           System.out.println("# The BlueJ Line");
           System.out.println("# Ticket");
           System.out.println("# " + price + " cents.");
           System.out.println("##################");
           System.out.println();

           total += balance;
           balance = 0;
     }
     }