Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente Prochaine révision Les deux révisions suivantes | ||
ens:dasi2014:juint [2014/04/02 22:58] admin Cordier Amelie () [Partie 2. Exercices simples] |
ens:dasi2014:juint [2014/04/03 11:01] admin Cordier Amelie () |
||
---|---|---|---|
Ligne 42: | Ligne 42: | ||
<code java BinString.java> | <code java BinString.java> | ||
- | // A C l a s s t h a t a d d s up a s t r i n g b a s e d on t h e A S C I I v a l u e s o f i t s | ||
- | // c h a r a c t e r s and t h e n r e t u r n s t h e b i n a r y r e p r e s e n t a t i o n o f t h e sum . | ||
public class BinString { | public class BinString { | ||
- | p u b l i c B i n S t r i n g ( ) {} | + | public BinString() {} |
- | public | + | |
- | | + | |
- | r e t u r n b i n a r i s e ( sum ( s ) ) ; | + | |
- | } | + | } |
- | public | + | |
- | } | + | |
- | i n t sum ( S t r i n g s ) { | + | |
- | i f ( s =="" | + | |
- | i f ( s . l e n g t h ()==1) | + | |
- | r e t u r n ( ( i n t ) ( s . c h a r A t ( 0 ) ) ) + sum ( s . s u b s t r i n g ( 1 ) ) ; | + | } |
- | } | + | |
- | public | + | |
- | } | + | |
- | String binarise ( int x) { | + | |
- | i f ( x ==0) r e t u r n " " ; | + | |
- | i f ( x %2==1) | + | } |
- | r e t u r n " | + | } |
</ | </ | ||
Ligne 68: | Ligne 66: | ||
<code java BinStringTest.java> | <code java BinStringTest.java> | ||
- | import | + | import |
- | p u b l i c c l a s s B i n S t r i n g T e s t e x t e n d s TestCase { | + | public class BinStringTest extends |
- | private BinString binString ; | + | |
- | public | + | |
- | B i n S t r i n g T e s t ( S t r i n g name ) { | + | |
- | s u p e r ( name ) ; | + | |
- | } | + | } |
- | p r o t e c t e d v o i d setUp () { | + | |
- | b i n S t r i n g = new B i n S t r i n g ( ) ; | + | protected void setUp(){ |
- | } | + | |
- | public | + | } |
- | void testSumFunction () { | + | |
- | i n t expected = 0; | + | |
- | a s s e r t E q u a l s ( e x p e c t e d , b i n S t r i n g . sum ( " " ) ) ; | + | |
- | expected = 100; | + | |
- | a s s e r t E q u a l s ( e x p e c t e d , b i n S t r i n g . sum ( " d " ) ) ; | + | expected = 100; |
- | expected = 265; | + | |
- | a s s e r t E q u a l s ( expected , | + | expected = 265; |
- | b i n S t r i n g . sum ( " Add " ) ) ; | + | |
- | public | + | } |
- | void testBinariseFunction () { | + | |
- | S t r i n g expected = " | + | public |
- | a s s e r t E q u a l s ( expected , b i n S t r i n g . b i n a r i s e ( 5 ) ) ; | + | |
- | expected = " | + | |
- | a s s e r t E q u a l s ( expected , b i n S t r i n g . b i n a r i s e (252)); | + | expected = " |
- | } | + | |
- | } | + | } |
- | public | + | |
- | } | + | public void testTotalConversion() { |
- | void testTotalConversion () { | + | |
- | S t r i n g expected = "1000001"; | + | |
- | a s s e r t E q u a l s ( e x p e c t e d , b i n S t r i n g . c o n v e r t ( "A " ) ) ; | + | } |
+ | } | ||
</ | </ | ||