Question

In: Computer Science

import java.util.List; public class Assembler { /** * Creates a new Assembler containing a list of...

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.

*

* @return true iff an assembly was performed

*/

public boolean assembleOnce() {

return false;

}

/**

* Repeatedly assembles fragments until no more assembly can occur.

*/

public void assembleAll() {

}

}

Solutions

Expert Solution

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 Solutions

Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public final class Account implements Comparable {     private String firstName;     private String lastName;     private int accountNumber;     private double balance;     private boolean isNewAccount;     public Account(             String firstName,             String lastName,             int accountNumber,             double balance,             boolean isNewAccount     ) {         this.firstName = firstName;         this.lastName = lastName;         this.accountNumber = accountNumber;         this.balance = balance;         this.isNewAccount = isNewAccount;     }     /**      * TO DO: override equals      */     @Override     public boolean equals(Object other) {...
import java.util.LinkedList; import java.util.Queue; import java.time.*; public class CheckOutLine { /** This is the main function...
import java.util.LinkedList; import java.util.Queue; import java.time.*; public class CheckOutLine { /** This is the main function for the program */ public static void main() { System.out.println("\n\tBegin CheckOutLine Demo\n"); System.out.println("\nCreating a Queue called \"register\" based on a LinkedList"); Queue<Customer> register = new LinkedList<Customer>();    System.out.println("\nAdding Customers to the Register queue"); // 1. TO DO: add five or more customers to the register line (5 Pts) // format: register.add(new Customer(name, checkout time));                System.out.printf("Register line has %d customers\n",...
import java.util.ArrayList; import java.util.Collections; import java.lang.Exception; public class ItemList { /** This is the main process...
import java.util.ArrayList; import java.util.Collections; import java.lang.Exception; public class ItemList { /** This is the main process for the project */ public static void main () { System.out.println("\n\tBegin Item List Demo\n"); System.out.println("Declare an ArrayList to hold Item objects"); ArrayList<Item> list = new ArrayList<Item>(); try { System.out.println("\n Add several Items to the list"); list.add(new Item(123, "Statue")); list.add(new Item(332, "Painting")); list.add(new Item(241, "Figurine")); list.add(new Item(126, "Chair")); list.add(new Item(411, "Model")); list.add(new Item(55, "Watch")); System.out.println("\nDisplay original Items list:"); listItems(list); int result = -1; // 1....
import java.util.LinkedList; import java.util.ListIterator; public class ProjectList { /** This is the main function for the...
import java.util.LinkedList; import java.util.ListIterator; public class ProjectList { /** This is the main function for the program */ public static void main() { System.out.println("\n\tBegin ProjectList demo program");    // 1. TO DO: Describe your project (1 Pt) System.out.println("Project: . . . . . . \n"); System.out.println("Create a LinkedList to store the tasks"); // 2. TO DO: Declare a LinkedList of String objects called "tasks" (1 Pt)       System.out.println("Add initial tasks into the list"); // 3. TO DO: Add initial...
import java.util.ArrayList; import java.util.Collections; public class BirthdayList { /** * This is the main process for...
import java.util.ArrayList; import java.util.Collections; public class BirthdayList { /** * This is the main process for class BirthdayList */ public static void main() { System.out.println("\n\tBegin Birthday List Program\n");    System.out.println("Declare an ArrayList for Birthday objects"); // 1. TO DO: Declare an ArrayList to store Birthday objects (3 Pts)       // 2. TO DO: Replace the xxx.xxxx() with the appropriate method (2 Pts) System.out.printf("\nBirthday List is empty: %s\n", xxx.xxxx() ? "True" : "False");    System.out.println("Add at least five Birthdays to...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r =...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r = new Random(seed); }    public static String numberToDirection(int a){ if(a==0) return "North";    if(a==1) return "NorthEast"; if(a==2) return "East"; if(a==3) return "Southeast"; if(a==4) return "South"; if(a==5) return "Southwest"; if(a==6) return "West";    if(a==7) return "Northwest";    return "Invalid Direction" ; } public String randomDirection(){ return numberToDirection(r.nextInt()% 4 + 1); } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter seed: ");...
import java.util.Scanner; public class Squaring { public static void main(String[] args) { Scanner sc = new...
import java.util.Scanner; public class Squaring { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num=0; String s = ""; while (true) { System.out.println("Enter an integer greater than 1: "); try { // reading input s = sc.nextLine(); // converting into int num = Integer.parseInt(s); break; } catch (Exception e) { System.out.println(s + " is not valid input."); } } // Now we have a valid number // putting into square int square = num; int count...
import java.util.Scanner; public class ZombieApocalypse{    public static void main(String[] args){    Scanner input = new...
import java.util.Scanner; public class ZombieApocalypse{    public static void main(String[] args){    Scanner input = new Scanner(System.in);           boolean gameOver = false; int colSize = 10; int rowSize= 10; String floorTile= "."; int playerX = 0; int playerY= 0; String playerTile="@"; int exitX= colSize-1; int exitY= rowSize-1; String exitTile="# "; int zombieX=5; int zombieY=5; // Defining Second Zombie int zombie2Y= 8; int zombie2X= 3; // Defining third zombie int zombie3Y= 1; int zombie3X= 7; String zombieTile="*"; String zombie2Tile="*";...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list =...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list = arrayList; return list; } public static void printList(ArrayList<String> arrayList) { System.out.println("Printing in 4 ways\n"); // 1 System.out.println(arrayList); //2 for(String s:arrayList) System.out.print(s+" "); System.out.println(); //3 System.out.println(Arrays.deepToString(list.toArray())); //4 for(int i=0;i<arrayList.size();i++) System.out.print(arrayList.get(i)+" "); System.out.println(); } public static void filterList(ArrayList<String> arrayList) { System.out.println("Filtered in 2 ways\n"); ArrayList<String> copyArrayList = arrayList; //1 for(int i=0;i<arrayList.size();i++) { if(arrayList.get(i).contains("chuck")) { arrayList.remove(i); i--; } } System.out.println(arrayList); //2 copyArrayList.removeIf(str -> str.contains("chunk")); System.out.println(copyArrayList); }   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT