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

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

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

         public void ap ()
         {
            bp();
            dp();
         }

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

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

  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";
       }
    }
    public class TwoA
    {
       private String A1;
       private String A2;
       private String A3;
       private TwoB twob;

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

       public TwoB()
       {
           B1 = "red";
           B2 = "pink";
           B3 = "blue";
       }
       public String getB1() { return B1; }
       public String getB2() { return B2; }
       public String getB3() { return B3; }
       public void setB2(String s) { B2=s; }
    }
    public class TwoA
    {
       private String A1;
       private String A2;
       private String A3;
       private TwoB twob;

       public TwoA()
      {
          twob = new TwoB();
          twob.setB(2, "white");
          A1 = twob.getB(1);
          A2 = twob.getB(2);
          A3 = twob.getB(3);
       }
    }
    public class TwoB
    {
       private ArrayList<String> B = new ArrayList <String>();

       public TwoB()
       {
           B.add("red");
           B.add("pink");
           B.add("blue");
       }

       public String getB(int n)
       {
           return B.get(n-1);
       }

       public void setB(int n, String s)
       {
           B.set(n-1, s);
       }  
    }
  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;
    }
}

public class Three
{
   Array <String> aL = new ArrayList<String>();

   public void setS (String theS)
   {
        aL.add( theS );
   }

   public String getS ( int n )  {
     return aL.get( n );
   }

   public String getS() {
     String result="";

     for( int i=0; i<aL.size(); i++)
           result = result + aL.get(i);

     return result;
    }
}