If you are using Java 5.0 (1.5) or above rewrite the Student class to use generics with ArrayList and List.
What will be an ideal response?
```
import j a v a . i o . ? ;
import j a v a . u t i l . ? ;
/? ?
? C l a s s t h a t d e s c r i b e s a s t u d e n t . A s t u d e n t has a name and an
? l i s t o f g r a d e s . You can g e t i n f o r m a t i o n a b o u t a s t u d e n t
? such as h e r / h i s name and g r a d e a v e r a g e . A s t u d e n t may
? a l s o have a p i c t u r e .
?
? @author Barb E r i c s o n
?/
public c l a s s Student
{
// ////////// f i e l d s //////////////////
/? ? t h e name o f t h e s t u d e n t : f i r s t l a s t ?/
private S t r i n g name ;
/? ? a l i s t o f g r a d e s f o r t h i s s t u d e n t ?/
private L i s t
/? ? a p i c t u r e o f t h i s s t u d e n t ?/
private P i c t u r e p i c t u r e ;
// ////////// c o n s t r u c t o r s ///////////
/? ?
? No argument c o n s t r u c t o r . Leave s
? a l l f i e l d s with d e f a u l t values
?/
public Student ( ) {}
/? ?
? C o n s t r u c t o r t h a t t a k e s t h e name
? @param theName t h e s t u d e n t ’ s name
?/
public Student ( S t r i n g theName )
{
t h i s . name = theName ;
}
/? ?
? C o n s t r u c t o r t h a t t a k e s a name and
? t h e number o f g r a d e s
? @param theName t h e s t u d e n t ’ s name
? @param numGrades t h e number o f g r a d e s
?/
public Student ( S t r i n g theName , i n t numGrades )
{
t h i s . name = name ;
t h i s . g r a d e L i s t = new A r r a y L i s t ( numGrades ) ;
}
/? ?
? C o n s t r u c t o r t h a t t a k e s t h e name
? and a l i s t o f g r a d e s
? @param theName t h e s t u d e n t ’ s name
? @param t h e G r a d e L i s t t h e l i s t o f g r a d e s
?/
public Student ( S t r i n g theName ,
L i s t
{
t h i s . name = theName ;
this . gradeList = theGradeList ;
}
/? ?
? Constructor that takes a delimited string , the
? name d e l i m i t e r , and t h e g r a d e d e l i m i t e r .It f i l l s
? i n t h e f i e l d s from t h e d e l i m i t e d s t r i n g .
? @param d e l i m S t r i n g s t u d e n t i n f o r m a t i o n as a
? delimited string
? @param nameDelim what d e l i m i t s t h e name f i e l d
? from t h e g r a d e s
? @param gradeDelim what d e l i m i t s t h e g r a d e s
?/
public Student ( S t r i n g d e l i m S t r i n g ,
S t r i n g nameDelim ,
S t r i n g gradeDelim )
{
// c r e a t e t h e l i s t f o r t h e g r a d e s , i f needed
i f ( t h i s . g r a d e L i s t == null )
t h i s . g r a d e L i s t = new A r r a y L i s t ( ) ;
// s p l i t s t r i n g b a s e d on name d e l i m i t e r
S t r i n g [ ] s p l i t A r r a y = d e l i m S t r i n g . s p l i t ( nameDelim ) ;
t h i s . name = s p l i t A r r a y [ 0 ] . t r i m ( ) ;
// g e t t h e g r a d e s t r i n g and b r e a k i t and c o n v e r t t o d o u b l e
String grades = splitArray [ 1 ] ;
S t r i n g [ ] g r a d e S t r A r r a y = null ;
i f ( g r a d e s != null )
{
g r a d e S t r A r r a y = g r a d e s . s p l i t ( gradeDelim ) ;
f o r ( i nt i = 0 ; i < g r a d e S t r A r r a y . l e n g t h ; i ++)
{
t h i s . g r a d e L i s t . add (
Double . p a r s e D o u b l e ( g r a d e S t r A r r a y [ i ] ) ) ;
}
}
}
// ///////// methods ///////////////
/? ?
? Method t o g e t t h e p i c t u r e f o r t h i s
? student
? @return t h e p i c t u r e (may be n u l l )
?/
public P i c t u r e g e t P i c t u r e ( )
{ return t h i s . p i c t u r e ; }
/? ?
? Method t o s e t t h e p i c t u r e f o r t h i s
? student
? @param t h e P i c t u r e t h e p i c t u r e t o u s e
?/
public void s e t P i c t u r e ( P i c t u r e t h e P i c t u r e )
{
this . p i c t u r e = thePicture ;
}
/? ?
? Method t o show t h i s s t u d e n t
? It w i l l display the picture i f there
? i s one e l s e j u s t p r i n t t h e s t u d e n t name
?/
public void show ( )
{
i f ( t h i s . p i c t u r e == null )
{
// u s e t o S t r i n g
System . out . p r i n t l n ( t h i s ) ;
}
else
{
t h i s . p i c t u r e . show ( ) ;
}
}
/? ?
? Method t o r e t u r n t h e name o f t h i s s t u d e n t
? @return t h e s t u d e n t ’ s name
?/
public S t r i n g getName ( ) { return t h i s . name ; }
/? ?
? Method t o s e t t h e name f o r t h i s s t u d e n t
? @param theName t h e new name t o u s e
? @return t r u e i f s u c c e s s e l s e f a l s e
?/
public boolean setName ( S t r i n g theName )
{
boolean r e s u l t = f a l s e ;
i f ( t h i s . name == null )
{
t h i s . name = theName ;
r e s u l t = true ;
}
return r e s u l t ;
}
/? ?
? Method t o g e t t h e g r a d e i n t h e g r a d e l i s t
? at the passed index
? @param i n d e x t h e i n d e x t h a t we want t h e g r a d e f o r
? @return t h e g r a d e i n t h e g r a d e l i s t a t t h i s p a s s e d i n d e x
?/
public double getGrade ( in t i n d e x )
{
return t h i s . g r a d e L i s t . g e t ( i n d e x ) ;
}
/? ?
? Method t o r e p l a c e t h e l i s t o f g r a d e s
? @param t h e A r r a y t h e new l i s t o f g r a d e s t o u s e
? @return t r u e i f s u c e s s , e l s e f a l s e
?/
public boolean setGradeArray ( L i s t
{
boolean r e s u l t = f a l s e ;
// o n l y s e t t h e g r a d e L i s t i f
i f ( t h i s . g r a d e L i s t == null )
{
this . gradeList = t h e L i s t ;
r e s u l t = true ;
}
return r e s u l t ;
}
/? ?
? Method t o s e t a g r a d e a t an i n d e x
? @param i n d e x t h e i n d e x t o s e t i t a t
? @param newGrade t h e g r a d e t o u s e
? @return t r u e i f s u c c e s s e l s e f a l s e
?/
public boolean s e t G r a d e ( in t index ,
double newGrade )
{
boolean r e s u l t = f a l s e ;
i f ( newGrade >= 0 | |
t h i s . g r a d e L i s t != null | |
index < this . gradeList . s i z e ( ) | |
i n d e x >= 0 ) {
t h i s . g r a d e L i s t . s e t ( index , newGrade ) ;
r e s u l t = true ;
}
return r e s u l t ;
}
/? ?
? Method t o r e t u r n t h e a v e r a g e o f t h e g r a d e s f o r t h i s s t u d e n t
? @return t h e a v e r a g e o f t h e g r a d e s or 0 . 0 i f no g r a d e l i s t or
? no g r a d e s
?/
public double g e t A v e r a g e ( )
{
double a v e r a g e = 0 . 0 ;
i f ( t h i s . g r a d e L i s t != null && t h i s . g r a d e L i s t . s i z e ( ) > 0 )
{
double sum = 0 . 0 ;
f o r ( i nt i = 0 ; i < t h i s . g r a d e L i s t . s i z e ( ) ; i ++)
{
sum = sum + t h i s . g r a d e L i s t . g e t ( i ) ;
}
a v e r a g e = sum / t h i s . g r a d e L i s t . s i z e ( ) ;
}
return a v e r a g e ;
}
/? ?
? Method t o r e t u r n t h e a v e r a g e o f t h e g r a d e s a f t e r you drop
? the l o w e s t grade f o r t h i s student
? @return t h e a v e r a g e o f t h e g r a d e s or 0 . 0 i f no g r a d e l i s t or
? no g r a d e s
?/
public double getAverageDropLowest ( )
{
double a v e r a g e = 0 . 0 ;
double l o w e s t = 1 0 0 ;
i n t numGrades = t h i s . g r a d e L i s t . s i z e ( ) ;
i f ( t h i s . g r a d e L i s t != null && numGrades > 0 )
{
double sum = 0 . 0 ;
// l o o p t h r o u g h t h e g r a d e s
f o r ( i nt i = 0 ; i < t h i s . g r a d e L i s t . s i z e ( ) ; i ++)
{
// c h e c k i f t h i s i s l o w e r
i f ( this . gradeList . get ( i ) < lowest )
{
lowest = this . gradeList . get ( i ) ;
}
// add t h i s g r a d e t o t h e sum
sum = sum + t h i s . g r a d e L i s t . g e t ( i ) ;
}
// i f t h e r e was more tha n one g r a d e remove t h e l o w e s t
i f ( numGrades > 1 )
{
sum = sum ? l o w e s t ;
numGrades??;
}
a v e r a g e = sum / numGrades ;
}
return a v e r a g e ;
}
/? ?
? Method t o r e t u r n a s t r i n g w i t h i n f o r m a t i o n a b o u t t h i s s t u d e n t
? @return a s t r i n g w i t h i n f o r m a t i o n a b o u t t h e s t u d e n t
?/
public S t r i n g t o S t r i n g ( ) {
return ” Student o b j e c t named : ” + t h i s . name +
” Average : ” + t h i s . g e t A v e r a g e ( ) ;
}
/? Used t o t e s t ?/
// p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
//
//
//
//
//
//
//
//
//
//
//
{
Making Text for the Web
S t u d e n t s t u d e n t 1 = new S t u d e n t (” Barb E r i c s o n ” ) ;
System . o u t . p r i n t l n ( s t u d e n t 1 ) ;
d o u b l e [ ] gradeArray1 = { 9 0 , 8 8 , 9 5 , 9 6 , 9 3 } ;
S t u d e n t s t u d e n t 2 = new S t u d e n t (” Mark G u z d i a l ” , gradeArray1 ) ;
System . o u t . p r i n t l n ( s t u d e n t 2 ) ;
P i c t u r e b a r b = new P i c t u r e ( F i l e C h o o s e r . getMediaPath (” b a r b a r a . j p g ” ) ) ;
student1 . s e t P i c t u r e ( barb ) ;
s t u d e n t 1 . show ( ) ;
s t u d e n t 2 . show ( ) ;
}
/? ?
? Method t o compare t h e c u r r e n t s t u d e n t
? with a passed in o b j e c t
? @param o t h e o b j e c t p a s s e d i n
? @return n e g a t i v e i f t h e c u r r e n t s t u d e n t
? i s l e s s tha n t h e p a s s e d o b j e c t
? 0 i f they are equal
? Positive i f the current student i s
? g r e a t e r th an t h e p a s s e d i n o b j e c t
? Warning t h i s w i l l s a y two s t u d e n t s
? w i t h t h e same name a r e e q u a l !
?/
public i n t compareTo ( Ob jec t o )
{
Student t e s t S t u d e n t = ( Student ) o ;
return t h i s . name . compareTo ( t e s t S t u d e n t . name ) ;
}
/? Used t o t e s t ?/
public s t a t i c void main ( S t r i n g [ ] a r g s )
{
// t e s t t h e c o n s t r u c t o r t h a t t a k e s a d e l i m i t e d s t r i n g
Student s t u d e n t 3 =
new Student ( ” Susan E r i c s o n : 5 0 , 6 0 , 7 0 , 8 0 , 9 0 , 1 0 0 ” , ” : ” , ” , ” ) ;
System . out . p r i n t l n ( s t u d e n t 3 ) ;
System . out . p r i n t l n ( s t u d e n t 3 . getAverageDropLowest ( ) ) ;
}
}
```
You might also like to view...
Which of the following is used to insert images into a world?
a. Billboard b. Dummy c. Pose d. Vehicle e. None of these
Match the following terms with their description
I. LinkedIn II. Connection III. Twitter IV. Tweet V. Retweet A. The people with whom you have an online relationship in LinkedIn B. A microblogging platform for social networking C. Sharing another user's tweet D. A professional social networking site that can connect people with opportunities E. Short messages limited to 140 characters and spaces
What type of content control would you use for simple numeric calculations?
A) Formula B) Smart C) Number D) Text
Which control allows you to choose from a list as well as make an entry from the keyboard?
A. Label B. List box C. Combo box D. Command box