Create a class PictureBook that is like the class PhoneBook except that it maps a person’s name to his or her picture.
What will be an ideal response?
```
import j a v a . u t i l . ? ;
import j a v a . i o . ? ;
/? ?
? A c l a s s t h a t r e p r e s e n t s a p i c t u r e book . This p i c t u r e
? book maps names t o p i c t u r e s .
?/
public c l a s s Pi c t u r e B o o k
{
// ///////////////// f i e l d s /////////////////////////
/? ? t h e f i l e name t o r e a d from ?/
private S t r i n g f i l e N a m e ;
/? ? t h e book map t o s t o r e t h e d a t a ?/
private Map bookMap =
new HashMap ( ) ;
// //////////////// 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 s a f i l e name and r e a d s
? i n t h e names and p i c t u r e f i l e s from a f i l e
? @param f i l e t h e name o f t h e f i l e t o r e a d
?/
public P i c t u r e B o o k ( S t r i n g f i l e )
{
this . fileName = f i l e ;
// r e a d t h e map i n f o r m a t i o n i n from t h e f i l e
readInfoFromFile ( ) ;
}
// ///////////////// methods /////////////////////
/? ?
? Get t h e p i c t u r e f o r t h e p a s s e d name
? @param name t h e name t o l o o k up i n t h e map
? @return t h e p i c t u r e i f found
?/
public P i c t u r e g e t P i c t u r e ( S t r i n g name )
{
P i c t u r e p i c t = null ;
S t r i n g p i c t F i l e = bookMap . g e t ( name ) ;
i f ( p i c t F i l e != null )
p i c t = new P i c t u r e ( F i l e C h o o s e r . getMediaPath ( p i c t F i l e ) ) ;
return p i c t ;
}
/? ?
? Method t o r e a d t h e p i c t u r e book i n f o r m a t i o n from a
? f i l e and u s e i t t o f i l l t h e map
?/
public void r e a d I n f o F r o m F i l e ( )
{
Making Text for the Web
S t r i n g l i n e = null ;
S t r i n g [ ] bookArray = null ;
try {
// c r e a t e t h e r e a d e r
BufferedReader reader =
new B u f f e r e d R e a d e r (new F i l e R e a d e r ( f i l e N a m e ) ) ;
// l o o p r e a d i n g from t h e f i l e
while ( ( l i n e = r e a d e r . r e a d L i n e ( ) ) != null )
{
i f ( l i n e . indexOf ( ” : ” ) >= 0 )
{
bookArray = l i n e . s p l i t ( ” : ” ) ;
bookMap . put ( bookArray [ 0 ] . t r i m ( ) ,
bookArray [ 1 ] . t r i m ( ) ) ;
}
}
// c l o s e t h e r e a d e r
reader . close ( ) ;
} catch ( FileNotFoundException ex ) {
SimpleOutput . showError ( ” Couldn ’ t f i n d f i l e ” + f i l e N a m e ) ;
} catch ( E x c e p t i o n ex ) {
ex . p r i n t S t a c k T r a c e ( ) ;
}
}
/? ?
? Method t o p r i n t o u t t h e c o n t e n t s o f t h e p i c t u r e book
?/
public void p r i n t B o o k ( )
{
// g e t t h e s e t o f k e y s
S e t k e y S e t = bookMap . k e y S e t ( ) ;
S t r i n g key = null ;
// l o o p t h r o u g h t h e k e y s
I t e r a t o r i t e r a t o r = keySet . i t e r a t o r ( ) ;
while ( i t e r a t o r . hasNext ( ) )
{
key = ( S t r i n g ) i t e r a t o r . next ( ) ;
System . out . p r i n t l n ( ”Name : ” + key +
” , Picture f i l e : ” +
bookMap . g e t ( key ) ) ;
}
}
/? main 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 )
{
P i c t u r e B o o k book =
new P i c t u r e B o o k ( F i l e C h o o s e r . getMediaPath ( ” b a r b s P i c t u r e B o o k . t x t ” ) ) ;
book . p r i n t B o o k ( ) ;
P i c t u r e p = book . g e t P i c t u r e ( ”Tammy” ) ;
p . show ( ) ;
p = book . g e t P i c t u r e ( ”Matt” ) ;
p . show ( ) ;
}
}
```
You might also like to view...
Options to change the style of an Action Button, such as its fill and outline color, are located on the ________ tab in the Shape Styles group
A) Insert B) Design C) Review D) Format
What is the output of the following code?
``` double[] myList = {1, 5, 5, 5, 5, 1}; double max = myList[0]; int indexOfMax = 0; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) { max = myList[i]; indexOfMax = i; } } System.out.println(indexOfMax);``` a. 0 b. 1 c. 2 d. 3 e. 4