Académique Documents
Professionnel Documents
Culture Documents
: EL BAHTI Ayoub
public Article(long reference, String intitule, float prixHT, int quantiteEnSt
ock) {
this.reference = reference;
this.intitule = intitule;
this.prixHT = prixHT;
this.quantiteEnStock = quantiteEnStock;
}
public long getReference() {
return reference;
}
public String getIntitule() {
return intitule;
}
public int getQuantiteEnStock() {
return quantiteEnStock;
}
public void approvisionner(int nombreUnites) {
quantiteEnStock += nombreUnites;
// System.out.println("quantite = " + quantiteEnStock);
}
public boolean vendre(int nombreUnites) {
if (nombreUnites > quantiteEnStock)
return false;
quantiteEnStock -= nombreUnites;
return true;
}
public String toString() {
return this.getClass().getSimpleName() + " reference : " + reference + "
intitule : " + intitule + " prixHT" + prixHT + "quantiteEnStock = " + quantiteEnS
tock;
}
public boolean equals(Article B) {
if (this.getReference() == B.getReference())
return true;
else
return false;
}
public float prixHT(){
return (float) quantiteEnStock * prixHT;
}
public float prixTTc(){
return (float) (prixHT() * (1 + TAUX_TVA));
}
public static void main(String[] args) {
Article[] stock = new Article[3];
stock[0] = new Article(112,"amos",112,20);
stock[1] = new Article(113,"signal",115,80);
stock[2] = new Article(114,"siris",142,70);
for(Article a:stock){
System.out.println(a);
}
stock[0].approvisionner(10);
stock[1].vendre(10);
System.out.println("prix HT article 3 = " + stock[2].prixHT());
System.out.println("prix TTC article 3 = " + stock[2].prixTTc());
for(Article a:stock){
System.out.println(a);
}
}
}
II- Dates :
import java.util.Calendar;
public class Date{
private int annee;
private int mois;
private int jour;
private int jourSemaine;
//constructeur
public Date(int annee,int mois,int jour){
boolean dateInvalide = (mois < 1 || mois > 12) || (jour <= 0 || jour > 31) ||
(jour == 31 && (mois == 4 || mois == 6 || mois == 9 || mois == 11)) || (jour >= 30
&& mois == 2) || (jour == 29 && mois == 2 && annee % 4 != 0);
if(dateInvalide == true) {
System.out.println("date incohérente !! ");
System.exit(1);
}
else {
this.annee = annee;
this.mois = mois;
this.jour = jour;
}
}
private int jourSemaines(int jour, int mois, int annee){
Calendar c = Calendar.getInstance();
c.set(annee, mois - 1, jour);
return (c.get(java.util.Calendar.DAY_OF_WEEK) + 5) % 7;
}
public int getJour(){
return jour;
}
public int getMois(){
return mois;
}
public int getAnnee(){
return annee;
}
public int getJourSemaine(){
jourSemaine = jourSemaines(jour, mois, annee);
return jourSemaine;
}
public String afficheJours(int jour){
String [] joursDeSmaine= {"lundi","mardi","mercredi","jeudi","vendredi","sam
edi","dimanche"};
return joursDeSmaine[jour];
}
public String afficheMois(int mois){
String [] nbrMois= {"","janvier", "février", "mars", "avril", "mai", "juin", "
juillet", "août", "septembre", "octobre", "novembre", "décembre"};
return nbrMois[mois];
}
@Override
public String toString(){
int v = getJourSemaine();
return " " + afficheJours(v) + " " + jour + " " + afficheMois(mois) + " "
+ annee;
}
public boolean infeg(Date d){
if(d.annee < this.annee) return false;
else if(!(d.annee < this.annee)) return true;
else if(d.mois < this.mois) return false;
else if(!(d.mois < this.mois)) return true;
else if(d.jour <this.jour) return false;
else if(!(d.jour <this.jour)) return true;
else return true;
}
public static void main(String[] args) {
Date d1 = new Date(2000,11,0);
Date d2 = new Date(2000,2,27);
System.out.println(d1.toString());
System.out.println(d2.toString());
System.out.println(d1.infeg(d2));
}
}
III- Flacons et bouteilles...
public class Flacon {
private final float capacite;
private float volume;
private float concentration;
private String etiquette;
//constructeur
public Flacon(String etiquette, float capacite){
this.etiquette = etiquette;
this.capacite = capacite;
volume = 0;
}
//tester les volumes donnés
public void verser(float volumeSirop, float volumeEau){
boolean test = (volumeEau + volumeSirop) <= capacite;
if(test == true){
concentration = (volumeSirop) / (volumeSirop + volumeEau);
volume = volumeSirop + volumeEau;
}
else {
System.out.println(" verifier les volumes inserés !! ");
System.exit(1);
}
}
public void transvaser(Flacon autreFlacon, float volume1){
if((this.volume + volume1) <= capacite && (volume1 + autreFlacon.volume) <
= autreFlacon.capacite) {
this.volume += volume1;
autreFlacon.volume -= volume1;
}
else {
System.out.println(" verifier la capacité et ton volume de ton Flacon!
! ");
System.exit(1);
}
}
//afficher les données
@Override
public String toString(){
return " etiquette = " + etiquette + " volume = " + volume + " ml " + " co
ncentraction = " + concentration + "%";
}
//capacité a été en état finale parceque c'est impossible de modiffier la capa
cité d'un flacon aprés la fabrication
public static void main(String[] args) {
Flacon f1 = new Flacon(" flacon 1 C = 50 ml",60);
Flacon f2 = new Flacon(" flacon 2 C = 70 ml",70);
f1.verser(20, 30);
f2.verser(20, 30);
System.out.println(" f1 --> " + f1.toString() + "\n f2--> " + f
2.toString());
f2.transvaser(f1, 10);
System.out.println(" f1 --> " + f1.toString() + "\n f2--> " + f
2.toString());
}
}
Points du plan :
import java.text.DecimalFormat;
public class Point {
private double x;
private double y;
public Point(double x,double y){
this.x = x;
this.y = y;
}
public double x(){
return x;
}
public double y(){
return y;
}
public double r(){
return Math.sqrt((x * x) + (y * y));
}
public double t(){
return Math.atan2(y, x);
}
public String tostring(){
return " " + x + ", " + y;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Point)) {
return false;
}
Point a = (Point) obj;
if ((x == a.x) && (y == a.y))
return true;
else
return false;
}
public void homothetie(double k){
x *= k;
y *= k;
}
public void translation(double dx, double dy){
x += dx;
y += dy;
}
public void rotation(double a){
x = this.r() * Math.cos(this.t() + a);
y = this.r() * Math.sin(this.t() + a);
}
private static DecimalFormat df = new DecimalFormat("#.##");
public static void main(String[] args) {
Point p1 = new Point(1,5);
Point p2 = new Point(1,5);
Object ob1 = p2;
System.out.println(" --> " + p1.equals(ob1));
System.out.println(" x == " + p1.x() + " y == " + p1.y() + " r == " + df.fo
rmat(p1.r()) + " t == " + df.format(p1.t()) );
}
public double getx(){
return x;
}
public double gety(){
return y;
}
}
LignePol :
public class LignePol {
private Point[] sommets;
public LignePol(Point[] sommets) {
this.sommets = sommets;
}
public LignePol(int n) {
this.sommets = new Point[n];
}
public Point getSommet(int i) {
return sommets[i];
}
public void setSommet(int i, Point p){
p = getSommet(i);
}
@Override
public String toString() {
String ajoute = "[ ";
for (int i = 0; i < sommets.length; i++)
ajoute += sommets[i] + " , ";
return ajoute + "]";
}
public void homothetie(double k){
for(Point e: sommets)
e.homothetie(k);
}
public void translation(double dx, double dy){
for(Point e: sommets)
e.translation(dx, dy);
}
public void rotation(double a){
for(Point e: sommets)
e.rotation(a);
}
public static void main(String[] args) {
Point[] p1 = new Point[2];
p1[0] = new Point(1,2);
p1[1] = new Point(1,2);
LignePol lp = new LignePol(p1);
System.out.println(lp.toString());
}
}
V- Fractions :
import java.math.BigInteger;
public class Fraction {
private BigInteger n;
private BigInteger d;
public Fraction(BigInteger num, BigInteger den){
this.n = num;
this.d = den;
}
public Fraction(int n1,int d1){
this.n = BigInteger.valueOf(n1);
this.d = BigInteger.valueOf(d1);
}
public Fraction(int n){
this(n,1);
}
public double doubleValue(){
double db = n.doubleValue() / d.doubleValue();
return db;
}
@Override
public String toString(){
String chr1 = String.valueOf(n);
String chr2 = String.valueOf(d);
return chr1 +"/" + chr2;
}
public void add(Fraction f1){
BigInteger n1 = (f1.n).multiply(this.d);
BigInteger n2 = (f1.d).multiply(this.n);
n = n1.add(n2);
d = (f1.d).multiply(this.d);
}
public void sub(Fraction f1){
BigInteger n1 = (this.n).multiply(f1.d);
BigInteger n2 = (this.d).multiply(f1.n);
n = n1.subtract(n2);
d = (f1.d).multiply(this.d);
}
public void mult(Fraction f1){
n = (f1.n).multiply(this.n);
d = (f1.d).multiply(this.d);
}
public void divi(Fraction f1){
n = (f1.n).multiply(this.d);
d = (f1.d).multiply(this.n);
}
public static void main(String[] args) {
Fraction f1 = new Fraction(1, 2);
Fraction f2 = new Fraction(5, 20);
f1.add(f2);
System.out.println(" f1 = " + f1.doubleValue() + " f2 = " + f2.doubl
eValue());
}
}