Question

Develop an application that, given two sequences of words, will determine if the first is a...

Develop an application that, given two sequences of words, will determine if the first is a subsequence of the second. The subsequence is defined as follow:

Given X = <x1,x2,…,xn> a sequence of length n greater or equal to zero and Y = <y1,y2,…,ym> a sequence of length m greater than or equal to zero, X will be consider a subsequence of Y if and only if there exists a strictly increasing sequence of elements K = <k1, k2,…,kn> such that every element xi is equal to yj where j = ki.

Consider the following examples:

  • X = < a b a> and Y = <a b a> è X is a subsequence of Y
  • X = < a b c> and Y = <a c b > è X is not a subsequence of Y
  • X = < a b a> and Y = <b c a c b a> è X is a subsequence of Y
  • X = < a b a> and Y = <b c a c a b> è X is not a subsequence of Y
  • X = < a b a a> and Y = <b c a c b a> è X is not a subsequence of Y
  • X = < a b a> and Y = <b c a c b> è X is not a subsequence of Y
  • X = < a b c> and Y = <b c a c b a> è X is not a subsequence of Y
  • X = < > and Y = <b c a c b a> è X is a subsequence of Y

Your Task:

  1. The skeleton of the program is provided for you. You need to give implementation for method isSubsequence using appropriate Iterator objects. After the check is done the sequences must remain intact.
  2. A sample run of the program and a UML diagram are provided.
  3. Make sure that the output is correct (see Sample Run below).
public class Sequence
{
    private ArrayList<String>  sequence;

    public Sequence(String... input)
    {
        this.sequence = new ArrayList<>();
        Collections.addAll(this.sequence,input);
    }

    public Sequence (ArrayList<String> input)
    {
        this.sequence = new ArrayList<>(input);
    }

    public void add (String element)
    {
        this.sequence.add(element);
    }

    public void clear()
    {
        this.sequence.clear();
    }

    public String toString()
    {
        return this.sequence.toString();
    }


    /**
     * isSubSequenceOf - determine if one sequence is a subsequence of the other
     *
     * @param other the second sequence to test
     * @return    a true if the first list is a subsequence of the other
     */
    private boolean isSubSequenceOf(Sequence other)
    {
        // TODO Project 4
        boolean result = false;
        boolean foundMatch = false;
        Iterator<String> iterThis  = this.sequence.iterator();
        Iterator<String> iterOther  = other.sequence.iterator();
        String itemOfThisList = null;
        String itemOfOtherList = null;


        return result;
    }
0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • I have implemented the above problem as per the requirement in the question.
  • I have attached the screenshot of output of the program.

Source code:-

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Scanner;

public class Sequence
{
    private ArrayList<String> sequence;

    public Sequence(String... input)
    {
        this.sequence = new ArrayList<>();
        Collections.addAll(this.sequence,input);
    }

    public Sequence (ArrayList<String> input)
    {
        this.sequence = new ArrayList<>(input);
    }

    public void add (String element)
    {
        this.sequence.add(element);
    }

    public void clear()
    {
        this.sequence.clear();
    }

    public String toString()
    {
        return this.sequence.toString();
    }


    /**
     * isSubSequenceOf - determine if one sequence is a subsequence of the other
     *
     * @param other the second sequence to test
     * @return    a true if the first list is a subsequence of the other
     */
    private boolean isSubSequenceOf(Sequence other)
    {
        // TODO Project 4
        boolean result = false;
        boolean foundMatch = false;
        Iterator<String> iterThis = this.sequence.iterator(); // what is the iterator iterating over?
        Iterator<String> iterOther = other.sequence.iterator();
        String itemOfThisList, itemOfOtherList;

        if (this.sequence.isEmpty())
        {
            result = true;
        }
        else if (this.sequence.size() > other.sequence.size())
        {
            result = false;
        }
        else
        {
            while (iterThis.hasNext() && iterOther.hasNext())
            {
                itemOfThisList = iterThis.next();
                do
                {
                    itemOfOtherList = iterOther.next();
                    foundMatch = false;
                } while (iterOther.hasNext() && !itemOfThisList.equals(itemOfOtherList));
                if (itemOfThisList.equals(itemOfOtherList))
                    foundMatch = true;
            }
            if (foundMatch && !iterThis.hasNext())
            {
                result = true;
            }
        }
        return result;
    }

  
    public static void main(String args[])
    {
        Scanner keyboard = new Scanner(System.in);
        Sequence seq1 = new Sequence("a","b","c");
        System.out.println("The seq1 is " + seq1);

        System.out.println("==> Please enter a sequence of words on a single line,"
                + " (words should be separated by spaces), or stop.");
        String line = keyboard.nextLine();
        while (!line.equalsIgnoreCase("stop"))
        {
            Sequence seq2 = new Sequence(line.split("\\s"));
            System.out.println("Is " + seq1 + " a subsequence of " + seq2 + ": " + seq1.isSubSequenceOf(seq2));
            System.out.println("==> Please enter a sequence of words on a single line,"
                    + " (words should be separated by spaces), or stop.");
            line = keyboard.nextLine();
        }

      
    }
}

--------------------------------------------------------------------------------------------------------------------------------

Output:-

***********************************************************************************

if you are satisfy with my answer then please,please like it.

Add a comment
Know the answer?
Add Answer to:
Develop an application that, given two sequences of words, will determine if the first is a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses...

