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, ?,...
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
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...
The purpose of this exercise is to give you experience in writing condition-controlled loops. Create a...
The purpose of this exercise is to give you experience in writing condition-controlled loops. Create a code in Python that will print monthly loan amortization schedule given the loan amount (P), APR i.e. Annual Percentage Rate (r=APR in %/12), loan terms in number of months (n). Please use these formulas: Monthly Payment (A) = P * ((r * (1 + r)**n)/(((1+r)**n)-1))            Monthly Interest Pmt (mIP)= r * Total Remaining Balance Monthly Principal Pmt (mPP) = A –...
In C++, create a class to hold a set of strings called setTree. setTrees contain copies...
In C++, create a class to hold a set of strings called setTree. setTrees contain copies of the strings inserted so that the strings cannot be changed due to the fact that changing the strings inside the set could break the binary search tree. The strings are case sensitive. TreeSet implements the following: bool add(const string& s) -- add s to the set, if it's not already there. Return true if the set changed, false otherwise. void clear() -- remove...
Recall that a 5-bit string is a bit strings of length 5, and a bit string...
Recall that a 5-bit string is a bit strings of length 5, and a bit string of weight 3, say, is one with exactly three 1’s. a. How many 5-bit strings are there? b. How many 5-bit strings have weight 0? c. How many 5-bit strings have weight 1? d. How many 5-bit strings have weight 2? e. How many 5-bit strings have weight 4? f. How many 5-bit strings have weight 5? g. How many 5-bit strings have weight...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT