Skip to content
Snippets Groups Projects
Commit d3ab9caf authored by Baptiste Royer's avatar Baptiste Royer
Browse files

Ajout tp09

parent 7065364a
No related branches found
No related tags found
No related merge requests found
Showing
with 227 additions and 8 deletions
public class Ordonnanceur {
}
\ No newline at end of file
File added
File added
File added
File added
package TP04.Exemple1; package TP04.Exercice_1.Exemple1;
public class Exemple1 { public class Exemple1 {
......
package TP04.Exemple2; package TP04.Exercice_1.Exemple2;
public class Dutilisation { public class Dutilisation {
public void example2() throws MyException { public void example2() throws MyException {
......
package TP04.Exemple2; package TP04.Exercice_1.Exemple2;
public class MyException extends Exception { public class MyException extends Exception {
public MyException() {} public MyException() {}
......
package TP04.Exemple3; package TP04.Exercice_1.Exemple3;
import TP04.Exemple2.MyException; import TP04.Exercice_1.Exemple2.MyException;
public class Exemple3 { public class Exemple3 {
public void doItAgain() throws MyException { public void doItAgain() throws MyException {
......
package TP04.Exemple4; package TP04.Exercice_1.Exemple4;
public class Exemple4 { public class Exemple4 {
public int example4() { public int example4() {
......
package TP04.Exemple5; package TP04.Exercice_1.Exemple5;
public class Exemple5 { public class Exemple5 {
public void example5() { public void example5() {
......
package TP04.Exemple6; package TP04.Exercice_1.Exemple6;
public class Exemple6 { public class Exemple6 {
public static void main(String[] args) { public static void main(String[] args) {
......
package TP04.Exercice_2;
import java.util.Scanner;
public class Exercice2 {
class LocalKeyboard {
public static int readInt(String message) {
Scanner scanner = new Scanner(System.in);
System.out.println(message);
int r = scanner.nextInt();
scanner.close();
return r;
}
}
public static int[] tab = {17, 12, 15, 38, 29, 157, 89, -22, 0, 5};
public static int division(int index, int divisor) {
return tab[index]/divisor;
}
public static void statement() {
int x, y;
x = LocalKeyboard.readInt("Write the index of the integer to divide: ");
y = LocalKeyboard.readInt("Write the divisor: ");
System.out.println("The result is: " + division(x,y));
}
}
\ No newline at end of file
package TP04.Exercice_3;
public enum Container {
BLISTER(1),
BOX(10),
CRATE(50);
private final int capacity;
Container(int capacity) {
this.capacity = capacity;
}
public int getCapacity() {
return this.capacity;
}
}
\ No newline at end of file
package TP04.Exercice_3;
public class ExtendedException extends Exception {
public ExtendedException(String message) {
super(message);
}
}
\ No newline at end of file
package TP04.Exercice_3;
public class Thing {
private String name;
private int quantity;
private Container container;
public Thing(String name, int quantity, Container container) {
this.name = name;
this.quantity = quantity;
this.container = container;
}
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
public Container getContainer() {
return container;
}
public void setName(String name) {
this.name = name;
}
public void setContainer(Container container) {
this.container = container;
}
@Override
public String toString() {
return "Thing{" +
"name='" + name + '\'' +
", quantity=" + quantity +
", container=" + container +
'}';
}
public void setQuantity(int value) throws ExtendedException {
try {
if(value < this.container.getCapacity()) {
this.quantity = value;
} else {
throw new ExtendedException("Quantity too high to fit in the box.");
}
} catch(ExtendedException e) {
System.out.println("Addition Impossible !");
}
}
public boolean transferToLargerContainer() {
if (this.container.ordinal() == Container.values().length - 1) {
return false;
}
this.container = Container.values()[this.container.ordinal() + 1];
return true;
}
public void add(int value) throws ExtendedException {
try {
setQuantity(value + this.getQuantity());
} catch(ExtendedException e) {
transferToLargerContainer();
setQuantity(value + this.getQuantity());
}
}
}
\ No newline at end of file
package TP04.Exercice_3;
public class UseThing {
public static void main(String[] args) throws ExtendedException {
Thing ch1 = new Thing("uneThing", 3, Container.BOX);
System.out.println(ch1);
ch1.add(20);
System.out.println(ch1 + "\n");
Thing ch2 = new Thing("maThing", 12, Container.BOX);
System.out.println(ch2);
ch2.add(60);
System.out.println(ch2 + "\n");
}
}
\ No newline at end of file
package TP04.Exercice_4;
public class Competitor {
private int ID;
private final String NAME;
private Country nationality;
private Team team;
public Competitor(String name, Country nationality, Team team) {
NumAuto temp = new NumAuto();
this.ID = temp.getID();
this.NAME = name;
this.nationality = nationality;
this.team = team;
}
public int getID() {
return this.ID;
}
public String getName() {
return this.NAME;
}
public Country getCountry() {
return this.nationality;
}
public Team getTeam() {
return this.team;
}
public String toString() {
return this.ID + " - " + this.NAME + " (" + this.nationality.getCode() + ") -> " + this.team.toString();
}
public boolean isFrom(Team team) {
return this.team == team;
}
public boolean isFrom(Country country) {
return this.nationality == country;
}
public boolean equals(int ID) {
return this.ID == ID;
}
}
\ No newline at end of file
package TP04.Exercice_4;
public enum Country {
FRANCE("FR"),
GERMANY("GE"),
RUSSIA("RU"),
SWEDEN("SW"),
AUSTRIA("AU"),
ITALY("IT");
private String code;
Country(String code) {
this.code = code;
}
public String getCode() {
return this.code;
}
}
\ No newline at end of file
package TP04.Exercice_4;
public class NumAuto {
private static int ID = 0;
public int getID() {
return ++ID;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment