Question

In: Computer Science

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, 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

Solutions

Expert Solution

import java.io.*;

import java.lang.*;

import java.util.Scanner;  // Import the Scanner class

import java.lang.Math;

class MyString

{

String word;

//constructor accepting a string vaiable

public MyString(Sting s) {
    word=s; // setting the attribute
}

String permute( String str, int i, int j)

{

char ch[] = str.toCharArray(); //converting to string into char Array

        char temp = ch[i];

        ch[i] = ch[j];

        ch[j] = temp;

  String result = new String(ch); //converting to char Array into String

        return result;

}

}// End of class MyString

Public class Jumble

{

public static void main(String args[])

{

Scanner ss = new Scanner(System.in);

System.out.println("Enter the word");

String p = ss.nextLine();  // Read user input

System.out.println("Enter the number of jumbled versions");

int n = ss.next();  // Read user input

MyString m= new MyString (p);

System.out.println(permute(p, (int)(Math.random() * n), p.length() - 2));

System.out.println(permute(p, (int)(Math.random() * n), p.length() - 1));

}

}


Related Solutions

: 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...
USING JAVA A method that parses the valid word file to a list of Strings. You...
USING JAVA A method that parses the valid word file to a list of Strings. You may use a loop in the parsing method. A nonrecursive method that prints the input String, makes the first call to the recursive anagramizer method, sends the result to the filter method, and prints the filtered result. You may use this code: private void anagramize(String inString) {         System.out.println("input string: " + inString);         List < String > l = filter(anagramizeRecursive(inString.toLowerCase()));         System.out.println("Anagrams: " + l); }
Please show solution and comments for this data structure using java.​ Implement a program in Java...
Please show solution and comments for this data structure using java.​ Implement a program in Java to convert an infix expression that includes (, ), +, -, *,     and / to postfix expression. For simplicity, your program will read from standard input (until the user enters the symbol “=”) an infix expression of single lower case and the operators +, -, /, *, and ( ), and output a postfix expression.
Java - Firstly, write a method, using the following header, that counts the number of prime...
Java - Firstly, write a method, using the following header, that counts the number of prime numbers between from and to (inclusive). public static int countPrimes(int from, int to) For example, countPrimes(11,19) returns 4 since 11, 13, 17 and 19 are primes. You can assume that someone has already written the following function to determine is an integer is prime or not. public static boolean isPrime(int i) // returns true if i is a prime number Secondly, write a program...
Java - Firstly, write a method, using the following header, that counts the number of prime...
Java - Firstly, write a method, using the following header, that counts the number of prime numbers between from and to (inclusive) . public static int countPrimes(int from, int to ) For example, countPrimes(11,19) returns 4 since 11, 13, 17 and 19 are primes. You can assume that someone has already written the following function to determine is an integer is prime or not. public static boolean isPrime(int i) // returns true if i is a prime number Secondly, write...
Exercise 3 – Strings Using a function Write a program that prompts the user to enter...
Exercise 3 – Strings Using a function Write a program that prompts the user to enter two inputs: some text and a word. The program outputs the starting indices of all occurrences of the word in the text. If the word is not found, the program should output “not found”. Example1: Input1: my dog and myself are going to my friend Input2: my Output: 0 11 31 Example 2: Input1: Programming is fun Input 2: my Output: not found
How do you implement an AVL tree for strings in Java?
How do you implement an AVL tree for strings in Java?
IN JAVA, For this exercise, you will create your own example of using a generic method....
IN JAVA, For this exercise, you will create your own example of using a generic method. First write a program that calls a method to multiply two integers and return the integer result. Then modify that program to make the method generic, calling it with two generic data types (T1 and T2) and then returning the result in the same type (also needs to be generic). Demonstrate your program by calling the method two times, once with an integer and...
Core Java Do exercise 9a, 9b, 9c, & 9d with indented code and comments. 9a. public...
Core Java Do exercise 9a, 9b, 9c, & 9d with indented code and comments. 9a. public class Demo {               public static void main(String[] args) {                             //create Employee object with int 101, String Sam,salary 1000 data                             //display this object data by passing to show method                             //add 100 bonus in salary                             //display this object data by passing to show method               }               public static void show(){                             //do required changes in show method               } }...
Basically, the code already functions properly. Could you just write in method header comments explaining what...
Basically, the code already functions properly. Could you just write in method header comments explaining what the method does and what the parameters are? Some of them are already done. Additionally could you change the variables and method names to make them more descriptive of what they're actually holding? Thank you! import java.util.Scanner; /** * This class contains the entire program to print out a yearly calendar. * * @author * @author TODO add your name here when you contribute...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT