Projet de programmation (IK3) : TP n 1 Correction Semaine du 20 septembre 2010 1 Entrées/sorties, types de bases et structures de contrôle Tests et types de bases Tests et types de bases (entiers) public class TestInt static DeugConsole console ; private static void print ( int a, int b, int c ) console. WriteInt ( a ) ; console. WriteInt (b ) ; console. WriteInt ( c ) ; int a, b, c ; console = DeugConsole. console ( ) ; a = console. ReadInt ( ) ; b = console. ReadInt ( ) ; c = console. ReadInt ( ) ; i f ( a < b) i f ( a < c ) i f (b < c ) print (a, b, c ) ; else print (a, c, b ) ; else print ( c, a, b ) ; else i f ( a < c ) print (b, a, c ) ; else i f (b < c ) print (b, c, a ) ; else print ( c, b, a ) ; 1
Tests et types de bases (chaînes) public class TestString static DeugConsole console ; private static void print ( String a, String b, String c ) console. WriteString ( a + " \n" + b + " \n" + " \n" + c ) ; String a, b, c ; console = DeugConsole. console ( ) ; a = console. ReadString ( ) ; b = console. ReadString ( ) ; c = console. ReadString ( ) ; i f ( a. compareto (b) < 0) i f ( a. compareto ( c ) < 0) i f (b. compareto ( c ) < 0) print (a, b, c ) ; else print (a, c, b ) ; else print ( c, a, b ) ; else i f ( a. compareto ( c ) < 0) print (b, a, c ) ; else i f (b. compareto ( c ) < 0) print (b, c, a ) ; else print ( c, b, a ) ; Tableaux et boucles Tableaux et boucles public class Table String [ ] noms ; int [ ] notes ; 2
int nb ; DeugConsole console = DeugConsole. console ( ) ; console. WriteString ( "Number of students? " ) ; nb = console. ReadInt ( ) ; noms = new String [ nb ] ; notes = new int [ nb ] ; for ( int i = 0 ; i < nb ; i++) I n t e g e r n = i + 1 ; noms [ i ] = console. ReadString ( "Name of student number " + n. tostring ( ) + "? " ) ; console. WriteString ( " Student s mark? " ) ; notes [ i ] = console. ReadInt ( ) ; for ( int i = 0 ; i < nb 1 ; i++) for ( int j = 0 ; j < nb i 1 ; j++ ) i f (noms [ j ]. compareto (noms [ j + 1 ] ) > 0) String t s = noms [ j ] ; int t i = notes [ j ] ; noms [ j ] = noms [ j + 1 ] ; notes [ j ] = notes [ j + 1 ] ; noms [ j + 1 ] = t s ; notes [ j + 1 ] = t i ; for ( int i = 0 ; i < nb ; i++) console. WriteString (noms [ i ] + " : " ) ; console. WriteInt ( notes [ i ] ) ; Fonctions récursives Fibonacci, version avec boucle public class FiboBoucle DeugConsole console = DeugConsole. console ( ) ; int n = console. ReadInt ( ) ; int a = 1, b = 1 ; for ( int i = 0 ; i < n ; i++) int c = a + b ; a = b ; b = c ; console. WriteString ( "The r e s u l t i s : " ) ; console. WriteInt ( a ) ; 3
Fibonacci, version récursive public class FiboRecursif public static int f i b o n a c c i ( int n) i f (n <= 1) return 1 ; else return ( f i b o n a c c i (n 1) + f i b o n a c c i (n 2 ) ) ; DeugConsole console = DeugConsole. console ( ) ; int n = console. ReadInt ( ) ; console. WriteString ( "The r e s u l t i s : " ) ; console. WriteInt ( f i b o n a c c i (n ) ) ; Tris On commence par réécrire le code précédant pour qu il utilise plusieurs fonctions. Tris à bulles public class TriABulle private static DeugConsole console ; private static void print ( int nb, String [ ] noms, int [ ] notes ) for ( int i = 0 ; i < nb ; i++) console. WriteString (noms [ i ] + " : " ) ; console. WriteInt ( notes [ i ] ) ; private static void t r i ( int nb, String [ ] noms, int [ ] notes ) for ( int i = 0 ; i < nb 1 ; i++) for ( int j = 0 ; j < nb i 1 ; j++ ) i f (noms [ j ]. compareto (noms [ j + 1 ] ) > 0) String t s = noms [ j ] ; int t i = notes [ j ] ; noms [ j ] = noms [ j + 1 ] ; notes [ j ] = notes [ j + 1 ] ; noms [ j + 1 ] = t s ; notes [ j + 1 ] = t i ; String [ ] noms ; 4
int [ ] notes ; int nb ; console = DeugConsole. console ( ) ; console. WriteString ( "Number of students? " ) ; nb = console. ReadInt ( ) ; noms = new String [ nb ] ; notes = new int [ nb ] ; for ( int i = 0 ; i < nb ; i++) I n t e g e r n = i + 1 ; noms [ i ] = console. ReadString ( "Name of student number " + n. tostring ( ) + "? " ) ; console. WriteString ( " Student s mark? " ) ; notes [ i ] = console. ReadInt ( ) ; t r i (nb, noms, notes ) ; print (nb, noms, notes ) ; Puis j indique seulement les différentes version de la fonction tri. Tris fusion public class TriFusion private static DeugConsole console ; private static void print ( int nb, String [ ] noms, int [ ] notes ) /... / private static void t r i ( int nb, String [ ] noms, int [ ] notes ) for ( int i = 0 ; i < nb 1 ; i++) for ( int j = 0 ; j < nb i 1 ; j++ ) i f (noms [ j ]. compareto (noms [ j + 1 ] ) > 0) String t s = noms [ j ] ; int t i = notes [ j ] ; noms [ j ] = noms [ j + 1 ] ; notes [ j ] = notes [ j + 1 ] ; noms [ j + 1 ] = t s ; notes [ j + 1 ] = t i ; //... t r i (nb, 0, noms, notes ) ; print (nb, noms, notes ) ; 2 Programmation orientée objet et communications réseaux Dérivation de fonctions La fonction de dérivation pourrait être un peu moins naïve. 5
public interface Fonction public double c a l c u l e ( double x ) ; public class Cos implements Fonction Dérivation de fonctions public double c a l c u l e ( double x) return Math. cos ( x ) ; public class Deriv Fonction f ; public Deriv ( Fonction g ) f = g ; static final private double e p s i l o n = 0.00001; public double d( double x) return ( ( f. c a l c u l e ( x+e p s i l o n ) f. c a l c u l e (x e p s i l o n ))/(2 e p s i l o n ) ) ; Serveur echo import deug. DeugSocket ; Client et serveur echo class Client extends DeugSocket Client ( String hostname ) super( hostname ) ; public void run ( ) DeugConsole c = DeugConsole. console ( ) ; String text ; do text = c. ReadString ( " Text to send? " ) ; WriteString ( text ) ; text = ReadString ( ) ; c. WriteString ( " Got : " + text ) ; c. NewLine ( ) ; while (! text. contentequals ( "EOF" ) ) ; class Server extends DeugSocket Server ( ) 6
super ( ) ; public void run ( ) String text ; DeugConsole c = DeugConsole. console ( ) ; do text = ReadString ( ) ; c. WriteString ( " Got : " + text ) ; c. NewLine ( ) ; WriteString ( text ) ; while (! text. contentequals ( "EOF" ) ) ; public class Echo i f ( args. length == 0) Server s = new Server ( ) ; s. run ( ) ; else Client c = new Client ( args [ 0 ] ) ; c. run ( ) ; Client HTTP import deug. DeugSocket ; Client HTTP public class ClientHTTP String u r l ; String hostname, l o c a t i o n ; DeugSocket socket ; DeugConsole c = DeugConsole. console ( ) ; int port = 80; int temp ; boolean page = false ; u r l = c. ReadString ( "URL? " ). trim ( ) ; i f ( u r l. startswith ( " http :// " ) ) u r l = u r l. s u b s t r i n g ( 7 ) ; temp = u r l. indexof ( / ) ; i f ( temp == 1) l o c a t i o n = " / " ; hostname = u r l ; else l o c a t i o n = u r l. s u b s t r i n g ( temp ) ; hostname = u r l. s u b s t r i n g (0, temp ) ; 7
temp = hostname. indexof ( : ) ; i f ( temp!= 1) port = I n t e g e r. parseint ( u r l. s u b s t r i n g ( temp ) ) ; hostname = u r l. s u b s t r i n g (0, temp ) ; socket = new DeugSocket( hostname, port ) ; socket. WriteLine ( "GET " + l o c a t i o n + " HTTP/1.1 " ) ; socket. WriteLine ( " Host : " + hostname ) ; socket. WriteLine ( " Connection : c l o s e " ) ; socket. WriteLine ( " User agent : p a r i s 7 small c l i e n t 1.0 " ) ; socket. NewLine ( ) ; while ( true ) String s = socket. ReadLine ( ) ; i f ( s == null ) System. e x i t ( 0 ) ; i f ( page ) c. WriteString ( s ) ; c. NewLine ( ) ; i f ( s. compareto ( " " ) == 0) page = true ; 8