0% found this document useful (0 votes)
48 views3 pages

SolTD 1

An Account class is defined with attributes of ID, name, and balance, and methods to get
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views3 pages

SolTD 1

An Account class is defined with attributes of ID, name, and balance, and methods to get
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1 public class Test {

2 //fonction minInt
3 static int minInt(int x, int y){
4 if (x < y)
5 return x;
6 return y;
7 }
8 //fonction maxInt
9 static int maxInt(int x, int y){
10 if (x > y)
11 return x;
12 return y;
13 }
14 //fonction pow
15 static int pow(int x, int y){
16 int resultat = 1;
17 for(int i = 1; i<=y; i++)
18 resultat = resultat * x;
19 return resultat;
20 }
21 public static void main(String[] args) {
22 //lire deux entier
23 java.util.Scanner sc = new java.util.Scanner(System.in); // créer un object du type Scanner pour la lecture
24 int a = sc.nextInt(); //lire entier a
25 int b = sc.nextInt(); //lire entier b
26 System.out.println("le maximum entre " +a+ " et " +b+ " est egale = "+maxInt(a,b));//afficher le maximum
27 System.out.println("le minimum entre " +a+ " et " +b+ " est egale = "+minInt(a,b));//afficher le minimum
28 System.out.println("le resultat de "+ a+ " a la puissance " +b+ " est egale = "+pow(a,b));//afficher la puissance
29 sc.close();//fermer le Scanner
30 }
31 }

Exercice 2 :
 La class Circle
1 class Circle {
2 //attributes
3 double radius;
4 String color;
5 //methods
6 Circle(){ radius = 1.0; color = "red";}
7 Circle(double r){ radius = r; color = "red";}
8 double getRadius(){ return radius;}
9 double getArea(){ return 3.14*radius*radius;}
10
11 //qustion 1
12 Circle(String color){ this.color = color; radius = 1.0;}
13 Circle(double radius, String color){ this.color = color; this.radius = radius;}
14
15 //question 2
16 String getColor() { return color;}
17 void setColor(String color) { this.color = color;}
18 void setRadius(double radius) { this.radius = radius;}
19 double getCircumference(){ return 2 * Math.PI * radius;}
20 public String toString(){ return "Cercle has a radius " + radius + " and the color " + color;}
21 }

 Tester la classe Circle et analyser les résultats


public static void main(String[] args) {
Circle c1 = new Circle();
1
Circle c2 = new Circle("blue");
2
Circle c3 = new Circle(3.2, "green");
3
4
System.out.println(c1.toString()); //Cercle has a radius 1.0 and the color red
5
System.out.println(c2.toString()); //Cercle has a radius 1.0 and the color blue
6
System.out.println(c3.toString()); //Cercle has a radius 3.2 and the color green
7
8
c1.setColor("black");
9
c2.setRadius(5);
10
System.out.println("c1 has a radius " + c1.getRadius() + " and the color " + c1.getColor());
11
//c1 has a radius 1.0 and the color black
12
System.out.println("c2 has an Area of " + c2.getArea() + " and a Circumference of " + c2.getCircumference());
13
//c2 has an Area of 78.5 and a Circumference of 31.41592653589793
14
15
}
Exercice 3
 La class Account
1 class Account {
2 //attributes
3 String id;
4 String name;
5 int balance = 0;
6 //methods
7 Account(String id, String name){this.id = id; this.name = name;}
8 Account(String id, String name, int balance){ this(id, name); this.balance = balance;}
9 String getID(){ return id;}
10 String getName(){ return name;}
11 int getBalance(){ return balance;}
12 int credit(int amount){
13 balance += amount;
14 return getBalance();}
15 int debit(int amount){
16 if(amount <= balance) balance -= amount;
17 else System.out.println("Amount exceeds balance");
18 return getBalance();
19 }
20 int transferTo(Account another, int amount){
21 if(amount <= balance) { another.credit(amount); debit(amount); }
22 else System.out.println("Amount exceeds balance");
23 return getBalance();}
24 public String toString(){return "Account [ id="+getID()+" name="+getName()+" balance="+getBalance() + "]";}
25
26 }
 La class Test
1 public static void main(String[] args) {
2 Account c1 = new Account("xc1254tyu", "mohamed chabaan");
3 Account c2 = new Account("xc8954cdf", "sami farouk", 5000);
4
5 c1.credit(3000);
6 c2.debit(1000);
7 c1.transferTo(c2, 2500);
8 System.out.println(c1.toString()); //Account [ id=xc1254tyu name=mohamed chabaan balance=500]
9 System.out.println(c2.toString()); //Account [ id=xc8954cdf name=sami farouk balance=6500]
10 c1.transferTo(c1, 1000);//Amount exceeds balance
11 }

You might also like