Aidez moi à corriger l'erreur

Fermé
dadi - Modifié par KX le 7/04/2016 à 19:42
KX Messages postés 16668 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 17 mars 2023 - 7 avril 2016 à 19:46
Bonjour,
je souhaiterai votre aide car je suis planqué
voici mon code

package coordCart;

public class LignePol {
 private Point[] sommets;

 public LignePol(int n) {
        Point[] sommets = new Point[n];
    }
    public LignePol(Point[] sommets) {
        this.sommets = sommets; 
    }
    
    public Point getSommet(int i) {
        return sommets[i];
    }
    public void setSommet(int i, Point p) {
        sommets[i] = p;
    }
    
    public String toString() {
        if (sommets.length == 0)
            return "[ ]";
    
        String res = "[ " + sommets[0];
        for (int i = 1; i < sommets.length; i++)
            res += ", " + sommets[i];
        return res + " ]";
    }
    
    public Object clone() {
        Point[] bis = new Point[sommets.length]; 
        for (int i = 0; i < sommets.length; i++) {
            Point p = sommets[i];
            bis[i] = new Point(p.x(), p.y());
        }   
        return new LignePol(bis);
    }

    public void homothetie(double k) {
        for (int i = 0; i < sommets.length; i++)
            sommets[i].homothetie(k);
    }
    public void translation(double dx, double dy) {
        for (int i = 0; i < sommets.length; i++)
            sommets[i].translation(dx, dy);
    }
    public void rotation(double a) {    
        for (int i = 0; i < sommets.length; i++)
            sommets[i].rotation(a);
    }
    
    void tracer() {
        for (int i = 1; i < sommets.length; i++)
            tracerSegment((int) sommets[i - 1].x(), (int) sommets[i - 1].y(), 
                    (int) sommets[i].x(), (int) sommets[i].y());
    }
    
    public static void main(String[] args) {
        Point[] t = { 
                new Point( 1, 3), new Point( 0, 2), new Point( 0, 0),
                new Point( 3, 5), new Point( 4, 4), new Point( 0, 4),
                new Point( 4, 2), new Point( 4, 0), new Point( 1, 1) };
        LignePol lp = new LignePol(t);

        double m = Double.parseDouble(args[0]); 
        double n = Double.parseDouble(args[1]); 
        double l = Double.parseDouble(args[1]); 
       
        lp.homothetie(l / 4.0);
        lp.translation(m, n);
        
        lp.tracer();
    }

    // pour la simulation
    static void tracerSegment(int x0, int y0, int x1, int y1) {
        System.out.println("(" + x0 + "," + y0 + ") --> (" + x1 + "," + y1 + ")");
    }


}

l’exécution du programme me donne le message suivant:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at coordCart.LignePol.main(LignePol.java:65)

Aidez moi.

1 réponse

KX Messages postés 16668 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 17 mars 2023 3 005
7 avril 2016 à 19:46
Bonjour,

Ton programme prends trois arguments (m, n et l) dans
String[] args

Il faut donc démarrer ton programme avec des valeurs pour chaque.

Exemple :
java coordCart.LignePol 1 2 3

Puisque toi tu n'as rien mis (uniquement
java coordCart.LignePol
) alors args est vide, la case args[0] n'existe pas, d'où l'erreur sur la ligne 65
double m = Double.parseDouble(args[0]);
0