Skip to content
Snippets Groups Projects
Commit 387eb374 authored by Kevin Bonga's avatar Kevin Bonga
Browse files

initial commit

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 416 additions and 0 deletions
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
File added
File added
import java.util.Iterator;
public class ArrayListInt implements ListInt {
private static int CAPACITY = 10;
private int[] tab;
private int size;
public ArrayListInt() {
this.tab = new int[CAPACITY];
this.size = 0;
}
boolean isFull() {
return this.size == CAPACITY;
}
void increaseCapacity() {
int[] tmp = new int[size];
System.arraycopy(tab, 0, tmp, 0, size);
CAPACITY += 10;
tab = new int[CAPACITY];
System.arraycopy(tmp, 0, tab, 0, size);
}
public void insert(int value) {
if (isFull()) {
increaseCapacity();
}
tab[size++] = value;
}
public void insertAll(int... values) {
for (int val : values) {
insert(val);
}
}
public int size() {
return this.size;
}
public boolean isEmpty() {
return size == 0;
}
private class ArrayListIntIterator implements Iterator<Integer> {
private int currentIndex = 0;
public boolean hasNext() {
return currentIndex < size;
}
public Integer next() {
return tab[currentIndex++];
}
}
private class LinkedListInt implements ListInt{
private int[] tab;
int value;
private int curr;
private int next;
private int head;
boolean tabHasNextInt(){
return false;
}
public void insert(int n) {
tab[curr + 1]= n;
/*if(tab[0]){
}*/
}
public void insertAll(int... values) {
}
public int size() {
return 0;
}
public Iterator<Integer> iterator() {
return null;
}
}
public Iterator<Integer> iterator() {
return new ArrayListIntIterator();
}
}
public class EmptyIntegerStackException extends RuntimeException {
public EmptyIntegerStackException(String message) {
super(message);
}
}
import java.util.Iterator;
public class Exo5_s5 {
public static void main(String[] args) {
ListInt list = new ArrayListInt();
list.insert(0);
list.insert(3);
list.insertAll(2,1); // prend un nombre arbitraire d'élément
list.insertAll(6,1,3,5,75,121,12,14,6,18,22); // prend un nombre arbitraire d'élément
System.out.println("Size: " + list.size());
for (int v: list) {
System.out.println("Value: "+v);
}
Iterator<Integer> it = list.iterator();
while(it.hasNext()){
System.out.println("Value: "+ it.next());
}
}
}
\ No newline at end of file
import java.util.Optional;
import java.util.function.Supplier;
public class Exo6_s5 {
public static Optional<Integer> test(Supplier<Integer> nb){
try{
Integer result = nb.get();
if (result == null) {
//System.err.println("Supplier returned null");
return Optional.empty();
}
return Optional.of(nb.get());
}
catch (ArithmeticException e){
System.err.println("La division par zero est interdite !");
return Optional.empty();
}catch (RuntimeException e) {
//System.err.println("RuntimeException: " + e.getMessage());
return Optional.empty();
}
}
public static void main(String[] args) {
test(()->3);
test(()->3 + 4);
test(()->3/0);
test(()->null);
test(()->{throw new RuntimeException();});
}
}
import java.util.ArrayList;
import java.util.Optional;
import java.util.function.Consumer;
public class IntegerArrayStack extends ArrayList implements IntegerStack{
public int pop() {
if (isEmpty()){
throw new EmptyIntegerStackException("Liste vide, impossible de pop");
}
int tmp = (int) this.get(0);
for (int i=0;i<this.size()-1;i++){
this.set(i, this.get(i + 1));
}
this.remove(this.size()-1);
return tmp;
}
public Optional<Integer> popOption() {
if (isEmpty()){
return Optional.empty();
}
int val = this.pop();
return Optional.of(val);
}
public void ifHeadIsPresent(Consumer<Integer> consumer) {
if (!isEmpty()) {
consumer.accept(peek());
} else {
throw new EmptyIntegerStackException("Liste vide, impossible de pop");
}
}
public void push(int i) {
this.add(i);
}
public int size() {
int cpt=0;
for (Object o: this) {
cpt++;
}
return cpt;
}
public int peek() {
if (isEmpty()) {
throw new EmptyIntegerStackException("Liste vide, impossible de peek");
}
return (int)this.get(0);
}
public Optional<Integer> peekOption() {
return Optional.empty();
}
public boolean isEmpty() {
return this.size() == 0;
}
public static void main(String[] args) {
IntegerStack stack = new IntegerArrayStack();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
stack.ifHeadIsPresent(v -> {
System.out.println("Head: " + v);
});
}
}
import java.util.ArrayList;
import java.util.Optional;
import java.util.function.Consumer;
public interface IntegerStack {
int pop();
Optional<Integer> popOption();
void ifHeadIsPresent(Consumer<Integer> consumer);
void push(int i);
int size();
int peek();
Optional<Integer> peekOption();
boolean isEmpty();
}
public interface ListInt extends Iterable<Integer> {
void insert(int n);
void insertAll(int... values);
int size();
}
class Button implements Pushable {
@Override
public void push() {
System.out.println("Button has been pushed");
}
}
class Test{
public static void push(Pushable p) {
p.push();
}
}
// classe géné utilise un type qui n'est pas défini à l'héritage
// type géné utilise un type qui est défini à l'instanciation
public class Main {
public static void main(String[] args) {
Button b = new Button();
Test.push(b);
}
}
\ No newline at end of file
public interface Pushable {
void push();
}
import java.util.Random;
public interface Status {
static Status process() {
int chance = new Random().nextInt(3);
if (chance == 0) {
return new On();
} else {
return chance == 1 ? new Off() : new Err("Oups");
}
}
static Err makeError(String message) {
return new Err(message);
}
default boolean isOn() {
return false;
}
default boolean isOff() {
return false;
}
default String getErrorMessage() {
throw new UnsupportedOperationException("...");
}
// Classe interne statique On
class On implements Status {
public boolean isOn() {
return true;
}
}
// Classe interne statique Off
class Off implements Status {
public boolean isOff() {
return true;
}
}
// Classe interne statique Err
class Err implements Status {
private String msg;
public Err(String msg) {
this.msg = msg;
}
public String getErrorMessage() {
return this.msg;
}
}
// Classe principale StatusApp
public class StatusApp {
public static void main(String[] args) {
Status s1 = Status.process();
if (s1.isOn()) {
System.out.println("L'appareil fonctionne");
} else if (s1.isOff()) {
System.out.println("L'appareil est éteint");
} else
System.out.println("L'appareil est instable" + s1.getErrorMessage());
Status s2 = Status.makeError("Oops");
}
}
}
public class exo2 {
}
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/git_cours.iml" filepath="$PROJECT_DIR$/.idea/git_cours.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ 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