Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This week our Learning Reflections assignment is short, probably about four lines of code. We will use the Java Collections.sort method to sort the substrings

This week our Learning Reflections assignment is short, probably about four lines of code. We will use the Java Collections.sort method to sort the substrings we generated in last week's generateFrequencies assignment. The substrings are the set of keys in the Map object returned by the generateFrequencies method you implemented last week. You can get this set of substrings with: generateFrequencies(index).keySet(). Since the method you are to write will return an object of type List, you will likely want to convert your Set of substrings into an ArrayList before sorting using Collections.sort (Links to an external site.) (notice that Collections.sort method changes its parameter object and it returns void) . Look at the constructor for ArrayList for ideas on how to convert a Set into an ArrayList.

We will again use Test Driven Development. The revised Sequences.java file has a new method called getSortedListOfSubstrings that you will implement so that all junit tests will pass in the revised PlainSequencesTest.java junit test file.

Herer is the test code

public void testGetSortedListOfSubstrings() { Sequences tester = new PlainSequences(); String fileName = "plainExampleSmall.text"; try { tester.readSequences(fileName); } catch (FileNotFoundException e) { } //<[A, AG, AGA, C, CA, CAG, CAGA, G, GA, GC, GCA, GCAG, GCAGA, GG, GGC, GGCA, GGCAG, GGCAGA]> List answer = new ArrayList(); answer.add("A"); answer.add("AG"); answer.add("AGA"); answer.add("C"); answer.add("CA"); answer.add("CAG"); answer.add("CAGA"); answer.add("G"); answer.add("GA"); answer.add("GC"); answer.add("GCA"); answer.add("GCAG"); answer.add("GCAGA"); answer.add("GG"); answer.add("GGC"); answer.add("GGCA"); answer.add("GGCAG"); answer.add("GGCAGA");

List sortedSubStrings = tester.getSortedListOfSubstrings(0); assertEquals("the sorted substrings is not right", answer,sortedSubStrings); }

}

here is my code

import java.util.*; import java.io.*;

public class PlainSequences implements Sequences {

private List descriptions; private List sequences; public PlainSequences() { descriptions = new ArrayList(); sequences = new ArrayList(); } public List getDescriptions() { return descriptions; } public List getSequences() { return sequences; } public void readSequences(String fileName) throws FileNotFoundException { Scanner input = new Scanner(new File(fileName)); String sequence = ""; int count = 0; while(input.hasNextLine()) { sequence = sequence + input.nextLine(); } descriptions.add(fileName); sequences.add(sequence); } public boolean isValidSequence(int index) { String sequence = sequences.get(index); for(int i = 0; i < sequence.length(); i++) { char dnaBase = sequence.charAt(i); if (!( dnaBase == ' ' || dnaBase == ' '|| dnaBase == 'A' || dnaBase == 'C' || dnaBase == 'G' || dnaBase == 'T' || dnaBase == 'U' || dnaBase == 'R' || dnaBase == 'Y' || dnaBase == 'K' || dnaBase == 'M' || dnaBase == 'S' || dnaBase == 'W' || dnaBase == 'B' || dnaBase == 'D' || dnaBase == 'H' || dnaBase == 'V' || dnaBase == 'N' )) { return false;} } return true; } public String formatInGroups(int index, int basesPerGroup, int groupsPerLine) { StringBuilder formattedSequence = new StringBuilder(); String sequence = sequences.get(index); int num = 0; while(num < sequence.length()) { for( int j = 0; j < basesPerGroup; j++) { if(num < sequence.length()) { if (num % (basesPerGroup * groupsPerLine) == 0){ formattedSequence.append(" "); } formattedSequence.append(sequence.charAt(num)); num++; } } formattedSequence.append(" "); } return formattedSequence.toString().trim(); }

public Map generateFrequencies(int index) { Map sequenceCount = new HashMap(); String sequence = sequences.get(index); for(int x = 1 ; x < sequence.length(); x++) { for(int y = 0; y <= sequence.length() - x ; y++) { String letter = sequence.substring(y, y + x); if(!sequenceCount.containsKey(letter)) { sequenceCount.put(letter, 1); } else { int oldval= sequenceCount.get(letter); sequenceCount.put(letter, oldval + 1); } } } sequenceCount.put(sequence, 1); return sequenceCount; } public List getSortedListOfSubstrings(int index) {

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions