Question

In: Computer Science

Assignment Purpose The purpose of this lab is to write a well commented java program that...

Assignment Purpose

The purpose of this lab is to write a well commented java program that demonstrates the use and re-use of methods with input validation.

Instructions

  1. It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example,

“I dn'ot gvie a dman for a man taht can olny sepll a wrod one way.” (Mrak Taiwn)

“We aer all moratls, Hamrbee is an immoratl, and tehre is no question abuot it.” (Kevin Unknown)

  1. Write a method named scramble that returns a String and takes a String as an argument.
    1. The argument is actually a word (of length 6 or more).
    2. It then constructs a scrambled version of that word, randomly flipping two characters other than the first and last one.
  2. Then write the main method in which you would read the word from the user, send it to scramble, and print the scrambled word.
    1. You can use a loop to read multiple words if you like but it is not necessary.
    2. If the length of the entered word is less than 6, you should keep prompting the user to enter a valid word.
  3. After writing all the comments, generate a Javadoc and submit it with the java file.

Hint: First generate two random integers in range of the length of the string. Then use substring method to access characters at those locations. Rest is left to your imagination.

Solutions

Expert Solution

Java code:


/**scramble word
* It is quite interesting that most of us are likely to be able to read and comprehend words,
* even if the alphabets of these words are scrambled (two of them) given
* the fact that the first and last alphabets remain the same
*
*/
import java.util.Scanner;

public class ScrambleWord {
   public String scramble(String word) // scramble method definition
   {
       String result;
       StringBuffer buffer = new StringBuffer(word); // create stringbuffer object
       if (word.length() >= 6) // validate the word length greater than 6 or not
       {
           // get the word length and subtract by 3 to avoid last character
           int len = word.length() - 3;
           // generate random number and add by 1 to avoid first character
           int randomnumber = (int) (Math.random() * len) + 1;
           // generate another random number to fetch another one character
           int randomnumber1 = randomnumber + 1;
           // get the first character to swap
           String first = word.substring(randomnumber, (randomnumber + 1));
           // get the second the chracter to swap
           String second = word.substring(randomnumber1, (randomnumber1 + 1));
           // swap the first character
           buffer.setCharAt(randomnumber, second.charAt(0));
           // swap the second character
           buffer.setCharAt(randomnumber1, first.charAt(0));

           result = buffer.toString();

       } else {
           result = "enter valid word";
       }
       return result;
   }

   public static void main(String args[]) {
       ScrambleWord sw = new ScrambleWord();
       String word, scrambleword;
       Scanner ss = new Scanner(System.in);
       System.out.println("Enter word");
       word = ss.next(); // get word from the user
       scrambleword = sw.scramble(word); // call the scramble method
       System.out.println(scrambleword); // print the scramble word

   }
}
Sample outputs:

sample 1:

sample 2:

Note : Read comments for better understanding of the code.

Steps to generate java document ( I used eclipse IDE)

Step 1 − Open eclipse, select the option Project →Generate Javadoc.

Step 2 − Select the javadoc.exe file from the bin folder of java installation directory,
select the destination folder for the generated java doc and select Next.

Step 3 − Type the title of the documentation in the Document title and select the
finish button.

Step 4 − Finally, you can observe the generated doc files in the generated folder.

You can view the document by opening the index.html file with the browser.


Related Solutions

Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods.(Need Comment, Write by Java Code) Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of two dimensional arrays, input validation, and methods. (Write by Java Code, Need Comment) Instructions A theater seating chart is implemented as a two-dimensional array of ticket prices, like this: Seat Ticket Price 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates the use of loops, and generation of random integers. Instructions You are taking some time off from your paint business and currently are on vacation in Bahamas. You decide to write a Java program that generates 10 random numbers between 1 and 20 (all integers). You cannot use arrays (even if you know what they are) to store these numbers. It then picks up...
Assignment Purpose Write a well commented java program that demonstrates the use and re-use of methods...
Assignment Purpose Write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht can olny sepll a wrod one way.”...
in java Write a Java Program that displays a menu with five different options: 1. Lab...
in java Write a Java Program that displays a menu with five different options: 1. Lab Test Average Calculator 2. Dice Roll 3. Circle Area Calculator 4. Compute Distance 5. Quit The program will display a menu with each of the options above, and then ask the user to enter their choice. There is also a fifth option to quit, in which case, the program will simply display a goodbye message. Based on the user’s choice, one of the options...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering Inheritance. Core Level Requirements (up to 6 marks) The scenario for this assignment is to design an online shopping system for a local supermarket (e.g., Europa Foods Supermarket or Wang Long Oriental Supermarket). The assignment is mostly concentrated on the product registration system. Design and draw a UML diagram, and write the code for the following classes: The first product category is a fresh...
• This lab, you will write a Java program to determine if a given Sudoku puzzle...
• This lab, you will write a Java program to determine if a given Sudoku puzzle is valid or not. • You determine if the puzzle is complete and valid, incomplete, or is invalid. • A puzzle is a 2-dimensional array 9x9 array. Each element contains the numbers 1 – 9. A space may also contain a 0 (zero), which means the spot is blank. • If you don’t know how a Sudoku Puzzle works, do some research, or download...
For this week’s lab assignment, you will write a program called lab9.c. You will write a...
For this week’s lab assignment, you will write a program called lab9.c. You will write a program so that it contains two functions, one for each conversion. The program will work the same way and will produce the same exact output. The two prototypes should be the following: int btod(int size, char inputBin[size]); int dtob(int inputDec); The algorithm for the main() function should be the following: 1. Declare needed variables 2. Prompt user to enter a binary number 3. Use...
In Java please: 5.23 LAB: Seasons Write a program that takes a date as input and...
In Java please: 5.23 LAB: Seasons Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT