Search code examples
javaclassclonedeep-copy

Deep copy and returning an instance Java


So i am doing a first year university assignment question and i am going to be honest about it. I just want to make things clear before some of you come down-voting my question. I don't want complete code, i just want some help with a few things.

The question is divided into two parts. The first part is to write a Nucleotide class whose constructor has two properties. A single character called base that has to be either 'a' or 'c' or 'g' or 't' otherwise it should be 'n' and a boolean called degenerate.

My code for this part is here:

class Nucleotide {
    private char base;
    private boolean degenerate;

    public nucleotide(char base, boolean degenerate){
      if(base != ‘a’ || base != ‘c’ || base != ‘g’ || base != ’t’){
        this.base = ’n’;
      } else {
          this.base = base;
      }
      this.degenerate = degenerate;
    }
}

The next part of the question says to use the Nucleotide object and create a new Bacteria class. An instance of bacteria consists of a genome (a collection of nucleotides), and a species (a String).

You must create a constructor which accepts a String and a collection, and uses those to initialize the species and the collection of nucleotides. My code for this part is here:

class Bacteria {
  //private ArrayList<Nucleotide> genome;
  private String species;

  public Bacteria(String species, ArrayList<Nucleotide> genome) {
    genome = new ArrayList<Nucleotide>();
    this.species = species;
  }

My problem starts with the next step which asks us to write an instance method that performs deep copy and returns an instance of Bacteria.

public Bacteria binaryFission() {

How can i perform deep copy without serialization and reflection. I hardly know anything about those things.

Again i need pointers or the basic idea of how to go about completing the binaryFission() method. I have gone through several deep copy questions that are on SO but none of them are relevant to my question so i don't believe that i am asking a duplicate question. I am happy to provide more details though.


Solution

  • This is the way to do it manually

    public Bacteria binaryFission() {
        String speciesClone = this.species;
        ArrayList<Nucleotide> genomeClone = new ArrayList<Nucleotide>();
        //now iterate over the existing arraylist and clone each Nucleotide
        for(int index = 0; index < this.genome.size(); index++)
        {
            genomeClone.add(new Nucleotide(
                                genome.get(index).getBase(), //needs to be added to the Nucleotide class to retrieve the base variable
                                genome.get(index).getDegenerate() //needs to be added to be allowed to get its degenerate
                    ));
        }
    
        return new Bacteria(speciesClone, genomeClone);
    }
    

    FYI - you'll need to add getters for your Nucleotide class private variables in order for this to work since they are private and Bacteria won't have access to their values without them.