Collections, Iterators and Generics

Objectives

In this lab, you will...
  • Write a generic method.
  • Iterate through a List to determine if a DNA sequence is GC-rich.

Pre-lab

Virtually all interesting programs must store collections of items. In this lab we will work on problems that store lists of items using the generic ArrayList class. Before coming to the lab review your notes on the ArrayList and Iterator classes and your notes on generic methods.


In-lab Exercises

Part 1. Writing a generic method

Lets assume that we're writing a program for the City of Vancouver Taxation Department. One of their requirements is that they can figure out the total taxes paid by a given list of properties. Property taxes are computed in different ways depending on the type of property: condominium, attached or detached. You have designed a Property class that has the following method:

public abstract double getTaxes()
// returns the annual taxes to be paid by this property

You have also designed three subclasses of Property each of which provides an implementation of the abstract method getTaxes(): Condominium, Attached and Detached.

Write a method that takes a list of properties as its only parameter and that returns the total annual taxes to be paid by the properties in the list. You must use an iterator to visit the items in the list - do not use the get() method of the ArrayList class. Note that your method must accept any of the following lists:

ArrayList<Property> list1 = new ArrayList<Property>();
ArrayList<Condominium> list2 = new ArrayList<Condominium>();
ArrayList<Attached> list3 = new ArrayList<Attached>();
ArrayList<Detached> list4 = new ArrayList<Detached>();

To get started:

  • Create a directory for the lab, i.e. CollectionsLab
  • Download taxesLab.zip into the lab directory.
  • In Eclipse, create a new Java project called TaxesLab. Choose Create new project in workspace
  • Import taxesLab.zip into the project.
  • Implement your method in the PropertyApp class

 

Part 2. Determining if a Sequence is GC-rich

A DNA sequence represents 4 kinds of nucleotides that are denoted by the characters A, C, G and T.  A sequence may be very long. For example, even the genome (i.e., the DNA) of a simple bacterium has more than a million nucleotides.

Biologists sometimes want to know if an organism is GC-rich. To determine this information, the total number of Cs and Gs appearing in a sequence must be counted, and the percentage of the sequence that is a C or a G computed. Note that we are not looking for the nucleotide sequence "GC" but are simply interested in the number of Gs and Cs irrespective of their order.

In this part of the lab, you will write code to report the percentage of a DNA sequence that is a G or a C nucleotide.

To get started:

  • Download dnaLab.zip into the directory you have created for this lab.
  • In Eclipse, create a new Java project called DnaLab. Choose Create new project in workspace
  • Import dnaLab.zip into the project.

You should now have three files in the dna package:

  1. DNAApplication.java - this class contains the main() method.
  2. DNASequence.java - this class supports the loading of a DNASequence into a list member variable.
  3. sars.txt - the genome for the SARS virus which was determined by the BC Genome Sciences Center. (See the SARS genome page at the NCBI for more information).

The DNASequence and DNAApplication classes should compile and you should be able to run DNAApplication as a Java application. By default, the application simply reports the number of nucleotides in the SARS genome. (Note:  The code works as provided, just don't touch this part of the code!)

You must add a method to the DNASequence class  to compute the percentage of the sequence that is a G or a C and you must write out a line to standard output of the form:

The SARS genome is <computed percentage>% GC.

Did you know? The Computer Science department offers Combined Majors programs with other Science departments. You can do a combined majors degree in Computer Science with Microbiology and Immunology for example. Unlike double majors degrees, these combined majors degrees can be completed in the usual four year period. If you are interested in knowing more about these options, see our website or make an appointment to see one of our advisors by e-mailing undergrad-info@cs.ubc.ca

When you are finished, call over your TA and see if your solution is correct.

(Note: We are not concerned with efficiency in this lab so we are willing to represent every nucleotide as a separate object in the DNA sequence even though this is not the most efficient way of proceeding.)