public class Fragment { /** * Creates a new Fragment based upon a String represe
ID: 3871021 • Letter: P
Question
public class Fragment {
/**
* Creates a new Fragment based upon a String representing a sequence of nucleotides,
* containing only the uppercase characters G, C, A and T.
* @param nucleotides
* @throws IllegalArgumentException if invalid characters are in the sequence of nucleotides
*/
public Fragment(String nucleotides) throws IllegalArgumentException {
}
/**
* Returns the length of this fragment. Should be straightforward
* @return the length of this fragment
*/
public int length() {
return 0;
}
/**
* Returns a String representation of this fragment, exactly as was passed to the constructor.
* Should be straightforward
* @return a String representation of this fragmeng
*/
@Override
public String toString() {
return null;
}
/**
* Return true if and only if this fragment contains the same sequence of nucleotides
* as another sequence.
*/
@Override
public boolean equals(Object o) {
return false;
}
/**
* Returns the number of nucleotides of overlap between the end of this fragment and
* the start of another fragment, f.
* The largest overlap is found, for example, CAA and AAG have an overlap of 2, not 1.
* using startsWith, endsWith, and substring might be helpful
*
* @param f the other fragment
* @return the number of nucleotides of overlap
*/
public int calculateOverlap(Fragment f) {
return 0;
}
/**
* Returns a new fragment based upon merging this fragment with another fragment f.
*
* This fragment will be on the left, the other fragment will be on the right; the
* fragments will be overlapped as much as possible during the merge.
* using startsWith, endsWith, and substring might be helpful
*
* @param f the other fragment
* @return a new fragment based upon merging this fragment with another fragment
*/
public Fragment mergedWith(Fragment f) {
return null;
}
}
==============================================================================
import java.util.List;
public class Assembler {
/**
* Creates a new Assembler containing a list of fragments.
*
* The list is copied into this assembler so that the original list
* will not be modified by the actions of this assembler.
*
* @param fragments
*/
public Assembler(List<Fragment> fragments) {
}
/**
* Returns the current list of fragments this assembler contains.
* @return the current list of fragments
*/
public List<Fragment> getFragments() {
return null;
}
/**
* Attempts to perform a single assembly, returning true iff an assembly was performed.
*
* This method chooses the best assembly possible, that is,
* it merges the two fragments with the largest overlap, breaking ties
* between merged fragments by choosing the shorter merged fragment.
*
* Merges must have an overlap of at least 1.
*
* After merging two fragments into a new fragment, the new fragment is inserted into
* the list of fragments in this assembler, and the two original fragments are removed
* from the list.
*
* Can use Integer.MAX_VALUE in this case
* (Note you may not need this, depending upon how you structure your code.)
*
* @return true iff an assembly was performed
*/
public boolean assembleOnce() {
return false;
}
/**
* Repeatedly assembles fragments until no more assembly can occur.
*/
public void assembleAll() {
}
}
Please translate written descriptions of behavior into code. Thank you so much
**RULES**
(1) any two fragments can have an overlap of at least zero and at most the length of the shorter fragment.
(2) order matters when comparing overlaps.
(3) only consider overlaps on the end
(4) need a correct equals method to work correctly
Explanation / Answer
Fragment.java
---------------------------------------------------------------------------------------------
public class Fragment {
private String frag;
/**
* Creates a new Fragment based upon a String representing a sequence of nucleotides,
* containing only the uppercase characters G, C, A and T.
* @param nucleotides
* @throws IllegalArgumentException if invalid characters are in the sequence of nucleotides
*/
public Fragment(String nucleotides) throws IllegalArgumentException {
boolean k = false;
for(int i = 0; i < nucleotides.length(); i++){
char lol = nucleotides.charAt(i);
if(lol=='A'||lol=='G'||lol=='C'||lol=='T'){
k = true;
}
else{
k = false;
}
if(k == false){
throw new IllegalArgumentException("Dosent work");
}
frag = nucleotides;
}
}
/**
* Returns the length of this fragment.
*
* @return the length of this fragment
*/
public int length() {
return frag.length();
}
/**
* Returns a String representation of this fragment, exactly as was passed to the constructor.
*
* @return a String representation of this fragment
*/
@Override
public String toString() {
return frag;
}
/**
* Return true if and only if this fragment contains the same sequence of nucleotides
* as another sequence.
*/
@Override
public boolean equals(Object o) {
boolean k = true;
if(o instanceof Fragment == false){
k = false;
}
else{
for(int i = 0; i<frag.length(); i++){
if(frag.charAt(i) == o.toString().charAt(i)){
k = true;
}
else
k = false;
}
}
return k;
}
/**
* Returns the number of nucleotides of overlap between the end of this fragment and
* the start of another fragment, f.
*
* The largest overlap is found, for example, CAA and AAG have an maximum overlap of 2.
*
* @param f the other fragment
* @return the number of nucleotides of overlap
*/
public int calculateOverlap(Fragment f) {
int overLap = 0;
String underFrag = f.toString();
final int maxlen = Integer.min(frag.length(), underFrag.length());
for(int i = 0; i<maxlen; i++){
if(this.frag.endsWith(f.toString().substring(0, i))){
if(i>overLap){
overLap = i;
}
}
}
return overLap;
}
/**
* Returns a new fragment based upon merging this fragment with another fragment f.
*
* This fragment will be on the left, the other fragment will be on the right; the
* fragments will be overlapped as much as possible during the merge.
*
* @param f the other fragment
* @return a new fragment based upon merging this fragment with another fragment
*/
public Fragment mergedWith(Fragment f) {
int val = this.calculateOverlap(f);
Fragment g = new Fragment(this.frag+f.frag.substring(val));
return g;
}
}
--------------------------------------------------------------------------------------------------------
Assembler.java
--------------------------------------------------------------
import java.util.ArrayList;
import java.util.List;
public class Assembler {
public List<Fragment> Fragments = new ArrayList<Fragment>();
/**
* Creates a new Assembler containing a list of fragments.
*
* The list is copied into this assembler so that the original list
* will not be modified by the actions of this assembler.
*
* @param fragments
*/
public Assembler(List<Fragment> fragments) {
for(int i = 0; i<fragments.size(); i++){
Fragments.add(fragments.get(i));
}
}
/**
* Returns the current list of fragments this assembler contains.
* @return the current list of fragments
*/
public List<Fragment> getFragments() {
return Fragments;
}
/**
* Attempts to perform a single assembly, returning true if an assembly was performed.
*
* This method chooses the best assembly possible, that is,
* it merges the two fragments with the largest overlap, breaking ties
* between merged fragments by choosing the shorter merged fragment.
*
* Merges must have an overlap of at least 1.
*
* After merging two fragments into a new fragment, the new fragment is inserted into
* the list of fragments in this assembler, and the two original fragments are removed
* from the list.
*
* @return true iff an assembly was performed
*/
public boolean assembleOnce() {
int c = 0;
int maxoverlap = -1;
Fragment m1 = null, m2 = null, m = null;
for(Fragment frag1 : Fragments){
for(Fragment frag2 : Fragments){
if(frag1 == null || frag2 == null){
return false;
}
if(frag1!=frag2){
c = frag1.calculateOverlap(frag2);
if(c>maxoverlap){
maxoverlap = c;
m1 = frag1;
m2 = frag2;
}
}
}
}
if(maxoverlap>0){
Fragments.remove(m1);
Fragments.remove(m2);
m = m1.mergedWith(m2);
Fragments.add(m);
return true;
}
else {
return false;
}
}
/**
* Repeatedly assembles fragments until no more assembly can occur.
*/
public void assembleAll() {
while(assembleOnce() !=false){
assembleOnce();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.