Exercise 7        Name __________________        Score __/24

Modified: 

  1. (4) 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 ap ()
         {
            System.out.println( S );
            System.out.println( I );
         }

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

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

  2. (10) Rewrite the following to eliminate explicit coupling between TwoA and TwoB.
    public class TwoA
    {
       private String A1;
       private String A2;
       private String A3;
       private TwoB twob;

       public TwoA()
      {
          twob = new TwoB();
          twob.B2 = "white";
          A1 = twob.B1;
          A2 = twob.B2;
          A3 = twob.B3;
       }
    }
    public class TwoB
    {
       public String B1;
       public String B2;
       public String B3;

       public TwoB()
       {
           B1 = "red";
           B2 = "pink";
           B3 = "blue";
       }
    }
  3. (10) Rewrite the following class to:
    1. Improve method cohesion.
    2. Extend the class to handle any number of Strings for:
      1. setting a field value,
      2. returning a field value,
      3. returning a String as the concatenation of the values all fields.
public class Three
{
   private String S1;
   private String S2;
   private String S3;

   public void setS (String theS1, String theS2, String theS3)
   {
        S1 = theS1;
        S2 = theS2;
        S3 = theS3;
   }

   public String getS (int n)
   {
      if (n == 0) return S1;
      if (n == 1) return S2;
      if (n == 2) return S3;
     
     return S1 + S2 + S3;
    }
}