Search code examples
javahashmapsubclasssuperclass

Java Code not printing Keys and Values of HashMap


My code inputs a user-defined string and categorizes it as DNA, RNA, or ODD. The output is supposed to print the keys and values of the HashMap nucleotide_hmap but the code currently does not. What should I change in my code so that it works?

Here is what the code should output: (if user inputs sequence ATGC)

  • ATGC
  • A 1 : 0.25
  • T 1 : 0.25
  • C 1 : 0.25
  • G 1 : 0.25
  • Sequence Length: 4
  • Reverse sequence: CGTA
  • Reverse complement: GCAT

And this is one of my subclasses:

package sequenceclasses; 

public class DNASequence extends MainSequence {
    public DNASequence(String dna_sequence){
        the_sequence = dna_sequence; 
        complement_hmap.put('A', 'T');
        complement_hmap.put('T', 'A');
        complement_hmap.put('C', 'G');
        complement_hmap.put('G', 'C');
    }   
    public void nuc_content(String dna_sequence){
        the_sequence = dna_sequence;
        double[] counts_ratios = new double[8];
        for (int i = 0; i < dna_sequence.length(); i++){
            if (dna_sequence.charAt(i) == 'A'){
                counts_ratios[0] += 1;}
            if (dna_sequence.charAt(i) == 'T'){
                counts_ratios[1] += 1;}
            if (dna_sequence.charAt(i) == 'C'){
                counts_ratios[2] += 1;}
            if (dna_sequence.charAt(i) == 'G'){
                counts_ratios[3] += 1;}
            }
            if (dna_sequence.length() > 0){
                counts_ratios[4] = counts_ratios[0] / dna_sequence.length();
                counts_ratios[5] = counts_ratios[1] / dna_sequence.length();
                counts_ratios[6] = counts_ratios[2] / dna_sequence.length();
                counts_ratios[7] = counts_ratios[3] / dna_sequence.length();
        }       
        String A_content = Double.toString(counts_ratios[0]) + " , " + Double.toString(counts_ratios[4]);
        String T_content = Double.toString(counts_ratios[1]) + " , " + Double.toString(counts_ratios[5]); 
        String C_content = Double.toString(counts_ratios[2]) + " , " + Double.toString(counts_ratios[6]);
        String G_content = Double.toString(counts_ratios[3]) + " , " + Double.toString(counts_ratios[7]);

        nucleotide_hmap.put('A', A_content);
        nucleotide_hmap.put('T', T_content);
        nucleotide_hmap.put('C', C_content);
        nucleotide_hmap.put('G', G_content);
    }
}

Solution

  • You never call the nuc_content method in the DNASequence class. To solve this, add the line nuc_content(dna_sequence); to the DNASequence class. The constructor should now look like this:

    public DNASequence(String dna_sequence){
            the_sequence = dna_sequence; 
            complement_hmap.put('A', 'T');
            complement_hmap.put('T', 'A');
            complement_hmap.put('C', 'G');
            complement_hmap.put('G', 'C');
            nuc_content(dna_sequence);
        }