Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ex2
- public class Tab {
- public static void main(String args[]) {
- int arra_nums[] = {-4, 8,-4,-4,-4,-4, 6, -5, 6, -2, 1, 2, 3, -11};
- System.out.println("tab ori: "+Arrays.toString(arra_nums));
- for( int i = 0, j=arra_nums.length-1 ; i<j ; )
- {
- if( arra_nums[i] < 0 && arra_nums[j] > 0 ){
- int temp = arra_nums[j];
- arra_nums[j] = arra_nums[i];
- arra_nums[i] = temp;
- }
- if( arra_nums[i] > 0 )
- i++;
- if( arra_nums[j] < 0 )
- j--;
- }
- System.out.println("tab: "+Arrays.toString(arra_nums));
- }
- }
- // ex3
- faux // f u
- true
- true
- true
- false
- true
- false
- true
- // ex4
- nbr erreurs = 7
- sans erreurs :
- public class Carre {
- public static void main(String args[]) {
- int tab[] = new int[8];
- String chaine;
- for( int i=0 ; i<tab.length ; i++ )
- System.out.println("tab["+i+"] : "+tab[i]);
- chaine = args[0];
- if ( chaine.equals("fleur") )
- System.out.print("La chaine de longueur " + chaine.length() + " est fleurie");
- else
- System.out.print("La chaine n'est pas fleurie");
- }
- }
- // ex5
- public class Anomalie extends Exception {
- int choix;
- Anomalie(int x){
- choix = x;
- }
- public String toString()
- {
- String str = new String();
- switch(choix) {
- case 0 :
- str = "index > taille";
- break;
- case 1 :
- str = "i > j";
- break;
- case 2 :
- str = "index negative";
- break;
- }
- return str;
- }
- }
- public class Ex5 {
- public static String extraire(String ch, int i, int j) {
- try {
- if( i < 0 || j < 0 )
- throw new Anomalie(2);
- if( i > j )
- throw new Anomalie(1);
- if( j >= ch.length() )
- throw new Anomalie(0);
- } catch ( Anomalie e ) {
- e.printStackTrace();
- System.exit(-1);
- }
- return ch.substring(i, j+1);
- }
- public static void main(String[] args) {
- String str = "abcdef";
- String e = extraire(str, 1, 3);
- System.out.print(e);
- }
- }
Add Comment
Please, Sign In to add comment