    When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Any suggestions? public class LinkedList<T> {    Node<T> itsFirstNode;    Node<T> itsLastNode;    private int size;    public LinkedList() {        itsFirstNode = null;        itsLastNode = null;          size = 0;    }    public Iterator<T> getIterator() {        return new Iterator(this);    }    // THIS WILL NEED...

  • Using a doubly linked list as the underlying data structure, implement a list ADT that implements...

    Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...

  • Below is the given code of implementation: #include <string> #include <iostream> #include <list> #include <cassert> using...

    Below is the given code of implementation: #include <string> #include <iostream> #include <list> #include <cassert> using namespace std; class List; class Iterator; class Node { public: /* Constructs a node with a given data value. @param s the data to store in this node */ Node(string s); /* Destructor */ ~Node() {} private: string data; Node* previous; Node* next; friend class List; friend class Iterator; }; class List { public: /** Constructs an empty list. */ List(); /* Destructor. Deletes...

  • LAB: Plant information (ArrayList)

    10.16 LAB: Plant information (ArrayList)Given a base Plant class and a derived Flower class, complete main() to create an ArrayList called myGarden. The ArrayList should be able to store objects that belong to the Plant class or the Flower class. Create a method called printArrayList(), that uses the printInfo() methods defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to the myGarden ArrayList, and output each element in myGarden using the printInfo() method.Ex. If the input is:plant Spirea 10  flower Hydrangea 30 false lilac  flower Rose 6 false white plant Mint 4...

  • I was told I need three seperate files for these classes is there anyway to tie...

    I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

  • Hi, So I have a finished class for the most part aside of the toFile method...

    Hi, So I have a finished class for the most part aside of the toFile method that takes a file absolute path +file name and writes to that file. I'd like to write everything that is in my run method also the toFile method. (They are the last two methods in the class). When I write to the file this is what I get. Instead of the desired That I get to my counsel. I am having trouble writing my...

  • Can anyone helps to create a Test.java for the following classes please? Where the Test.java will...

    Can anyone helps to create a Test.java for the following classes please? Where the Test.java will have a Scanner roster = new Scanner(new FileReader(“roster.txt”); will be needed in this main method to read the roster.txt. public interface List {    public int size();    public boolean isEmpty();    public Object get(int i) throws OutOfRangeException;    public void set(int i, Object e) throws OutOfRangeException;    public void add(int i, Object e) throws OutOfRangeException; public Object remove(int i) throws OutOfRangeException;    } public class ArrayList implements List {   ...

  • Why am I getting compile errors. rowPuzzle.java:9: error: class, interface, or enum expected public static boolean...

    Why am I getting compile errors. rowPuzzle.java:9: error: class, interface, or enum expected public static boolean rowPuzzle(ArrayList<Integer> squares, int index, ArrayList<Boolean> visited) ^ rowPuzzle.java:16: error: class, interface, or enum expected } //Java Program import java.util.*; // rowPuzzle helper function implementation // File: rowPuzzle.cpp // Header files section // function prototypes public static boolean rowPuzzle(ArrayList<Integer> squares, int index, ArrayList<Boolean> visited) { // base case // return true if the puzzle is solvable if (index == squares.size() - 1) {    return...

  • Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test...

    Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test class: Remember your header with name, date, and assignment. Also include class names that will be tested. Psuedocode (level 0 or mixture of level 0 and algorithm [do not number steps]) is required if main() contains more than simple statements (for example, your program includes constructs for decisions (if/else), loops, and methods. For Secondary class(es): Include a JavaDoc comment that describes the purpose of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT