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

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.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: ");...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main {   public static void main( String[] args ) {     Scanner myInput = new Scanner(System.in); // Create a Scanner object     System.out.println("Enter (3) digits: ");     int W = myInput.nextInt();     int X = myInput.nextInt();     int Y = myInput.nextInt();      } } Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and...
Create a new Java file, containing this code public class DataStatsUser { public static void main...
Create a new Java file, containing this code public class DataStatsUser { public static void main (String[] args) { DataStats d = new DataStats(6); d.append(1.1); d.append(2.1); d.append(3.1); System.out.println("final so far is: " + d.mean()); d.append(4.1); d.append(5.1); d.append(6.1); System.out.println("final mean is: " + d.mean()); } } This code depends on a class called DataStats, with the following API: public class DataStats { public DataStats(int N) { } // set up an array (to accept up to N doubles) and other member...
import java.util.LinkedList; public class StudentLinkedList { public static void main(String[] args) { LinkedList<Student> linkedlist = new...
import java.util.LinkedList; public class StudentLinkedList { public static void main(String[] args) { LinkedList<Student> linkedlist = new LinkedList<Student>(); linkedlist.add(new Student("Ahmed Ali", 20111021, 18, 38, 38)); linkedlist.add(new Student("Sami Kamal", 20121021, 17, 39, 35)); linkedlist.add(new Student("Salem Salim", 20131021, 20, 40, 40)); linkedlist.add(new Student("Rami Mohammed", 20111031, 15, 35, 30)); linkedlist.add(new Student("Kim Joe", 20121024, 12, 32, 32)); linkedlist.addFirst(new Student("Hadi Ali", 20111025, 19, 38, 39)); linkedlist.addLast(new Student("Waleed Salim", 20131025, 10, 30, 30)); linkedlist.set(0, new Student("Khalid Ali", 20111027, 15, 30, 30)); linkedlist.removeFirst(); linkedlist.removeLast(); linkedlist.add(0, new Student("John Don",...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following:...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following: // // 1.) Define a private instance variable which can // hold a reference to a Random object. // // 2.) Define a constructor which takes a seed value. // This seed will be used to initialize the // aforementioned Random instance variable. // // 3.) Define a static method named numberToDirection // which takes a direction number and returns a String // representing...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class BowlerReader { private static final String FILE_NAME =...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class BowlerReader { private static final String FILE_NAME = "bowler.txt"; public static void main(String[] args) throws FileNotFoundException { System.out.println("Reading Data from file"); Scanner fileReader = new Scanner(new File(FILE_NAME)); System.out.printf("%-20s%-10s%-10s%-10s%-10s\n", "Sample Data", "Game 1", "Game 2", "Game 3", "Average"); int bowler = 1; while (fileReader.hasNext()) { String scores[] = fileReader.nextLine().split("\\s+"); double average = Integer.parseInt(scores[0]) + Integer.parseInt(scores[1]) + Integer.parseInt(scores[2]); average /= 3; System.out.printf("%-20s%-10s%-10s%-10s%-10.2f\n", "Bowler " + bowler, scores[0], scores[1], scores[2], average); bowler += 1; }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT