Question

In: Computer Science

java: Sample output: Aoccdrnig to reacresh at an Elingsh uinervtisy, it deos not mttaer in waht...

java:

Sample output: Aoccdrnig to reacresh at an Elingsh uinervtisy, it deos not mttaer in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht the frist and lsat ltteer are in the rghit pclae. The rset can be a toatl mses and you can sitll raed it wouthit a porbelm. Tihs is bcuseae we do not raed ervey lteter by it slef but the wrod as a wlohe and the biran fguiers it out aynawy.

PROGRAM DESCRIPTION: In this assignment, you will read an unspecified number of lines of text from STDIN. Input is terminated with EOF (ctrl-d for keyboard input). Your only output, to STDOUT, will echo the input except that the letters of any word with more than three characters will be randomly shuffled (except for the first and last letters). Don't scramble any punctuation, and you may assume that punctuation will only occur at the end of a word. Any word will have, at most, one punctuation character. Punctuation does not count towards the length of the word. You may also assume that each word will be separated by one space and that a line will have no leading or trailing whitespace. Blank lines are allowable. You must preserve the newlines (output must have the same number of lines and the same number of words on each line as when input.) (A word, here, is defined as a sequence of alphanumeric characters delimeted by whitespace.) For this assignment you must code the shuffle algorithm as described in class, and not a library shuffle function.

Sample input:According to research at an English university, it does not matter in what order the letters in a word are, the only important thing is that the first and last letter are in the right place. The rest can be a total mess and you can still read it without a problem. This is because we do not read every letter by itself but the word as a whole and the brain figures it out anyway.

Sample run:

This is a test
This is a tset
Hello, how are you today?
Hlleo, how are you toady?

SUGGESTIONS

You may find it easiest to complete this program in stages. Begin by writing a program that echos input lines of text to output. Then add the ability (a function) to break a line of text into individual words, finally add the scrambling feature (another function) for a word.

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class WordShuffle {
  
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = "";
do
{
System.out.print("Enter a string: ");
input = sc.nextLine().trim();
if(input.equalsIgnoreCase("quit"))
{
System.out.println("Thanks..Goodbye!\n");
System.exit(0);
}
  
// read a line from keyboard and split the words and insert them in an array
String[] data = input.trim().split(" ");
// shuffle the words if the length is greater than 3
String result = "";
for(int i = 0; i < data.length; i++)
{
String word = data[i];
if(word.length() > 3)
result += getShuffledWord(word) + " ";
else
result += word + " ";
}
System.out.println("Result string: " + result.trim() + "\n");
}while(!input.equalsIgnoreCase("quit"));
}
  
private static String getShuffledWord(String str)
{
String res = "";
// array list to hold the characters of the string str
ArrayList<Character> chars = new ArrayList<>();
// a for loop from index 1 to length() - 1 so as to exclude the first and last letter of the str
// and add the remaining letters to the arraylist
char firstLetter = getFirstLetter(str);
char lastLetter = getLastLetter(str);
char punc = 0;
if(!Character.isLetter(str.charAt(str.length() - 1)))
punc = str.charAt(str.length() - 1);
while(res.equalsIgnoreCase(str))
{
if(punc == 0)
{
// the last letter is not a punctuation
for(int i = 1; i < str.length() - 1; i++)
chars.add(str.charAt(i));
}
else
{
// the last letter is a punctuation
for(int i = 1; i < str.length() - 2; i++)
chars.add(str.charAt(i));
}
  
// shuffle the arraylist
Collections.shuffle(chars);
// now arrange the letters
res += firstLetter + "";
for (Character c : chars)
res += c;
if(punc != 0)
res += lastLetter + String.valueOf(punc);
else
res += lastLetter;
}
  
if (punc == 0) {
// the last letter is not a punctuation
for (int i = 1; i < str.length() - 1; i++) {
chars.add(str.charAt(i));
}
} else {
// the last letter is a punctuation
for (int i = 1; i < str.length() - 2; i++) {
chars.add(str.charAt(i));
}
}
  
// shuffle the arraylist
Collections.shuffle(chars);
// now arrange the letters
res += firstLetter + "";
for (Character c : chars)
res += c;
// if there is a punctuation, add it to the end of the word
if(punc != 0)
res += lastLetter + String.valueOf(punc);
else
res += lastLetter;
  
return res;
}
  
