Test 3                   Name _______________           __/100

1.    (7) Rewrite the following class definition to remove duplication.

public class One
{
   private String S;
   private int I;

   public One (String theS, int theI)
   {
      S = theS;
      I = theI;
   }

   public void a ()
   {
      I = S.indexOf(“X”);
      S = S.subString(I, I+4);
      System.out.println( I );     
   }

   public void b ()
   {      
      I = S.indexOf(“X”);
      S = S.subString(I, I+4);     
      System.out.println( S );
   }
}

2.    (8) Eliminate the explicit coupling between the A2 and B2 classes.

public class A2 {
   private String A;
   private B2 ab;

  public A2()
 {
    ab = new B2();
    ab.B = "white";
    A = ab.B;
 }
}

public class B2 {
 public String B;

 public B2()
 {
     B = "red";
 }
}

 

3.     (5) Give the inheritance hierarchy diagram for the following:

class A extends Object { }
class B extends A { }
class C extends B { }
class D extends A { }
class E extends C { }
class F extends B { }
class G extends D { }
class H extends E { }  




4.    (10) Use inheritance to remove duplication. The resulting classes A and B should still behave equivalently.

A

B

public class A
{
  private String S;
  private int I;

  public A (String aS, int aI)
  {
    S = aS;
    I = aI;
  }

  public void ap ()
  {
    I = S.indexOf(“X”);
    S = S.subString(I, I+4);     
  }

  public void print ()
  {      
    System.out.println( S );
  }
}

public class B
{
   private String S;
   private int I;

   public B (String theS, int theI)
   {
      S = theS;
      I = theI;
   }

   public void bp ()
   {      
      I = S.indexOf(“X”);
      S = S.subString(I, I+4);     
   }

  public void print ()
  {      
    System.out.println( I );
  }
}  


5.    (20) Indicate syntactically invalid statements (i.e. errors detected by the compiler). Trace the line numbers executed for valid statements.

a.    TicketMachine tm = new TicketMachine( 50 );         ________________

b.    BusTM btm = new BusTM( 50, “A”, “X Y Z” );           ________________

c.     tm.insertMoney( 100 );                                           ________________

d.    btm.insertMoney( 100 );                                         ________________

e.    tm.printStops( );                                                    ________________

f.      btm.printStops( );                                                   ________________

g.    tm.printTicket( );                                                    ________________

h.    btm.printTicket( );                                                  ________________

i.      tm = btm;                                                              ________________

j.      btm = tm;                                                              ________________

6.     (12) True or False. Indicate syntactically valid statements. For valid statements, indicate if an error occurs when executed.

public class Six
{
   private ArrayList aL;
   private TicketMachine tm;
   private BusTM btm;

   public Six ()
   {
      aL = new ArrayList();
      aL.add(new TicketMachine( 50 ) );
      aL.add(new BusTM( 50, “A”, “X Y Z” ) );
                                                                         Valid           Execution
                                                                   Syntax           Error

a.              tm = aL.get( 0 );                                     ____             _____

b.              btm = aL.get( 1 );                                  ____             _____

c.               tm = (TicketMachine) aL.get( 0 );            ____             _____

d.              tm = (TicketMachine) aL.get( 1 );            ____             _____

e.              btm = (BusTM) aL.get( 0 );                      ____             _____

f.                btm = (BusTM) aL.get( 1 );                      ____             _____

7.    (15) Give the output produced, if any, when the following is executed.

public class Seven
{
   private TicketMachine tm;
   private BusTM btm;

   public Seven ()
   {
          tm = new TicketMachine( 50 ) ;
          btm = new BusTM( 50, “A”, “X Y Z” ) ;
                                                                         

a.              tm.printTicket();               ________________________________

b.              btm.printTicket();             ________________________________

c.               btm.printStops();              ________________________________

d.              tm = btm;                        ________________________________

e.              tm.printTicket();               ________________________________

8.    (23) Complete the definitions for the BusTerminal class.

a.    Define an ArrayList field.

b.    Define a BusTerminal constructor of no parameters. The constructor initializes the ArrayList field and adds 3 BusTM objects to the ArrayList field. Chose appropriate parameters for constructing BusTM objects.

c.     Define a method that calls the printStops() method on all BusTM objects of the ArrayList.

d.    Define a method getAllNames() that returns the concatenation of the name field of all BusTM objects in the ArrayList.

e.    Define a method lowestPrice() that returns the lowest value of the price field of all BusTM objects in the ArrayList.

public class BusTerminal {

 

















1.     public class TicketMachine

2.     {

3.        private int price;

4.        private int balance;

5.        private int total; 
 

6.        public TicketMachine(int ticketCost)

7.       {

8.          price = ticketCost;

9.          balance = 0;

10.       total = 0;

11.     }
 

12.    public void insertMoney(int amount)

13.    {

14.       balance += amount;

15.    }
 

16.    public void printTicket()

17.    {

18.       System.out.println(“# “ + price );

19.       total += balance;

20.       balance = 0;

21.    }

22.    public int getPrice()

23.    {

24.       return price;

25.    }

26. }

23. public class BusTM extends TicketMachine

24. {

25.     private String name, stops;
 

26.     public BusTM( int theCost, String theName,
                         String theStops )

27.     {

28.          super( theCost );

29.          name = theName;

30.          stops = theStops;

31.      }
 

32.      public void printStops()

33.      {

34.         System.out.println("# Bus " + name );

35.         System.out.println("# Stops " + stops );

36.      } 
 

37.      public void printTicket()

38.      {

39.         System.out.println("# Bus  " + name );

40.         super.printTicket();

41.      }

42.      public String getName()

43.      {

44.         return name;

45.      }

46. }