Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with the following code requirement: User Story When a user initiates the Scramble program, the user is prompted to supply a 5-letter word.

Need help with the following code requirement:

User Story

When a user initiates the Scramble program, the user is prompted to supply a 5-letter word. If the word is

not 5 letters (no more, no less), then an error message is presented, and the program terminates. Here is a

sample of the input and output of the program. Please follow it exactly.

Enter 5-letter word: croak

Scrambled word: AOKRC

Fake word: KAROC (note that this is only if you want extra credit)

Tasks

1.

Create a Project called Scramble (File > New Project, Java/Java Application, Next, Finish)

2.

Present the prompt and read the word from input.

3.

Verify that the word is exactly 5 characters and if it isnt do no more processing. A quick way to

terminate a Java program is to use the command:

System.exit(1);

4.

Convert the word to upper case for processing and output.

5.

Scramble the word by doing the following:

a.

Declare the scrambled word and set it to the empty string:

String scram_word = ; (dont copy this as the quotes are wrong)

b.

Randomly generate a number between 0 and 4 (inclusive). The following is a hint and is only

part of a valid Java statement.

CMPSCI 111 Lab

Project 3 - Scramble

Summer Session 2 2018

2

Math.random() * 5

c.

Remove that character from the input word and add it to the scrambled word.

d.

Randomly generate a number between 0 and 3 (inclusive).

e.

Remove that character from the input word and add it to the scrambled word.

f.

And so on.

Suggestions

Build a section of code that moves a random character from the given word to a scrambled word.

Once you have that working, youll just copy that code three times.

Youll only need to get a random letter four times. When there is only one character left, just throw

that on the end of the scrambled word.

Recommend you have the following variables declared:

o

String word (input word)

o

String scram_word (scrambled word)

o

int i (gets the random number that will be an index into the string)

o

char c (the character being moved from the input word to the scrambled word)

Extracting a character from a string is done by getting the substring from the start of the string up to

the character and the substring after the character. Therefore you should see something in your code

like the following to extract a character from a string. Note that this is not Java but just guidance on

what how to implement the extraction:

o

Before = substring up to the random character being extracted

o

After = substring after the random character being extracted

o

Word = Before + After

Recommend you play with String.substring() in your Test project and understand exactly how it

works before trying to implement it in your Scramble program.

Checklist

Use the following as guidance for getting a 10 on this project:

Has comment header per the guidelines

Indentation is consistent with guidelines (easy as Source > Format)

Variables named in a manner consistent with the conventions

Program behaves exactly as specified (prompts and output are consistent with what was required)

Program does the appropriate checks as specified

Extra Credit (2 points)

Try to create a fake word by interleaving consonants with vowels after the word is scrambled. All that is

required is you start the scrambled word with a consonant, followed by a vowel, followed by a consonant,

followed by a vowel. The last letter can be left alone.

This is what I have so far and I am stuck on writting substring and being able to remove the letters from the words:

package scramble;

import java.util.Scanner; public class Scramble {

public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter a Word"); String wordInput = input.next(); int wordLength = wordInput.length(); if (wordLength == 5) { System.out.print(""); } else{ System.out.println("Word must be 5 letters"); System.exit(1); } } }

Please help. Thank you.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions