You’ve seen code to mirror pictures in Program 19, and you’ve seen code to mirror sounds in Program 79. It should be pretty easy to use the same algorithm to mirror text. Write a function to take a string, then return the mirrored string with the front mirrored to the back.

What will be an ideal response?


```
/? ?
? C l a s s t h a t has methods t h a t o p e r a t e on a s t r i n g
? @author Barb E r i c s o n e r i c s o n @ c c . g a t e c h . edu
? C o p y r i g h t 2006
?/
public c l a s s StringWorker
{
// ////////// f i e l d s /////////////////////
/? ? s t r i n g t o work on ?/
private S t r i n g a S t r i n g ;
// //////////// c o n s t r u c t o r s /////////////
/? ?
? C o n s t r u c t o r t h a t t a k e t h e s t r i n g t o work w i t h
? @param t h e S t r i n g t h e s t r i n g t o work w i t h
?/
public StringWorker ( S t r i n g t h e S t r i n g )
{
this . aString = theString ;
}
// ////////////// methods //////////////////
/? ?
? Method t o m i r r o r f r o n t o f t h e s t r i n g t o t h e
? back
? @return t h e new s t r i n g t h a t i s m i r r o r e d
?/
public S t r i n g m i r r o r ( )
{
char [ ] charArray = t h i s . a S t r i n g . toCharArray ( ) ;
i n t m i r r o r P o i n t = charArray . l e n g t h ;
i n t l a s t I n d e x = charArray . l e n g t h ? 1 ;
char theChar = charArray [ 0 ] ;
// l o o p up t o m i r r o r p o i n t
f o r ( i n t i = 0 ; i < m i r r o r P o i n t ; i ++)
{
theChar = charArray [ i ] ;
charArray [ l a s t I n d e x ? i ] = theChar ;
}
return new S t r i n g ( charArray ) ;
}
/? ?
? Main method f o r t e s t i n g
?/
public s t a t i c void main ( S t r i n g [ ] a r g s )
{
Creating and Modifying Text
// t r y a s t r i n g w i t h an even number o f c h a r a c t e r s
StringWorker worker = new StringWorker ( ” This i s a t e s t ” ) ;
S t r i n g mString = worker . m i r r o r ( ) ;
System . out . p r i n t l n ( mString ) ;
// t r y one w i t h an odd number
worker = new StringWorker ( ”Try an odd number o f c h a r a c t e r s ” ) ;
mString = worker . m i r r o r ( ) ;
System . out . p r i n t l n ( mString ) ;
}
}
```

Computer Science & Information Technology

You might also like to view...

Arrays static method ________ fills an array with values produced by a generator function that receives an int and returns a value of type int, long or double.

a. parallelFill b. parallelFillAll c. generateAll d. parallelSetAll

Computer Science & Information Technology

What is the difference between tasks and calendar events?

What will be an ideal response?

Computer Science & Information Technology

The y-axis of a chart is also referred to as the value axis

Indicate whether the statement is true or false

Computer Science & Information Technology

A(n) ____________________ is a description of a task that is performed in response to a message.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology