Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Moteur {
- // variables d'instance
- private int puissance;
- private char carburant;
- //getter/setter
- public int getPuissance() {
- return puissance;
- }
- public void setPuissance(int puissance) {
- this.puissance = (puissance <= 1000 ? 1000 : puissance);
- }
- public char getCarburant() {
- return carburant;
- }
- public void setCarburant(char carburant) {
- this.carburant = (carburant != 'E' && carburant != 'D' && carburant != 'T' ? 'E' : carburant);
- //E = essence, D = Diesel, si le carburant est différent de E et D alors il est assigné à E par défaut
- }
- // Constructeurs
- public Moteur() {
- setCarburant('E');
- setPuissance(1000);
- }
- public Moteur(int puissance, char carburant) {
- this.setPuissance(puissance);
- this.setCarburant(carburant);
- }
- //conversion to string
- @Override
- public String toString() {
- return "Moteur{" +
- "puissance=" + puissance +
- ", carburant=" + carburant +
- '}';
- }
- //test unitaire
- public static void main(String[] args) {
- Moteur m1= new Moteur(1200, 'D');
- Moteur m2= new Moteur( 1000, 'E');
- System.out.println(m1);
- System.out.println(m2);
- }
- }
- public class Voiture {
- // variables de classe
- private static int dernier_num_serie = 1;
- // variables d'instance
- private int num_serie;
- private String marque;
- //composition;
- private Moteur moteur = new Moteur();
- //getter/setter
- public int getDernier_num_serie() {
- return dernier_num_serie;
- }
- public int getNum_serie() {
- return num_serie;
- }
- public String getMarque() {
- return marque;
- }
- private static void setDernier_num_serie() {
- Voiture.dernier_num_serie++; // on incrémente le numéro de série
- }
- private void setNum_serie() {
- this.num_serie = dernier_num_serie; // on fait correspondre le numéro de série au numéro de série précedemment incrémenté
- }
- private void setMarque(String marque) {
- this.marque = ( marque==null || marque.trim().equals("")
- ? "FIAT"
- : marque.toUpperCase()
- );
- }
- //Constructeurs
- public Voiture(String marque, int puissance, char carburant) {
- this.setMarque(marque);
- this.moteur = new Moteur(puissance, carburant);
- this.setNum_serie();
- }
- public Voiture() {
- this(null, 1000, 'E');
- }
- public Voiture(String marque, Moteur moteur){
- if(moteur!=null)
- this.moteur = new Moteur(moteur.getPuissance(), moteur.getCarburant());
- this.setNum_serie();
- this.setMarque(marque);
- }
- @Override
- public String toString() {
- return "Voiture{" +
- "num_serie=" + num_serie +
- ", marque='" + marque + '\'' +
- ", moteur=" + moteur +
- '}';
- }
- public static void main(String[] args) {
- Voiture v1 = new Voiture("",new Moteur());
- Voiture v2 = new Voiture("Renaud",new Moteur());
- Voiture v3 = new Voiture("Hot Wheels",12000,'T');
- System.out.println(v1);
- System.out.println(v2);
- System.out.println(v3);
- }
- }
- public class Personne {
- // variables de classe
- private static int dernier_matricule = 1;
- // variables d'instance
- private int matricule;
- private String nom;
- private String prenom;
- //getter
- public static int getDernier_matricule() {
- return dernier_matricule;
- }
- public int getMatricule() {
- return matricule;
- }
- public String getNom() {
- return nom;
- }
- public String getPrenom() {
- return prenom;
- }
- //setter
- public static void setDernier_matricule(int dernier_matricule) {
- Personne.dernier_matricule++; // on incrémente le numéro de série
- }
- public void setMatricule(int matricule) {
- this.matricule = dernier_matricule;
- }
- public void setNom(String nom) {
- this.nom = nom;
- }
- public void setPrenom(String prenom) {
- this.prenom = prenom;
- }
- }
- public class Complex {
- private float realnum;
- private float complexnum;
- //getter
- public float getRealnum() {
- return realnum;
- }
- public float getComplexnum() {
- return complexnum;
- }
- //setter
- public void setRealnum(float realnum) {
- this.realnum = realnum;
- }
- public void setComplexnum(float complexnum) {
- this.complexnum = complexnum;
- }
- //fonction addition
- public Complex addition(Complex c2){
- float reel = getRealnum() + c2.getRealnum();
- float complex = getComplexnum() + c2.getComplexnum();
- return new Complex(complex, reel);
- }
- //fonction soustraction
- public Complex soustraction(Complex c2){
- float reel = getRealnum() - c2.getRealnum();
- float complex = getComplexnum() - c2.getComplexnum();
- return new Complex(complex, reel);
- }
- //fonction multiplication
- public Complex multiplication(Complex c2){
- float reel = (getRealnum() * c2.getRealnum());
- float complex = getComplexnum() * c2.getComplexnum();
- return new Complex(complex, reel);
- }
- //constructeur
- public Complex() {
- setComplexnum(1);
- setRealnum(1);
- }
- public Complex(float complexnum, float realnum) {
- this.setComplexnum(complexnum);
- this.setRealnum(realnum);
- }
- //conversion to string
- @Override
- public String toString() {
- return "Complex{" +
- "a=" + realnum +
- ", b=" + complexnum +
- "i" +
- '}';
- }
- //test unitaire
- public static void main(String[] args) {
- Complex c1= new Complex(2,3);
- Complex c2= new Complex(5,8);
- Complex c3= c1.addition(c2);
- Complex c4= c1.soustraction(c3);
- System.out.println(c1);
- System.out.println(c3);
- System.out.println(c4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement