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...
Answer the following questions true (T) or false (F)
1. When using the BorderLayout manager you must specify all five regions. 2. One of the main functions of JPanel objects is to subdivide a JFrame (or other container) into different areas.
Show the average cost of a course with no prerequisites (1195)
What will be an ideal response?
Describe two advantages and two disadvantages of an anomaly-based system.
What will be an ideal response?
What is the difference between an FD and an RD in EIGRP?
A) An FD is reported by a neighbor, and an RD is the calculated route on the local EIGRP router. B) An RD is reported by a neighbor, and an FD is the calculated route on the local EIGRP router. C) An FD is a networkwide metric, and an RD is a metric local to the router. D) An RD is a networkwide metric, and an FD is a metric local to the router.