private static char getFirstLetter(String s)
{
char res = 0;
for(int i = 0; i < s.length(); i++)
{
if(Character.isLetter(s.charAt(i)))
{
res = s.charAt(i);
break;
}
}
return res;
}
  
private static char getLastLetter(String s)
{
char res = 0;
for(int i = s.length() - 1; i >= 0; i--)
{
if(Character.isLetter(s.charAt(i)))
{
res = s.charAt(i);
break;
}
}
return res;
}
}

******************************************************** SCREENSHOT ********************************************************


Related Solutions

The Language is Java. Sample Output: There is no given sample Output IsEqualToTest (30) Write a...
The Language is Java. Sample Output: There is no given sample Output IsEqualToTest (30) Write a simple generic version of method isEqualTo that compares its two arguments with the equals method and returns true if they’re equal and false otherwise. Use this generic method in a program that calls isEqualToTest with the built-in types Integer, String, Double and Object. The main reads in two Integer, and two Double values with autoboxing, as well as two String and calls the method....
This needs to be in Java implementing Insertion Sort and displaying the floats sorted. Sample Output:...
This needs to be in Java implementing Insertion Sort and displaying the floats sorted. Sample Output: (green is user input) Please select one of the following: 1: Initialize a default array 2: To specify the max size of the array 3: Add value to the array 4: Display values in the array 5: Display the average of the values 6: Enter multiple values 7: Read values from file 8: Save values to file 9: Sort the array 10: To Exit...
It's Java; the code should be the same as the output sample below; please, thank you....
It's Java; the code should be the same as the output sample below; please, thank you. Note: create a single-file solution that has multiple class definitions. Also, note that the static main() method should be a member of the Vehicle class. Create a class called Vehicle that has the manufacturers name (type String), number of cylinders in the engine (type int), and owner (type Person given next). Then, create a class called Truck that is derived from Vehicle and has...
java.. please dont change the format and give me an output sample! user need to input...
java.. please dont change the format and give me an output sample! user need to input k. public class Josephus {    /**    * All persons sit in a circle. When we go around the circle, initially starting    * from the first person, then the second person, then the third...    * we count 1,2,3,.., k-1. The next person, that is the k-th person is out.    * Then we restart the counting from the next person, go...
In JAVA answer has to be able to do exact Sample Output shown Write a do-while...
In JAVA answer has to be able to do exact Sample Output shown Write a do-while loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user whether he or she wishes to perform the operation again. If so, the loop should repeat; otherwise it should terminate. Name your class as NumberSum and add to it your header and sample output as a block comments. Upload the...
Waht is the application of flixibe manufacturing system (FMS)??
Waht is the application of flixibe manufacturing system (FMS)??
Please code by Java. The following preemptive program with this problem after sample output(fill random number,...
Please code by Java. The following preemptive program with this problem after sample output(fill random number, recursive search, non-recursive search, exit), You just need to modify this code to add sorting and time calculations. Problem Description: Expand on Lab3 by adding methods that implement Bubble Sort, Selection Sort, Insertion Sort, Merge Sort and Quick Sort for displaying the sorted integers from the randomly generated array. NOTE: Create separate methods for each type of sort. Run Tests for all 4 types...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards to the other...
What is control system waht are its types and differentiate them.
What is control system waht are its types and differentiate them.
C program that demonstrate tree traversal. see for the sample output. Sample Output: How many node...
C program that demonstrate tree traversal. see for the sample output. Sample Output: How many node do you have in your tree : 5 Enter root: 20 if you want to add to the left press 0 otherwise press 1 answer: 0 Enter next node: 10 if you want to add to the left press 0 otherwise press 1 answer: 0 Enter next node: 8 if you want to add to the left press 0 otherwise press 1 answer: 0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT