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...
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.
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...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of the following sorting methods (Insertion Sort, Selection Sort, Quick Sort, and Merge Sort) to sort ArrayList of objects using Comaprable interface. (60 points)
Please write a java program that has the following methods in it: (preferably in order)   a...
Please write a java program that has the following methods in it: (preferably in order)   a method to read in the name of a University and pass it back a method to read in the number of students enrolled and pass it back a method to calculate the tuition as 20000 times the number of students and pass it back a method print the name of the University, the number of students enrolled, and the total tuition Design Notes: The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT