Mysterious Organism (Java)

I tried to do the Mysterious Organism project in Java (after doing it in JavaScript a while ago)
and I used classes instead of the factory function that is meant to be used in the original project.

Details of the Project

Project Goals

Context: You’re part of a research team that has found a new mysterious organism at the bottom of the ocean near hydrothermal vents. Your team names the organism, Pila aequor ( P. aequor ), and finds that it is only comprised of 15 DNA bases. The small DNA samples and frequency at which it mutates due to the hydrothermal vents make P. aequor an interesting specimen to study. However, P. aequor cannot survive above sea level and locating P. aequor in the deep sea is difficult and expensive. Your job is to create objects that simulate the DNA of P. aequor for your research team to study.

As you progress through the steps, use the terminal and System.out.println() statements to check the output of your loops and functions.

You may choose to use ArrayLists instead of arrays in this project.

2.

Look over the starter code. There are two helper functions: returnRandBase() and mockUpStrand() .

DNA is comprised of four bases ( Adenine, Thymine, Cytosine, and Guanine). When returnRandBase() is called, it will randomly select a base and return the base ( 'A' , 'T' , 'C' , or 'G' ).

mockUpStrand() is used to generate an array containing 15 bases to represent a single DNA strand with 15 bases.

You’ll use these helper functions later to create your objects that represent P. aequor .

public class pAeqour {

  public static char returnRandBase() {
    final char[] dnaBases = {'A', 'T', 'C', 'G'};
    int randomInt = (int)Math.floor(Math.random() * 4);
    return dnaBases[randomInt];
  }

  public static char[] mockUpStrand() {
    char[] newStrand = new char[15];
    for (int i = 0; i < 15; i++) {
      newStrand[i] = returnRandBase();
    }
    return newStrand;
  }

}

3.

Since you need to create multiple objects, create a constructor for pAequor that has two parameters:

  • The first parameter is a number (no two organisms should have the same number).
  • The second parameter is an array of 15 DNA bases.

pAequor() should return an object that contains the properties specimenNum and dna that correspond to the parameters provided.

You’ll also add more methods to this object in the later steps.

4.

Your team wants you to simulate P. aequor ‘s high rate of mutation (change in its DNA).

To simulate a mutation, add the (non-static) method .mutate() .

.mutate() is responsible for randomly selecting a base in the object’s dna property and changing the current base to a different base. Then .mutate() will return the object’s dna .

For example, if the randomly selected base is the 1st base and it is 'A' , the base must be changed to 'T' , 'C' , or 'G' . But it cannot be 'A' again.

5.

Your research team wants to be able to compare the DNA sequences of different P. aequor . You’ll have to add a new method ( .compareDNA() ) to pAeqour objects.

.compareDNA() has one parameter, another pAequor object.

The behavior of .compareDNA() is to compare the current pAequor ‘s .dna with the passed in pAequor ‘s .dna and compute how many bases are identical and in the same locations. .compareDNA() does not return anything, but prints a message that states the percentage of DNA the two objects have in common — use the .specimenNum to identify which pAequor objects are being compared.

For example:

ex1 = ['A', 'C', 'T', 'G']
ex2 = ['C', 'A', 'T', 'T']

ex1 and ex2 only have the 3rd element in common ( 'T' ) and therefore, have 25% (1/4) of their DNA in common. The resulting message would read something along the lines of: specimen #1 and specimen #2 have 25% DNA in common .

6.

P. aequor have a likelier chance of survival if their DNA is made up of at least 60% 'C' or 'G' bases.

In the of pAequor class, add another method .willLikelySurvive() .

.willLikelySurvive() returns true if the object’s .dna array contains at least 60% 'C' or 'G' bases. Otherwise, .willLikelySurvive() returns false.

7.

With the class set up, your team requests that you create 30 instances of pAequor that can survive in their natural environment. Store these instances in an array for your team to study later.

8.

Great Job! Your research team now has the ability to simulate the DNA of P. aequor for additional study.

9.

If you’d like to challenge yourself further, you could consider the following:

  • Create a .complementStrand() method to the factory function’s object that returns the complementary DNA strand. The rules are that 'A' s match with 'T' s and vice versa. Also, 'C' s match with 'G' s and vice versa. (Check the hint for more details)
  • Use the .compareDNA() to find the two most related instances of pAequor

If anyone has any comments, or would like to share their version, post a reply.

Here’s what I came up with for Mysterious Organism in Java, as a Codecademy workspace:
https://www.codecademy.com/workspaces/61f4c8c08f6c970a7affabb9

I used arrays instead of Java ArrayLists for the project.