Question

In: Computer Science

: This exercise will give you a review of Strings and String processing. Create a class...

: This exercise will give you a review of Strings and String processing. Create a class called MyString that has one String called word as its attribute and the following methods: Constructor that accepts a String argument and sets the attribute. Method permute that returns a permuted version of word. For this method, exchange random pairs of letters in the String. To get a good permutation, if the length of the String is n, then perform 2n swaps. Use this in an application called Jumble that prompts the user for a word and the required number of jumbled versions and prints the jumbled words. For example, Enter the word: mixed Enter the number of jumbled versions required: 10 xdmei eidmx miexd emdxi idexm demix xdemi ixdme eximd xemdi xdeim Notes: 1. It is tricky to swap two characters in a String. One way to accomplish this is to convert your String into an array of characters, swapping the characters in the array, converting it back to a String and returning it. char[] chars = word.toCharArray(); will convert a String word to a char array chars. String result = new String(chars); converts a char array chars into a String. p 6 2. Use Math.random to generate random numbers. (int)(n*Math.random()) generates a random number between 0 and n-1. java

Solutions

Expert Solution

JAVA CODE: (MyString.java)

import java.util.*;

public class MyString
{
   //variable to store the word
   String word;  
  
  
   // constructor to initialize the object
   public MyString(String word)
   {
       this.word=word;
   }
  
   // method to return a permuted word
   public String permute()
   {
  
       // converting word into character array
       char[] chars = word.toCharArray();
      
      
       // finding the length of the word
       int n=word.length();
      
      
       // swapping characters for 2 * n times
       for(int i=0;i< 2*n;i++)
       {
          
           // finding two random numbers
           int r1=(int)(n*Math.random());
           int r2=(int)(n*Math.random());
          
           //swapping the characters at position r1 and r2 of the array
           char t=chars[r1];          
           chars[r1]=chars[r2];
           chars[r2]=t;          
       }
      
       //converting character array into string
       String result = new String(chars);
      
       // return the string
       return result;
   }
}

JAVA CODE (Jumble.java)

import java.util.*;

public class Jumble
{
   public static void main(String args[])
   {
       //creating scanner object to get keyboard input
       Scanner kb=new Scanner(System.in);
      
       //getting the word from the user
       System.out.print("Enter the word: ");
       String word=kb.next();
      
       // getting the n value
       System.out.print("\nEnter the number of jumbled versions required: ");
       int n=kb.nextInt();
      
       System.out.println();
      
       // calling the permute() method for n times
       for(int i=0;i<n;i++)
       {
           MyString ob=new MyString(word);
           System.out.println(ob.permute());
       }
      
   }
  
}

SCREENSHOT FOR OUTPUT:


Related Solutions

using java with proper header and comments: This exercise will give you a review of Strings...
using java with proper header and comments: This exercise will give you a review of Strings and String processing. Create a class called MyString that has one String called word as its attribute and the following methods: Constructor that accepts a String argument and sets the attribute. Method permute that returns a permuted version of word. For this method, exchange random pairs of letters in the String. To get a good permutation, if the length of the String is n,...
The split method in the String class returns an array of strings consisting of the substrings...
The split method in the String class returns an array of strings consisting of the substrings split by the delimiters. However, the delimiters are not returned. Implement the following new method that returns an array of strings consisting of the substrings split by the matching delimiters, including the matching delimiters. public static String[] split(String s, String regex) For example, split("ab#12#453", "#") returns ab, #, 12, #, 453 in an array of String, and split("a?b?gf#e", "[?#]") returns a, ?, b, ?,...
Given the strings s1 and s2 that are of the same length, create a new string...
Given the strings s1 and s2 that are of the same length, create a new string s3 consisting of the last character of s1 followed by the last character of s2, followed by the second to last character of s1, followed by the second to last character of s2, and so on
C PROGRAM STRING AND FILE PROCESSING LEAVE COMMENTS! I WILL LEAVE POSITIVE REVIEW! THANK YOU :)...
C PROGRAM STRING AND FILE PROCESSING LEAVE COMMENTS! I WILL LEAVE POSITIVE REVIEW! THANK YOU :) I need a program that 1) Count all words in a file. A word is any sequence of characters delimited by white space or the end of a sentence, whether or not it is an actual English word. 2)Count all syllables in each word. To make this simple, use the following rules: •Each group of adjacent vowels (a, e, i, o, u, y) counts...
Create a class that calls for user to input a string. Pass the string from that...
Create a class that calls for user to input a string. Pass the string from that class to another and count how many words the string has, count how many vowels are in the string and count how many letters end with d. java language
---------------- Exercise 2: String Permutations ---------------- Create a program that takes a string from the command...
---------------- Exercise 2: String Permutations ---------------- Create a program that takes a string from the command line and prints every permutation of that string. You may assume the string will contain all unique characters. You may print the permutations in any order, as long as you print them all. ---------------- Output: ---------------- $>./prog dog d do dog dg dgo o od odg og ogd g gd gdo go god ---------------- I need help on this exercise that requires recursion only....
---------------- Exercise 2: String Permutations ---------------- Create a program that takes a string from the command...
---------------- Exercise 2: String Permutations ---------------- Create a program that takes a string from the command line and prints every permutation of that string. You may assume the string will contain all unique characters. You may print the permutations in any order, as long as you print them all. ---------------- Output: ---------------- $>./prog dog d do dog dg dgo o od odg og ogd g gd gdo go god ---------------- I have no idea on coding this. The files I...
Class Instructions You will create a password verification program using C-strings and character testing. The user...
Class Instructions You will create a password verification program using C-strings and character testing. The user will enter a password as a C-string. (Character array). You must test that the password contains at least: One lowercase letter One uppercase letter One number (0-9) The password can contain no spaces You are to add one more requirement to the password. I need help in setting this program up. #include <iostream> #include <cctype> using namespace std; int main() { char input; cout...
If we create a 5 digit bit string that is randomly generated, all strings equally likely......
If we create a 5 digit bit string that is randomly generated, all strings equally likely... 1. possibility of string containing three consecutive zeroes? 2. conditional probability of it containing three consecutive zeroes where first number is a one?
Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT