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

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...
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...
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...
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#
Using Java: Create a class that will hold the information about a clock, called Clock. You...
Using Java: Create a class that will hold the information about a clock, called Clock. You need to keep minutes, hours and seconds in military time (24-hour) format. Include member functions that will setTime ( this will set the hours, minutes and seconds of the clock), getHours (return the hours), getMinutes (return the minutes) and getSeconds (return the seconds). Another method printTime which will print the time out like this 03:13:09 (hours:minutes:seconds). Overloaded constructors that will take in hours, minutes...
Let's get some practice implementing a known interface. Create a public class named MyString. MyString should...
Let's get some practice implementing a known interface. Create a public class named MyString. MyString should provide a public constructor that accepts a single String argument. You should reject null Strings in your constructor using assert. MyString should also implement the Java Comparable interface, returning 1 for a positive result and -1 for a negative result. Normally Strings are compared lexicographically: "aaa" comes before "z". MyString should compare instances based on the length of its stored String. So MyString("aaa") should...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT