Question

Java /** * Reports whether or not s1 and s2 contain exactly the same tokens in...

Java

/**
* Reports whether or not s1 and s2 contain exactly the same tokens in the same order.
*
* Examples: <br>
* sameTokens("this is a test", " this is a test ") is true <br>
* sameTokens("", "") is true <br>
* sameTokens("hello there", "hello there Joe") is false <br>
* sameTokens("abc def", "def abc") is false <br>
* sameTokens("a", "A") is false
*
* IMPLEMENTATION NOTE: You can write this as an accumulation loop with a twist. Be aware
* of the implications of the short-circuited evaluation of &&.
*/

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class CheckTokens {

public static void main(String[] args) {

String s1 = "abc def";

String s2 = "def abc";

// split array into tokens

String sa1[] = s1.split(" ");

String sa2[] = s2.split(" ");

boolean flag = true;

// check both having same number of tokens

if (sa1.length == sa2.length) {

// iterate and checks for each token

for (int i = 0; i < sa1.length; i++) {

if (!sa1[i].equals(sa2[i])) {

flag = false;

break;

}

}

} else {

System.out.println("false");

return;

}

// prints result

System.out.println(flag);

}

}

2 public 3 pub September/src/ReadLine.java ring[] args) 4. String s1- abc def; String s2 = def abc; // split array into tokens string sa1 [ ] = s1. split( ); string sa2[] = s2.split( ); boolean flag = true; // check both having same number of tokens if (sal.lengthsa2.length) { 6 7 10 // iterate and checks for each tokern for (int i = 0; 1 く sal. length; i++) { if (sa1[i].equals(sa2[i])) { 12 13 14 15 16 17 18 19 20 21 flag false; break; else System.out.println(false); return; // prints result 23 24 25 26 System.out.println(flag); Console X <terminated> CheckTokens [Java Application] C:SoftPegaEclipse-win64-4.5.2.2\PegaEclipse-win64-4.5.2. false

Add a comment
Know the answer?
Add Answer to:
Java /** * Reports whether or not s1 and s2 contain exactly the same tokens in...
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
  • In Java, you'll find that it contains several methods that have not yet been finished. For now, the methods contain...

    In Java, you'll find that it contains several methods that have not yet been finished. For now, the methods contain only a placeholder return value so that the code will compile. Fill in your own implementation. public class SomePracticeStringMethods { /* * returns true if c is a letter in the word "temple" or false otherwise */ public static boolean inTU(char c) { /* placeholder just so that the function * compiles. fill in your implementation here. * * there...

  • Problem 1 1. In the src → edu.neiu.p2 → problem1 directory, create a Java class called...

    Problem 1 1. In the src → edu.neiu.p2 → problem1 directory, create a Java class called HW4P1 and add the following: • The main method. Leave the main method empty for now. • Create a method named xyzPeriod that takes a String as a parameter and returns a boolean. • Return true if the String parameter contains the sequential characters xyz, and false otherwise. However, a period is never allowed to immediately precede (i.e. come before) the sequential characters xyz....

  • The last 3 cases are tests .cpp files to test the code. The language of this...

    The last 3 cases are tests .cpp files to test the code. The language of this code must be C++ because that is the only I am familiar with. Soreland, a software company, is planning on releasing its first C++ compiler with the hopes of putting MiniSoft's Visual C++ out of business (that reminds me of another story). Consequently, Soreland has contracted with the Fruugle Corporation to design and build part of their compiler. Of course, since Fruugle gets all...

  • In Java plz due today Assignment 4 - Email, Shwitter and Inheritance Select one option from...

    In Java plz due today Assignment 4 - Email, Shwitter and Inheritance Select one option from below. All (both) options are worth the same number of points. The more advanced option(s) are provided for students who find the basic one too easy and want more of a challenge. OPTION A (Basic): Message, EMail and Tweet Understand the Classes and Problem Every message contains some content ("The British are coming! The British are coming!"). We could enhance this by adding other...

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

  • IN JAVA PLS DUE TODAY Assignment 4 - Email, Shwitter and Inheritance Select one option from...

    IN JAVA PLS DUE TODAY Assignment 4 - Email, Shwitter and Inheritance Select one option from below. All (both) options are worth the same number of points. The more advanced option(s) are provided for students who find the basic one too easy and want more of a challenge. OPTION A (Basic): Message, EMail and Tweet Understand the Classes and Problem Every message contains some content ("The British are coming! The British are coming!"). We could enhance this by adding other...

  • In Problem Set 7 you designed and implemented a Message class. This time, let's design and...

    In Problem Set 7 you designed and implemented a Message class. This time, let's design and implement a Mailbox class in a file named Mailbox java. Do the following with this class • You may use the Message class from PS 7. You will have to add new features to the Message class from PS 7 as you work through this problem. You are welcome to start with my sample solution if you wish • Suppose there are multiple mail...

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