Test 3                   Name ___Llave______           __/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 );
   }
}

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

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

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

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

  
public void b ()
   {      
      ab();
      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 A2 {
   private String A;
   private B2 ab;

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

public class B2 {
 public String B;

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

public class B2 {
 private String B;

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

 public void setB(String b)
 {
     B=b;
  }

  public String getB()
  {

      return B;

   }
}

 

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

 

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 int getI()
  {
       return I;
   }

}

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

   public B (String theS, int theI)
   {
      super(theS, theI );
   }

   public void bp ()
   {      
      ap();     
   }

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

  
                                         OR

C

A

B

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

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

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

  public int getI()
  {
       return I;
   }

  public int getS()
  {
       return S;
   }

}

 

public class A extends C
{
  public A (String aS, int aI)
  {
    super( aS, aI);
  }

  public void ap ()
  {
    cp();     
  }

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

public class B extends C
{
  public B (String theS, int theI)
   {
      super(theS, theI );
   }

   public void bp ()
   {      
      cp();     
   }

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

 


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 );         _____6-11________

b.    BusTM btm = new BusTM( 50, “A”, “X Y Z” );           ____26-28, 6-11, 29-31____

c.     tm.insertMoney( 100 );                                           _____12-15______

d.    btm.insertMoney( 100 );                                         _____12-15______

e.    tm.printStops( );                                                    ______invalid   ____

f.      btm.printStops( );                                                   _____32-36  _____

g.    tm.printTicket( );                                                    _____16-21______

h.    btm.printTicket( );                                                  _____37-40, 16-21, 41_

i.      tm = btm;                                                              _____valid_____

j.      btm = tm;                                                              _____invalid____

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

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

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

a.              tm = aL.get( 0 );                                     _T__             _No__

b.              btm = aL.get( 1 );                                   _T__             _No__

c.               tm = aL.get( 0 );                                    _T__             _No__

d.              tm =  aL.get( 1 );                                    _T__             _No__

e.              btm = aL.get( 0 );                                   _T__             _Yes__

f.                btmaL.get( 1 );                                  _T__             _No__
 

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();               _________# 50_________________

b.              btm.printTicket();             _________# Bus A        
                                            ________ # 50______

c.               btm.printStops();              ________ # A               
                                            ________# Stops X Y Z_____

d.              tm = btm;                        ________________________________

e.              tm.printTicket();               _________# Bus A        
                                            ________ # 50______

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

a.    Define an ArrayList field of type BusTM.

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 {

    // a.
    private ArrayList <BusTM> al;

    // b.

    public BusTerminal() {

        al = new ArrayList <BusTM>();

 

        al.add( new BusTerminal(100, "Red Line", "34th Elm Stadium") );

        al.add( new BusTerminal(200, "Blue Line", "28th Oak IUS") );

        al.add( new BusTerminal(150, "Pink Line", "6th YMCA Bono") );

    }

    // c.

    public void printAllStops() {

        for( int i = 0; i<al.size(); i++)

            al.get(i).printStops();

    }

    // d.

    public String getAllNames() {

        String result = "";

        for( int i=0; i<al.size(); i++)

            result = result + al.get(i).getName();

        return result;

    }

    // e.

    public int lowestPrice() {

        int lowest = al.get(0).getPrice();

        for( int i=0; i<al.size(); i++)

            if( al.get( i ).getPrice() < lowest )

                lowest = al.get( i ).getPrince();

    }

}


 

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. }