Question

In: Computer Science

Using java, create a class called MyString that has one String called word as its attribute...

Using java, 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.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//MyString.java class

public class MyString {

      // word

      private String word;

      // constructor accepting a word

      public MyString(String word) {

            this.word = word;

      }

      // method that returns a random permutation

      public String permute() {

            // converting word to array

            char arr[] = word.toCharArray();

            // looping for arr.length/2 number of times

            for (int i = 0; i < arr.length / 2; i++) {

                  // generating two random indices between 0 and arr.length-1

                  int randIndex1 = (int) (Math.random() * arr.length);

                  int randIndex2 = (int) (Math.random() * arr.length);

                  // swapping elements at these indices

                  char c = arr[randIndex1];

                  arr[randIndex1] = arr[randIndex2];

                  arr[randIndex2] = c;

            }

            //converting array back to String and returning it

            return new String(arr);

      }

}

//Jumble.java file

import java.util.Scanner;

public class Jumble {

      public static void main(String[] args) {

            // setting up a Scanner

            Scanner scanner = new Scanner(System.in);

            // asking and reading word

            System.out.print("Enter the word: ");

            String word = scanner.next();

            // creating a MyString object using word

            MyString myString = new MyString(word);

            // asking and reading an integer

            System.out.print("Enter the number of jumbled versions required: ");

            int n = scanner.nextInt();

            // looping for n times

            for (int i = 0; i < n; i++) {

                  // finding a permutation and printing it

                  System.out.println(myString.permute());

            }

      }

}

/*OUTPUT*/

Enter the word: amazing

Enter the number of jumbled versions required: 10

mganiza

amziagn

amiznag

ngazaim

anazigm

gmaznai

imanagz

azanimg

aiazmng

zmagani


Related Solutions

Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that defines an integer parameter to set the private integer attribute. Create a setter that validates the attribute does not accept a value lower than 2 or the method will throw a IllegalArgumetException. Create a getter to return the private integer attribute value. Define a public method that is called isPrime() that returns a boolean and implements the Sieve of Eratosthenes method. Define a public...
A. Create a Dollar currency class with two integer attributes and one string attribute, all of...
A. Create a Dollar currency class with two integer attributes and one string attribute, all of which are non-public. The int attributes will represent whole part (or currency note value) and fractional part (or currency coin value) such that 100 fractional parts equals 1 whole part. The string attribute will represent the currency name. B. Create a CIS22C Dollar derived/inherited class with one additional non-public double attribute to represent the conversion factor from/to US Dollar. The value of the conversion...
Code in Java Create a class named DictionaryWord as: DictionaryWord - word: String                            &n
Code in Java Create a class named DictionaryWord as: DictionaryWord - word: String                                                              - meanings: String + DictionaryWord (String word, String meanings) + getWord(): String + setWord (String word): void + getMeanings(): String + setMeanings(String meanings): void Write a program with the following requirements: Creates 8 DictionaryWord objects with: Word and meanings as the table: word meanings bank robber Steals money from a bank burglar Breaks into a home to steal things forger Makes an illegal copy of something...
1. public class MyString { private char[] data; MyString(String string) { data = string.toCharArray(); } public...
1. public class MyString { private char[] data; MyString(String string) { data = string.toCharArray(); } public int compareTo(MyString other) { /* code from HW1 here */ } public char charAt(int i) { return data[i]; } public int length() { return data.length; } public int indexOf(char c) { /* code from HW1 here */ } public boolean equals(MyString other) { /* code from HW1 here */ } /* * Change this MyString by removing all occurrences of * the argument character...
Using java: Write a class called StringAlternateSorting what it does: 1. ask user for one String...
Using java: Write a class called StringAlternateSorting what it does: 1. ask user for one String (String1) (must be sorted) 2. ask user for second String (String2) (must be sorted) 3. Then prints all the characters of two previous Strings, in such a way that they are sorted (see examples below). 4. consider the possibility that strings are of different sizes, one or both can be empty, etc. 5. strings can contain any kind of characters as long as they...
C++ PROGRAM 1) Create a "CashOut" currency class with two integer attributes and one string attribute,...
C++ PROGRAM 1) Create a "CashOut" currency class with two integer attributes and one string attribute, all of which are non-public. The int attributes will represent the whole part (or currency note value) and fractional part (or currency coin value) such that 100 fractional parts equal 1 whole part. The string attribute will represent the currency name. 2) Create a "MoreCash" derived/inherited class with one additional non-public double attribute to represent the conversion factor from/to US Dollar. The value of...
Using Java Create the class RightTriangle which is a triangle with one of its angles equal...
Using Java Create the class RightTriangle which is a triangle with one of its angles equal to 90º – a “right angle”. Its constructor will require the lengths only of its two legs. It will then calculate the length of the third side (its “hypotenuse”) using the Pythagorean Theorem: a2 + b2 = c2. This class will have no instance variables itself, so it will need to set the appropriate variables in its parent class, Triangle. It will inherit equals...
Create a class called Dishwash with a double called CubicFeet, a string called color, and a...
Create a class called Dishwash with a double called CubicFeet, a string called color, and a method called Washing. The Washing method should return a string to the main class with the text "Now washing dishes!" In the main class create an array of DishWasher size 3. Give each DishWasher a different color and CubicFeet. Use a foreach loop to display that info, call the Washing method, and display the text returned from the Washing method call. c#
C++ For this assignment, you will implement the MyString class. Like the string class in C++’s...
C++ For this assignment, you will implement the MyString class. Like the string class in C++’s standard library, this class uses C-strings as the underlying data structure. Recall that a C-string is a null-terminated array of type char. See section 8.1 in Savitch for more information about C-strings; in particular, you will find it helpful to take advantage of several of the C-string functions mentioned in that section. What To Do. In the hw8 directory that is created, you will...
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the...
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the class name YourlastnameLab7 with your actual last name. Create a Java class file for a Polygon class. Implement the Polygon class. Add a private instance variable representing the number of sides in the polygon. Add a constructor that takes a single argument and uses it to initialize the number of sides. If the value of the argument is less than three, display an error...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT