Skip to content
Snippets Groups Projects
Commit faee8fb4 authored by Malori Alvarez's avatar Malori Alvarez
Browse files

INIT

parent 4ffb4342
No related branches found
No related tags found
No related merge requests found
Showing
with 907 additions and 0 deletions
File added
package tp1;
class Book {
// class attributes
String author;
String title;
int year;
// constructor
Book(String author, String title, int year) {
this.author = author;
this.title = title;
this.year = year;
}
// methods
String getAuthor() {
return this.author;
}
String getTitle() {
return this.title;
}
String print() {
return author + "\t" + title + "\t" + year;
}
}
class UseBook{
public static void main(String[] args){
Book[] biblio = {
new Book("Edwin A. Abbott", "Flatland", 1884),
new Book("Edwin A. Abbott", "Flatland", 1885),
new Book("Edwin A. Abbott", "Flatland", 1886)
};
for (int i = 0; i < biblio.length; i = i + 1) {
System.out.println(biblio[i].print());
}
}
}
\ No newline at end of file
package tp1;
class Irregular{
int[][] irregular;
int[] lineSizeTab;
Irregular(int[] lineSize){
irregular = new int[lineSize.length][3];
lineSizeTab = lineSize;
randomFilling();
}
void randomFilling(){
for (int i = 0; i < irregular.length; i = i + 1) {
for(int id = 0; id < lineSizeTab[i]; id++){
irregular[i][id] = (int)(Math.random() * 10);
}
}
}
String display(){
String res = "";
for (int i = 0; i < irregular.length; i = i + 1) {
for(int id = 0; id < lineSizeTab[i]; id++){
res = res + String.valueOf(irregular[i][id]);
}
res = res + "\n";
}
return res;
}
}
class UseIrregular{
public static void main(String[] args){
Irregular irr = new Irregular(new int[]{3, 2, 3});
System.out.println(irr.display());
}
}
\ No newline at end of file
package tp1;
class Parameter {
public static void main(String[] args) {
if(args.length==0) {
System.out.println("No parameter");
}
for(int i=0; i<args.length; i++) {
System.out.println("(" + (i+1) + ") " + args[i]);
}
}
}
class Competitor{
int numberSign;
int score;
int min;
int sec;
Competitor(int numberSign, int score, int min, int sec){
if(numberSign > 100 || numberSign < 1){
numberSign = -1;
}
if(score > 60 || score < 0){
score = -1;
}
this.numberSign = numberSign;
this.score = score;
this.min = min;
this.sec = sec;
}
String display(){
return "[No"+String.valueOf(numberSign)+","+String.valueOf(score)+"points,"+(sec+min*60)+" s]";
}
}
class Main{
public static void main(String[] args){
Competitor[] competitorTab = new Competitor[100];
competitorTab[0] = new Competitor(1,45,15,20);
competitorTab[1] = new Competitor(2,32,12,45);
competitorTab[2] = new Competitor(5,12,13,59);
competitorTab[3] = new Competitor(12,12,15,70);
competitorTab[4] = new Competitor(32,75,15,20);
System.out.println(competitorTab[0].display());
System.out.println(competitorTab[1].display());
System.out.println(competitorTab[2].display());
}
}
\ No newline at end of file
class Dice{
int numberSides;
double rand = 0;
int value = 1;
Dice(int numberSides) {
this.numberSides = numberSides;
if(this.numberSides <= 0) {
this.numberSides = 1;
}
}
void roll(){
this.rand = Math.random() * numberSides;
this.value = (int)this.rand;
}
String display(){
return "Dice(" + numberSides+ "faces, " + rand+ " rand, "+this.value+" choisi)";
}
int getValue(){
return this.value;
}
}
class DicePlayer{
String name;
int totalValue = 0;
int nbDiceRolls = 0;
DicePlayer(String name){
this.name = name;
}
void play(Dice dice6){
dice6.roll();
this.totalValue = this.totalValue + dice6.getValue();
this.nbDiceRolls++;
}
int getTotalValue(){
return this.totalValue;
}
int getNbDiceRolls(){
return this.nbDiceRolls;
}
String displayPlayer(){
return this.name+": "+this.totalValue+" points en "+this.nbDiceRolls+" coups.";
}
}
class OneDicePlayerGame{
public static void main(String[] args){
NDicePlayerGame game = new NDicePlayerGame(10);
/*
DicePlayer dicePlayer = new DicePlayer("Cyprien");
while(dicePlayer.getTotalValue() < 20){
Dice dice = new Dice(6);
dicePlayer.play(dice);
System.out.println(dice.display());
}
System.out.println(dicePlayer.displayPlayer());
DicePlayer dicePlayer2 = new DicePlayer("Gabin");
while(dicePlayer2.getTotalValue() < 20){
Dice dice = new Dice(6);
dicePlayer2.play(dice);
System.out.println(dice.display());
}
System.out.println(dicePlayer2.displayPlayer());
TwoDicePlayerGame twoDicePlayerGame = new TwoDicePlayerGame(dicePlayer, dicePlayer2);
System.out.println(twoDicePlayerGame.winner().displayPlayer()+" winner");
*/
}
}
class TwoDicePlayerGame{
DicePlayer dicePlayer1;
DicePlayer dicePlayer2;
TwoDicePlayerGame(DicePlayer dicePlayer1, DicePlayer dicePlayer2){
this.dicePlayer1 = dicePlayer1;
this.dicePlayer2 = dicePlayer2;
}
DicePlayer winner(){
if(dicePlayer1.getTotalValue() > 20 && dicePlayer2.getTotalValue() > 20){
if(dicePlayer1.getNbDiceRolls() < dicePlayer2.getNbDiceRolls()){
return dicePlayer1;
}else if(dicePlayer1.getNbDiceRolls() > dicePlayer2.getNbDiceRolls()){
return dicePlayer2;
}else{
if(dicePlayer1.getTotalValue() > dicePlayer2.getTotalValue()){
return dicePlayer1;
}else if(dicePlayer1.getTotalValue() > dicePlayer2.getTotalValue()){
return dicePlayer2;
}else{
return null;
}
}
}else{
if(dicePlayer1.getTotalValue() > dicePlayer2.getTotalValue()){
return dicePlayer1;
}else if(dicePlayer1.getTotalValue() > dicePlayer2.getTotalValue()){
return dicePlayer2;
}else{
return null;
}
}
}
}
class NDicePlayerGame{
DicePlayer[] players;
NDicePlayerGame(int nbPlayer){
players = new DicePlayer[nbPlayer];
int i = 0;
while(i < players.length){
players[i] = new DicePlayer(String.valueOf(i));
while(players[i].getTotalValue() < 20){
Dice dice = new Dice(6);
players[i].play(dice);
System.out.println(dice.display());
}
System.out.println(players[i].displayPlayer());
i++;
}
/*
DicePlayer[] winners = winners();
for (int place = 0; place < 3; i++){
System.out.println(place+"- "+winners[place].displayPlayer());
}*/
}
/*
DicePlayer[] winner(){
return Arrays.sort(players.displayPlayer());
}*/
}
\ No newline at end of file
package tp3;
import tp3.Rank;
import tp3.Color;
import java.util.Arrays;
public class Card {
private Color color;
private Rank rank;
public Color getColor() {
return color;
}
public Rank getRank() {
return rank;
}
public String toString() {
return this.color+" "+this.rank;
}
public Card(Color color, Rank rank){
this.rank=rank;
this.color=color;
}
public Card(String color, String rank){
this.rank=Rank.valueOf(rank);
this.color=Color.valueOf(color);
}
public boolean equals(Card carte){
if (this.rank==carte.rank && this.color==carte.color){
return true;
}
return false;
}
public boolean isBefore(Card carte){
if (this.rank.ordinal()<carte.rank.ordinal()){
return true;
}
return false;
}
public int compareRank(Card carte){
if (this.rank.ordinal()<carte.rank.ordinal()){
return -1;
}else if (this.rank.ordinal()==carte.rank.ordinal()){
return 0;
}
return 1;
}
public int compareColor(Card carte){
if (this.color.ordinal()<carte.color.ordinal()){
return -1;
}else if (this.color.ordinal()==carte.color.ordinal()){
return 0;
}
return 1;
}
}
package tp3;
public enum Color {
CLUB, DIAMOND, HEART, SPADE;
}
\ No newline at end of file
package tp3;
public enum Rank {
SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE;
}
package tp3;
import tp3.TaskStatus;
import java.time.LocalDate;
import java.time.Period;
public class Task {
private String idTask;
private LocalDate creationDate;
private LocalDate deadline;
private TaskStatus state;
private String description;
private int duration;
private static int counter = 0;
public void Task(String description, int duration){
this.description=description;
this.duration=duration;
this.creationDate=LocalDate.now();
this.state=TaskStatus.TODO;
this.deadline=creationDate.plusDays(duration);
counter++;
this.idTask= String.valueOf(LocalDate.now().getYear()).substring(String.valueOf(LocalDate.now().getYear()).length() - 2, String.valueOf(LocalDate.now().getYear()).length())
+String.valueOf(LocalDate.now().getMonth()) + String.valueOf(counter);
}
public void Task(String description, LocalDate creation, LocalDate deadline, int duration){
this.description=description;
this.creationDate=creation;
this.deadline=deadline;
this.duration=duration;
this.state=TaskStatus.TODO;
counter++;
this.idTask= String.valueOf(LocalDate.now().getYear()).substring(String.valueOf(LocalDate.now().getYear()).length() - 2, String.valueOf(LocalDate.now().getYear()).length())
+String.valueOf(LocalDate.now().getMonth()) + String.valueOf(counter);
}
public String toString() {
return "(T"+idTask+" = "+description+":"+state+"("+duration+")"+" :"+deadline;
}
void changeStatus(TaskStatus st){
this.state=st;
}
void changeStatus(char c){
if (c=='T'){
this.state=TaskStatus.TODO;
}else if (c=='O'){
this.state=TaskStatus.ONGOING;
}else if (c=='D'){
this.state=TaskStatus.DELAYED;
}else if (c=='F'){
this.state=TaskStatus.FINISHED;
}
}
void changeStatus(){
int index=this.state.ordinal();
this.state=TaskStatus.values()[(index+1)%TaskStatus.values().length];
}
boolean isLate(){
Period period=LocalDate.now().until(this.deadline);
if(period.isNegative()){
return true;
}
return false;
}
void delay(int nbDays){
this.deadline=this.deadline.plusDays(nbDays);
}
LocalDate getDeadline(){
return this.deadline;
}
LocalDate getCreationDate(){
return this.creationDate;
}
Period getDuration(){
return this.creationDate.until(this.deadline);
}
}
\ No newline at end of file
package tp3;
public enum TaskStatus {
TODO, ONGOING, DELAYED, FINISHED;
}
package tp3;
import java.util.*;
import java.time.LocalDate;
public class ToDoList {
private List<Task> chores = new ArrayList<Task>();
public void ToDoList(){
}
public String toString(){
return ""+this.chores;
}
void addTask(Task aTask){
chores.add(aTask);
}
void removeTask(int i){
chores.remove(i);
}
void removeTask(Task aTask){
int i = chores.indexOf(aTask);
chores.remove(i);
}
boolean isOverwhelmed(){
if(chores.get(chores.size() - 1) == null){
return false;
}
return true;
}
int getNbTask(){
return chores.size();
}
void onSickLeave(int nbDays){
for (int ind = 0; ind < chores.size(); ind = ind + 1) {
chores.get(ind).delay(nbDays);
}
}
List<Task> dueTasks(){
List<Task> res = new ArrayList<Task>();
for (int ind = 0; ind < chores.size(); ind = ind + 1) {
if(chores.get(ind).getDeadline() == LocalDate.now()){
res.add(chores.get(ind));
}
}
return res;
}
/*
void durationSort(){
chores.sort((Task o1, Task o2)->o1.getDuration().getDays().compareTo(o2.getDuration().getDays()));
}
void senioritySort(){
chores.sort((Task o1, Task o2)->o1.getCreationDate().compareTo(o2.getCreationDate()));
}*/
}
package tp3;
import tp3.Card;
import tp3.Color;
import tp3.Rank;
public class UseCard {
public static void main(String[] args){
Card c1 = new Card("CLUB", "NINE");
Card c2 = new Card(Color.SPADE, Rank.ACE);
System.out.println(c1.getColor());
System.out.println(c2.getRank());
System.out.println(c1.compareColor(c2));
System.out.println(c1.compareRank(c2));
System.out.println(c1.isBefore(c2));
System.out.println(c1.equals(c2));
System.out.println(c1.toString());
}
}
package tp3;
import java.time.LocalDate;
import java.time.Period;
import java.util.Scanner;
public class UseLocalDate {
public static String dateOfTheDay(){
return "Today's date "+LocalDate.now();
}
public static LocalDate inputDate(){
Scanner input = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter day");
int day = input.nextInt();
if(day < 1){
day = 1;
}else{
if(day > 31){
day = 31;
}
}
System.out.println("Enter month");
int month = input.nextInt();
if(month < 1){
month = 1;
}else{
if(month > 12){
month = 12;
}
}
System.out.println("Enter year");
int year = input.nextInt();
if(year < 0){
year = 0;
}
return LocalDate.of(year, month, day);
}
public static String diffDate(){
LocalDate date1=inputDate();
LocalDate date2=inputDate();
Period period=date1.until(date2);
return period.toString();
}
public static void main(String[] args){
System.out.println(dateOfTheDay());
System.out.println(inputDate().toString());
System.out.println(diffDate());
}
}
package tp4;
import tpOO.tp04.*;
public class PendingCaseQueue{
public final static int CAPACITY = 10;
int id;
PendingCase[] pendingCases;
PendingCaseQueue(){
pendingCases = new PendingCase[CAPACITY];
}
void clear(){
pendingCases = new PendingCase[CAPACITY];
}
boolean isEmpty(){
if(pendingCases[0]==null){
return true;
}
return false;
}
boolean isFull(){
if(pendingCases.length == CAPACITY){
return true;
}
return false;
}
int size(){
return pendingCases.length;
}
void addOne(PendingCase other){
if(isFull()){
System.out.println("impossible");
}else{
PendingCase[] before = pendingCases;
PendingCase[] newGrade = new PendingCase[pendingCases.length + 1];
for (int notes = 0; notes < before.length; notes = notes + 1) {
newGrade[notes] = before[notes];
}
newGrade[newGrade.length] = other;
pendingCases = newGrade;
}
}
PendingCase removeOne(){
if(isEmpty()){
return null;
}else{
PendingCase[] before = pendingCases;
PendingCase[] newGrade = new PendingCase[pendingCases.length - 1];
for (int notes = 0; notes < before.length - 1; notes = notes + 1) {
newGrade[notes] = before[notes];
}
pendingCases = newGrade;
}
return null;
}
public String toString(){
String res = "";
for (int name = 0; name < pendingCases.length; name++) {
if(pendingCases[name] != null){
res = res + pendingCases[name]+"\n";
}else{}
}
return res;
}
}
\ No newline at end of file
package tp4;
public class Person{
private static int ID = -1; //pour être à 0 sur la première assignation
private String forename;
private String name;
public Person(String forename, String name){
this.forename = forename;
this.name = name;
ID++; //assignation automatique
}
//getters
int getID() {
return this.ID;
}
String getForename(){
return this.forename;
}
String getName(){
return this.name;
}
//setters
void setName(String name){
this.name = name;
}
void setForename(String forename){
this.forename = forename;
}
public String toString(){
return getID()+":"+getForename()+" "+getName();
}
public boolean equals(Object obj) {
if(this == obj) return true;
if(obj == null) return false;
if(getClass() != obj.getClass()) return false;
Person other = (Person) obj;
if(ID != other.ID) return false;
if (name == null) {
if (other.name != null) return false;
} else if (!name.equals(other.name)) return false;
if (forename == null) {
if (other.forename != null) return false;
} else if (!forename.equals(other.forename)) return false;
return true;
}
}
\ No newline at end of file
package tp4;
import tp4.Person;
public class Student{
private double[] grades;
private Person person;
Student(Person pers, double[] grades){
this.person = pers;
this.grades = grades;
}
Student(String forename, String name, double[] grades){
this(forename, name);
this.grades = grades;
}
private Student(String forename, String name){
person = new Person(forename, name);
}
int getID() {
return person.getID();
}
String getForename(){
return person.getForename();
}
String getName(){
return person.getName();
}
/*
double[] getGrade(){
return thid.grades;
}*/
void setName(String name){
person.setName(name);
}
void setForename(String forename){
person.setForename(forename);
}
double getAverage(){
if(this.grades.length == 0) return 0.0;
double total = 0;
for (int notes = 0; notes < this.grades.length; notes = notes + 1) {
total = total + this.grades[notes];
}
return total/this.grades.length;
}
public String toString(){
String res;
res = "Student["+getID()+":"+ getForename()+" "+getName()+" = [";
for (int notes = 0; notes < this.grades.length; notes = notes + 1) {
res = res + this.grades[notes]+", ";
}
res = res + "]]";
return res;
}
void addGrade(double aGrade){
double[] before = this.grades;
double[] newGrade = new double[this.grades.length + 1];
for (int notes = 0; notes < before.length; notes = notes + 1) {
newGrade[notes] = before[notes];
}
newGrade[newGrade.length - 1] = aGrade;
}
}
\ No newline at end of file
package tp4;
import tp4.Student;
public class StudentAbs{
public Student student;
private int nbAbsence;
StudentAbs(Student student, int nbAbsence){
this.student = student;
this.nbAbsence = nbAbsence;
}
public String toString(){
return student.toString()+" nbAbs="+nbAbsence;
}
boolean warning(int thresholdAbs, double thresholdAvg){
if(thresholdAbs <= nbAbsence) return true;
if(student.getAverage() <= thresholdAvg) return true;
return false;
}
boolean validation(int thresholdAbs, double thresholdAvg){
if(thresholdAbs >= nbAbsence && student.getAverage() >= thresholdAvg) return true;
return false;
}
}
\ No newline at end of file
package tp4;
import tpOO.tp04.PendingCase;
public class UsePendingCaseQueue {
public static void main(String[] args) {
PendingCase pc1 = new PendingCase("Alice", "AAA", 1234.56);
PendingCase pc2 = new PendingCase("Bruno", "BBB", 0.42);
PendingCase pc3 = new PendingCase("Chloé", "CCC", 745.99);
PendingCase pc4 = new PendingCase("Denis", "DDD", 125.0);
//PendingCaseQueue.CAPACITY = 3;
PendingCaseQueue pcq = new PendingCaseQueue();
System.out.println("Before anything: " + pcq);
System.out.println("Empty? " + pcq.isEmpty() + "\tFull? " + pcq.isFull());
pcq.addOne(pc1);
pcq.addOne(pc2);
System.out.println("After addition of pc1 and pc2: " + pcq);
System.out.println("Empty? " + pcq.isEmpty() + "\tFull? " + pcq.isFull());
pcq.addOne(pc3);
System.out.println("After addition of pc3: " + pcq);
System.out.println("Empty? " + pcq.isEmpty() + "\tFull? " + pcq.isFull());
pcq.addOne(pc4);
System.out.println("After addition of pc4: \t" + pcq);
pcq.removeOne();
System.out.println("After a removal: " + pcq);
pcq.addOne(pc1);
System.out.println("After addition of pc1: " + pcq);
pcq.clear();
System.out.println("After clearing: " + pcq);
}
}
package tp4;
public class UsePerson {
public static void main(String[] args) {
Person alice = new Person("Alice", "A");
Person bruno = new Person("Bruno", "B");
Person autre = alice;
boolean testsOK = true;
// test des getters
if(!alice.getForename().equals("Alice") || !alice.getName().equals("A") || !bruno.getForename().equals("Bruno") || !bruno.getName().equals("B") || !autre.getForename().equals("Alice") || !autre.getName().equals("A")) {
System.out.println("Erreur de méthodes GET");
testsOK = false;
}
// test des setters
alice.setForename("Anatole");
bruno.setName("BB");
if(!alice.getForename().equals("Anatole") || !alice.getName().equals("A") || !bruno.getForename().equals("Bruno") || !bruno.getName().equals("BB") || !autre.getForename().equals("Anatole") || !autre.getName().equals("A")) {
System.out.println("Erreur de méthodes SET");
testsOK = false;
}
// test du equals
if(alice.equals(bruno) || !alice.equals(autre)) {
System.out.println("Erreur de méthode equals");
testsOK = false;
}
if(testsOK) System.out.println("Tests de UsePerson réussis !");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment