Question

In: Computer Science

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

  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.

Submission

A .java and a .html (generated with Javadoc) file

Solutions

Expert Solution

import java.util.Random;
import java.util.Scanner;

public class A {
        /**
        * Returns a scramble string. 
        * The string argument is the string which user wants to scramble. 
        * <p>

        * @param       str string to be scrambled
        * @return      the scrambled string
        */
        public static String scramble(String str)
        {
                Random rand=new Random();
                int n=str.length();
                int r;
                StringBuilder s=new StringBuilder(str);
                for(int i=1;i<n-1;i++)
                {
                        r=Math.abs(rand.nextInt(n-2))+1;
                        s.setCharAt(i, str.charAt(r));
                        s.setCharAt(r, str.charAt(i));
                        str=s.toString();
                }
                return s.toString();
        }
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Scanner src=new Scanner(System.in);
                System.out.println("Enter any string:");
                String s=src.next();
                while(s.length()<6)
                {
                        System.out.println("Enter a valid word");
                        s=src.next();
                }
                System.out.println(scramble(s));

        }

}

Related Solutions

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 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 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...
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...
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...
3) Create a Java program that uses NO methods, but use scanner: Write a program where...
3) Create a Java program that uses NO methods, but use scanner: Write a program where you will enter the flying distance from one continent to another, you will take the plane in one country, then you will enter miles per gallon and price of gallon and in the end it will calculate how much gas was spend for that distance in miles. Steps: 1) Prompt user to enter the name of country that you are 2) Declare variable to...
CIT 149 JAVA 1 program question? Write a program that will use static methods as described...
CIT 149 JAVA 1 program question? Write a program that will use static methods as described in the specifications below. Utilizing the if and else statements, write a program which will calculate a commission based on a two-tiered commission plan of 3% and 7% paid on amounts of $15,000 or less, or over $15,000 in monthly sales by a sales force paid on a commission basis. Use the output specifications below. ? Specifications There will be two classes (separate files)...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads three strings from the keyboard. Although the strings are in no particular order, display the string that would be second if they were arranged lexicographically.
JAVA Lab Assignment #13:  Looping Lab with both types of loops. This lab demonstrates the use of...
JAVA Lab Assignment #13:  Looping Lab with both types of loops. This lab demonstrates the use of the While Loop and the Do While Loop as error checking mechanisms. You will be using each of these loops to solve the same problem. Please put them both in the same program. When you test the code, you will not see a difference in the way they execute - but there will be a difference in the logic when writing the code. You...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT