Question: Please use JAVA Design and code a new method to be exported from ArrayStringLog called smallest, with the following signature: public String smallest ( )
Please use JAVA
Design and code a new method to be exported from ArrayStringLog called smallest, with the following signature:
public String smallest ( )
The method returns the smallest string in the StringLog. By smallest, we mean in terms of the lexicographic ordering supported by the String classs compareTo method. As a precondition you should assume that the StringLog is not empty.
publ i c i nt e r f a c e St r i ngLogI nt e r f a c e
{
voi d i ns e r t ( St r i ng e l eme nt ) ;
/ / Pr e c ondi t i on: Thi s St r i ngLog i s not f ul l .
/ /
/ / Pl a c e s e l eme nt i nt o t hi s St r i ngLog.
bool e a n i s Ful l ( ) ;
/ / Re t ur ns t r ue i f t hi s St r i ngLog i s f ul l , ot he r wi s e r e t ur ns f a l s e .
i nt s i z e ( ) ;
/ / Re t ur ns t he numbe r of s t r i ngs i n t hi s St r i ngLog.
bool e a n c ont a i ns ( St r i ng e l eme nt ) ;
/ / Re t ur ns t r ue i f e l eme nt i s i n t hi s St r i ngLog,
/ / ot he r wi s e r e t ur ns f a l s e .
/ / I gnor e s c a s e di f f e r e nc e s whe n doi ng a s t r i ng c ompa r i s on.
voi d c l e a r ( ) ;
/ / Ma ke s t hi s St r i ngLog empt y.
St r i ng ge t Name ( ) ;
/ / Re t ur ns t he name of t hi s St r i ngLog.
St r i ng t oSt r i ng( ) ;
/ / Re t ur ns a ni c e l y f or ma t t e d s t r i ng r e pr e s e nt i ng t hi s St r i ngLog.
}
publ i c c l a s s Ar r a ySt r i ngLog i mpl eme nt s St r i ngLogI nt e r f a c e
{
pr ot e c t e d St r i ng name ; / / name of t hi s St r i ngLog
pr ot e c t e d St r i ng[ ] l og; / / a r r a y t ha t hol ds s t r i ngs
pr ot e c t e d i nt l a s t I nde x = - 1; / / i nde x of l a s t s t r i ng i n a r r a y
publ i c Ar r a ySt r i ngLog( St r i ng name , i nt ma xSi z e )
/ / Pr e c ondi t i on: ma xSi z e > 0
/ /
/ / I ns t a nt i a t e s a nd r e t ur ns a r e f e r e nc e t o a n empt y St r i ngLog obj e c t
/ / wi t h name " name " a nd r oom f or ma xSi z e s t r i ngs .
{
l og = new St r i ng[ ma xSi z e ] ;
t hi s . name = name ;
}
publ i c Ar r a ySt r i ngLog( St r i ng name )
/ / I ns t a nt i a t e s a nd r e t ur ns a r e f e r e nc e t o a n empt y St r i ngLog obj e c t
/ / wi t h name " name " a nd r oom f or 100 s t r i ngs .
{
l og = new St r i ng[ 100] ;
t hi s . name = name ;
}
publ i c voi d i ns e r t ( St r i ng e l eme nt )
/ / Pr e c ondi t i on: Thi s St r i ngLog i s not f ul l .
/ /
/ / Pl a c e s e l eme nt i nt o t hi s St r i ngLog.
{
l a s t I nde x++;
l og[ l a s t I nde x] = e l eme nt ;
}
publ i c bool e a n i s Ful l ( )
/ / Re t ur ns t r ue i f t hi s St r i ngLog i s f ul l , ot he r wi s e r e t ur ns f a l s e .
{
i f ( l a s t I nde x == ( l og. l e ngt h - 1) )
r e t ur n t r ue ;
e l s e
r e t ur n f a l s e ;
}
publ i c i nt s i z e ( )
/ / Re t ur ns t he numbe r of s t r i ngs i n t hi s St r i ngLog.
{
r e t ur n ( l a s t I nde x + 1) ;
}
publ i c bool e a n c ont a i ns ( St r i ng e l eme nt )
/ / Re t ur ns t r ue i f e l eme nt i s i n t hi s St r i ngLog,
/ / ot he r wi s e r e t ur ns f a l s e .
/ / I gnor e s c a s e di f f e r e nc e s whe n doi ng s t r i ng c ompa r i s on.
{
i nt l oc a t i on = 0;
whi l e ( l oc a t i on <= l a s t I nde x)
{
i f ( e l eme nt . e qua l s I gnor eCa s e ( l og[ l oc a t i on] ) ) / / i f t he y ma t c h
r e t ur n t r ue ;
e l s e
l oc a t i on++;
}
r e t ur n f a l s e ;
}
publ i c voi d c l e a r ( )
/ / Ma ke s t hi s St r i ngLog empt y.
{
f or ( i nt i = 0; i <= l a s t I nde x; i ++)
l og[ i ] = nul l ;
l a s t I nde x = - 1;
}
publ i c St r i ng ge t Name ( )
/ / Re t ur ns t he name of t hi s St r i ngLog.
{
r e t ur n name ;
}
publ i c St r i ng t oSt r i ng( )
/ / Re t ur ns a ni c e l y f or ma t t e d s t r i ng r e pr e s e nt i ng t hi s St r i ngLog.
{
St r i ng l ogSt r i ng = " Log: " + name + " \ n\ n" ;
f or ( i nt i = 0; i <= l a s t I nde x; i ++)
l ogSt r i ng = l ogSt r i ng + ( i +1) + " . " + l og[ i ] + " \ n" ;
r e t ur n l ogSt r i ng;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
