Programmation Java

Fermé
Medd12 Messages postés 32 Date d'inscription dimanche 6 avril 2014 Statut Membre Dernière intervention 13 février 2018 - Modifié par KX le 22/10/2016 à 17:54
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 - 22 oct. 2016 à 17:57
bonjour,

Je débute en java, je suis en train de faire un exercice pour apprendre un peu la notion d'héritage mais je bloque.
Je possède une classe Point, une classe Polygone, une classe Triangle, une classe Rectangle, une classe PolygoneRegulier, et une classe TestPolygone avec une méthode main pour tester tout ça.

Voici les classes et les constructeurs (j'ai réussi à faire toute les classes), mais je bloque en classe Test :
class point {
    private int x;
    private int y;

    public point() {
        this.x = 0;
        this.y = 0;
    }

    public point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getx() {
        return this.x;
    }

    public int gety() {
        return this.y;
    }

    public void setx(int x) {
        this.x = x;
    }

    public void sety(int y) {
        this.y = y;
    }

    public void affichage() {
        System.out.println("(" + getx() + "," + gety() + ")");
    }

    public double distance(point p) {
        return Math.sqrt(((this.getx() - p.getx()) * (this.getx() - p.getx()) + (Math.pow(this.gety() - p.gety(), (2)))));
    }
}

public class polygone {
    private point[] tab;

    public polygone(point[] tab) {
        this.tab = tab;
    }

    public void affiche() {
        System.out.println("is s'agit d'un polygone");
        for (int i = 0; i < tab.length; i++) {
            tab[i].affichage();
        }
    }

    public int nbsommets() {
        return tab.length;
    }

    public double perimetre() {
        double p = 0;
        for (int i = 0; i < tab.length - 1; i++) {
            p = p + tab[i].distance(tab[i + 1]);
        }
        p = p + tab[0].distance(tab[tab.length - 1]);
        return p;
    }
}

public class rectangle {
    private point[] tab;

    public rectangle(point p1, point p2) {

        tab[0] = p1;
        tab[1] = new point(p2.getx(), p1.gety());
        tab[2] = p2;
        tab[3] = new point(p2.gety(), p1.getx());

    }

    public rectangle(point p, int l, int L) {
        this(p, new point(p.getx() - L, p.gety() - l));

    }

    public void affiche() {
        System.out.println("is s'agit d'un rectangle ");
        for (int i = 0; i < tab.length; i++) {
            tab[i].affichage();
        }
    }

    public int nbsommets() {
        return tab.length;
    }

    public double perimetre() {
        double p = 0;
        for (int i = 0; i < tab.length - 1; i++) {
            p = p + tab[i].distance(tab[i + 1]);
        }
        p = p + tab[0].distance(tab[tab.length - 1]);
        return p;
    }
}

public class carre {
    private point[] tab;

    public carre(point p, int ct) {
         this (p,new point (p.getx()+ct,p.gety()+ct));
    }

    public void affiche() {
        System.out.println("is s'agit d'un carre ");
        for (int i = 0; i < tab.length; i++) {
            tab[i].affichage();
        }
    }

    public int nbsommets() {
        return tab.length;
    }

    public double perimetre() {
        double p = 0;
        for (int i = 0; i < tab.length - 1; i++) {
            p = p + tab[i].distance(tab[i + 1]);
        }
        p = p + tab[0].distance(tab[tab.length - 1]);
        return p;
    }
}
A voir également:

1 réponse

KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
22 oct. 2016 à 17:57
Bonjour,

Tu parles d'héritage mais tu n'en fait pas...

Quant à l'utilisation de this en tant que constructeur (ce qui n'a rien à voir avec de l'héritage), cela implique que tu ais un deuxième constructeur, or dans la classe
carre
tu n'en as qu'un (contrairement à la classe
rectangle
qui en a bien deux)

Mais peut-être devrais-tu plutôt utiliser
super
si tu veux faire de l'héritage... sous réserve que tes classes en
extends
une autre.
0