CPSC 332 / Section 500

Dr. Salih Yurttas - October 13, 2006

Test 1


[ You can use either "C#" or "Java" or "C++" code to answer ]

  1. (60 points)
    
    // A01.cs
    
    using System;
    using System.Collections;
    
    public class A01 {
      private static string ACGT = "ACGT";
      private static int LE = ACGT.Length;
    
      public static void Main(string[] args) {
        // init
        ArrayList D = new ArrayList();
        D.Add("AACCAG");
        D.Add("GAAACG");
        D.Add("AACCTT");
        D.Add("GCTATT");
    
        char A = ACGT[LE-1];
        int N = D.Count;
        ArrayList List = new ArrayList();
    
        // extract strings without T
        for(int i=0; i<N; i++) {
          bool Matches = false;
          string T = (string)D[i];
          int L = T.Length;
          for(int j=0; j<L; j++)
            if(T[j]==A) {
              Matches = true;
              break;
            }
          if(!Matches) List.Add(T);
        }
    
        // output
        int M = List.Count;
    
        Console.WriteLine();
        for(int i=0; i<M; i++) 
          Console.WriteLine("{0}. {1}", i+1, List[i]);
    
        List.Clear();
    
        // extract strings without T
        for(int i=0; i<N; i++) {
          bool Matches = false;
          string T = (string)D[i];
          int L = T.Length;
          for(int j=0; j<L; j++)
            if(T[j]==A) {
              Matches = true;
              break;
            }
          if(Matches) List.Add(T);
        }
    
        // output
        M = List.Count;
    
        Console.WriteLine();
        for(int i=0; i<M; i++) 
          Console.WriteLine("{0}. {1}", i+1, List[i]);
      }
    }
    
    
    
    
    1. AACCAG
    2. GAAACG
    
    1. AACCTT
    2. GCTATT
    
    


    
    // A01.java
    
    import java.util.*;
    
    public class A01 {
    
      private final static String ACGT = "ACGT";
    
      private final static int L = ACGT.length();
    
      public static void main(String[] args) {
        // init
        ArrayList<String> d = new ArrayList<String>();
    
        d.add("AACCAG");
        d.add("GAAACG");
        d.add("AACCTT");
        d.add("GCTATT");
    
        char a = ACGT.charAt(L-1);
    
        ArrayList<String> list = new ArrayList<String>();
    
        int n = d.size();
    
        // extract strings without T
        for(int i=0; i<n; i++) {
          boolean matches = false;
          String t = d.get(i);
          int l = t.length();
          for(int j=0; j<l; j++)
            if(t.charAt(j)==a) {
              matches = true;
              break;
            }
          if(!matches) list.add(t);
        }
    
        // output
        int m = list.size();
    
        System.out.println();
        for(int i=0; i<m; i++)
          System.out.println((i+1) + ". " + list.get(i));
    
        list.clear();
    
        // extract strings with T
        for(int i=0; i<n; i++) {
          boolean matches = false;
          String t = d.get(i);
          int l = t.length();
          for(int j=0; j<l; j++)
            if(t.charAt(j)==a) {
              matches = true;
              break;
            }
          if(matches) list.add(t);
        }
    
        // output
        m = list.size();
    
        System.out.println();
        for(int i=0; i<m; i++)
          System.out.println((i+1) + ". " + list.get(i));
      }
    
    }
    
    
    
    
    1. AACCAG
    2. GAAACG
    
    1. AACCTT
    2. GCTATT
    
    


    
    // a_01.cpp
    
    #include <iostream>
    
    #include <vector>
    #include <string>
    
    using namespace std;
    
    const string ACGT = "ACGT";
    
    int main(int argc, char* argv[]) {
    
      const int L = ACGT.length();
    
      // init
      string d[] = {"AACCAG",
                    "GAAACG",
                    "AACCTT",
                    "GCTATT"};
    
      vector<string> v_d(d, d+4);
    
      char a = ACGT.at(L-1);
    
      vector<string> list;
    
      int n = v_d.size();
    
      // extract strings without T
      for(int i=0; i<n; i++) {
        bool matches = false;
        string t = v_d.at(i);
        int l = t.length();
        for(int j=0; j<l; j++)
          if(t.at(j)==a) {
            matches = true;
            break;
          }
        if(!matches) list.push_back(t);
      }
    
      // output
      int m = list.size();
    
      cout << endl;
      for(int i=0; i<m; i++)
        cout << i+1 << ". " << list.at(i) << endl;
    
      list.clear();
    
      // extract strings with T
      for(int i=0; i<n; i++) {
        bool matches = false;
        string t = v_d.at(i);
        int l = t.length();
        for(int j=0; j<l; j++)
          if(t.at(j)==a) {
            matches = true;
            break;
          }
        if(matches) list.push_back(t);
      }
    
      // output
      m = list.size();
    
      cout << endl;
      for(int i=0; i<m; i++)
        cout << i+1 << ". " << list.at(i) << endl;
    
    }
    
    
    
    
    1. AACCAG
    2. GAAACG
    
    1. AACCTT
    2. GCTATT
    
    


  2. (40 points)
    • (20 points)
      What will be printed out when the application A02.cs is executed?

    • (20 points)
      Do the necessary functional abstraction to factor out compute block as a static method
      invoked from the Main method of the given A02.cs.

    
    // A02.cs
    
    using System;
    using System.Collections;
    
    public class A02
    {
    
      public static void Main(string[] args)
      {
        // init
        ArrayList DD = new ArrayList();
    
        ArrayList D = new ArrayList();
    
        int N;
    
        Console.Write("N : ");
        N = Int32.Parse(Console.ReadLine());   // assume 8 for N
        Console.WriteLine();
    
        int K;
    
        Console.Write("K : ");
        K = Int32.Parse(Console.ReadLine());   // assume 2 for K
        Console.WriteLine();
    
        //compute
        int A;
     
        Console.Write("A : ");
        A = Int32.Parse(Console.ReadLine());   // assume 1 for A
        Console.WriteLine();
    
        while(A<N)
        {
          A += K;
          D.Add(A);
        }
    
        DD.Add(new ArrayList(D));
    
        D.Clear();
    
        //compute
        Console.Write("A : ");
        A = Int32.Parse(Console.ReadLine());   // assume 4 for A
        Console.WriteLine();
    
        while(A<N)
        {
          A += K;
          D.Add(A);
        }
    
        DD.Add(new ArrayList(D));
    
        // output
        int M = DD.Count;
    
        for(int i=0; i<M; i++)
        {
          D = (ArrayList)DD[i];
          N = D.Count;
          for(int j=0; j<N; j++)
            Console.WriteLine("{0}", D[j]);
          Console.WriteLine();
        }
      }
    
    }
    
    
    
    3
    5
    7
    9
     
    6
    8
    
    
    


    • (20 points)
      What will be printed out when the application A02.java is executed?


    • (20 points)
      Do the necessary functional abstraction to factor out compute block as a static method
      invoked from the main method of the given A02.java.

    
    // A02.java
    
    import java.io.*;
    
    import java.util.*;
    
    public class A02 {
    
      public static void main(String[] args)
        throws IOException
      {
        // init
        ArrayList<ArrayList<Integer>> dd = new ArrayList<ArrayList<Integer>>();
    
        ArrayList<Integer> d = new ArrayList<Integer>();
    
        DataInputStream dIS = new DataInputStream(System.in);
        Reader kR = new BufferedReader(new InputStreamReader(dIS));
        StreamTokenizer kTokens = new StreamTokenizer(kR);
    
        int n;
    
        System.out.print("n : ");
        System.out.flush();
        kTokens.nextToken();        // assume 8 for n
        n = (int)kTokens.nval;
    
        System.out.println() ;
    
        int k;
    
        System.out.print("k : ");
        System.out.flush();
        kTokens.nextToken();        // assume 2 for k
        k = (int)kTokens.nval;
    
        System.out.println() ;
    
        // compute
        int a;
    
        System.out.print("a : ");
        System.out.flush();
        kTokens.nextToken();        // assume 1 for a
        a = (int)kTokens.nval;
    
        System.out.println() ;
    
        while(a<n) {
          a += k;
          d.add(new Integer(a));
        }
    
        dd.add(new ArrayList<Integer>(d));
    
        d.clear();
    
        // compute
        System.out.print("a : ");
        System.out.flush();
        kTokens.nextToken();        // assume 4 for a
        a = (int)kTokens.nval;
    
        System.out.println() ;
    
        while(a<n) {
          a += k;
          d.add(new Integer(a));
        }
    
        dd.add(new ArrayList<Integer>(d));
    
        // output
        int m = dd.size();
    
        for(int i=0; i<m; i++) {
          ArrayList<Integer> aL = (ArrayList<Integer>)dd.get(i);
          n = aL.size();
          for(int j=0; j<n; j++)
            System.out.println(aL.get(j));
          System.out.println();
        }
      }
    
    }
    
    
    
    3
    5
    7
    9
     
    6
    8
    
    
    


    • (20 points)
      What will be printed out when the application a_02.cpp is executed?

    • (20 points)
      Do the necessary functional abstraction to factor out compute block as a function called
      from the main method of the given a_02.cpp.

    
    // a_02.cpp
    
    
    #include <iostream>
    
    #include <vector>
    
    using namespace std;
    
    int main(int argc, char* argv[]) {
    
      // init
      vector< vector<int> > dd;
    
      vector<int> d;
    
      int n;
    
      cout << "n : ";
      cin >> n;        // assume 8 for n
      cout << endl;
    
      int k;
    
      cout << "k : ";
      cin >> k;        // assume 2 for k
      cout << endl;
    
      int a;
    
      // compute
      cout << "a : ";
      cin >> a;        // assume 1 for a
      cout << endl;
    
      while(a<n) {
        a += k;
        d.push_back(a);
      }
    
      dd.push_back(d);
    
      d.clear();
    
      // compute
      cout << "a : ";
      cin >> a;        // assume 4 for a
      cout << endl;
    
      while(a<n) {
        a += k;
        d.push_back(a);
      }
    
      dd.push_back(d);
    
      // output
      int m = dd.size();
      for(int i=0; i<m; i++) {
        vector<int> v = dd.at(i);
        int n = v.size();
        for(int j=0; j<n; j++)
          cout << v.at(j) << endl;
        cout << endl;
      }
    
    }
    
    
    
    3
    5
    7
    9
     
    6
    8