Write a program to read a list of words from a file and then output a new file with the letters scrambled using Random.

What will be an ideal response?


```
import java . u t i l . * ;
import java . i o . * ;
/**
* Clas s t h a t can scramble l e t t e r s
* @author Barb Er icson
* Copyr ight 2006
*/
public clas s Scrambler
{
// //////////// f i e l d s ////////////////////////////
/** random number g ene rator */
private Random randNumGen = new Random( ) ;
// //////////// c o n s t r u c t o r s //////////////////////
/**
* No argument c ons t r u c t o r
*/
public Scrambler ( )
{}
// ///////////// methods /////////////////////////
/**
* Method to scramble the l e t t e r s in the s t r i n g
* @param input the s t r i n g to scramble the l e t t e r s in
* @return the new s t r i n g wi th the l e t t e r s scrambled
*/
public St r ing scramble ( St r ing input )
{
char [ ] inputAr ray = input . toCharArray ( ) ;
Li s t cha rLi s t = new Ar rayLi s t ( ) ;
// move c h a r a c t e r s int o the l i s t
for ( char currChar : inputAr ray )
cha rLi s t . add ( currChar ) ;
// g e t somthing t h a t can i t e r a t e through the l i s t
I t e r a t o r i t e r a t o r = cha rLi s t . i t e r a t o r ( ) ;
int index = 0 ;
char [ ] charArray = new char [ cha rLi s t . s i z e ( ) ] ;
// loop t i l l a l l the l e t t e r s are used
while ( i t e r a t o r . hasNext ( ) )
{
charArray [ index ] = cha rLi s t . remove ( randNumGen . next Int ( cha rLi s t . s i z e ( ) ) ) ;
index++;
}
return new St r ing ( charArray ) ;
}
/**
* Method to scramble the l e t t e r s in each l i n e of a f i l e
* @param i n p u tFi l e the path f o r the input f i l e
* @param o u t p u tFi l e the path f o r the output f i l e
*/
public void s c r ambl eFi l e ( St r ing inputFi l e , St r ing outputFi l e )
{
St r ing l i n e = nul l ;
St r ing scrambledLine = nul l ;
// t r y to do the f o l l owi n g
try {
// c r e a t e the b u f f e r e d r eade r
Buf feredReader r eade r =
new Buf feredReader (new Fi l eReade r ( i n p u tFi l e ) ) ;
// t r y to open the b u f f e r e d wr i t e r
Buf f e r edWr i t e r wr i t e r =
new Buf f e r edWr i t e r (new Fi l eWr i t e r ( outputFi l e ) ) ;
// Loop wh i l e t h e r e i s more data
while ( ( l i n e = r e ade r . r eadLine ( ) ) != nul l )
{
// scramble the l i n e
scrambledLine = scramble ( l i n e ) ;
// wr i t e out the scrambled l i n e
wr i t e r . wr i t e ( scrambledLine ) ;
wr i t e r . newLine ( ) ;
}
// c l o s e the r eade r and wr i t e r
r eade r . c l o s e ( ) ;
wr i t e r . c l o s e ( ) ;
g catch ( Fi leNotFoundException ex ) f
SimpleOutput . showError ( "Couldn ' t f i n d " + i n p u tFi l e +
" p l e a s e pi ck i t . " ) ;
i n p u tFi l e = Fi l eChoos e r . pi ckAFi l e ( ) ;
s c r ambl eFi l e ( inputFi l e , outputFi l e ) ;
g catch ( Except ion ex ) f
SimpleOutput . showError ( "Er ror with f i l e " + i n p u tFi l e ) ;
ex . pr intStackTrac e ( ) ;
}
}
/**
* Main method f o r t e s t i n g
*/
public s tat ic void main ( St r ing [ ] args )
{
Scrambler s c rambl e r = new Scrambler ( ) ;
s c rambl e r . s c r ambl eFi l e ( " c : / int r o¡prog¡java / bo okCla s s e sFina l / textToScramble . txt " ,
" c : / int r o¡prog¡java / bo okCla s s e sFina l / scrambledText . txt " ) ;
}
}
```

Computer Science & Information Technology

You might also like to view...

When you ________ a video in a presentation, connection is created to another location such as a storage device or website

Fill in the blank(s) with correct word

Computer Science & Information Technology

Match the following Internet terms to their descriptions:

I. Pages A. The computers that store the Web pages that are viewed. II. Servers B. A document intended to serve as the starting point for a Web site. III. Internet Service Provider C. The specified location of a page on the World Wide Web. IV. Home Page D. The individual documents that form a network of linked documents on the Internet. V. Uniform Resource Locator E. A business that sets up large computers that act as servers to other computers.

Computer Science & Information Technology

NIS domains are created to allow for servers and clients to communicate and the servers and clients can be in different domains.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

Match each item with a statement below:

A. a feature that enables users to access resources over the network without having to repeatedly supply their credentials B. protocol that picks one of the available authentication methods for a system C. a traditional authentication method for Windows operating systems using a simple challenge response protocol D. allows fine-grained control over the mapping between access control and tasks performed in the deployment area E. divides authentication into the authentication interface library, and the actual authentication mechanism-specific modules F. a place where a number of services are being offered G. the core authentication class in JAAS H. a framework for authentication and authorization in Internet protocols I. retrieves and makes available NT security information for the current user

Computer Science & Information Technology